Action 带参数的委托方法使用,并赋值操作

**********************************************含委托参数的方法定义**************************************************

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ActionArmy : MonoBehaviour {


    public static void CreateArmy(GameObject go, Action<ActionArmy> callback)
    {
        //安全校验
        if (go != null)
        {
            //参数操作
            ActionArmy army = go.AddComponent<ActionArmy>();
            //校验
            if (callback != null)
            {
                //回调方法
                callback(army);
            }
        }
    }

}

*****************************************************调用执行****************************************************

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Army : MonoBehaviour {

    //私有对象
    ActionArmy actionArmy;
    public GameObject go;
void Start () {
        //调用创建对象,并赋值给actionArmy
        ActionArmy.CreateArmy(go,(army)=>{actionArmy=army;});
}
}

posted @ 2018-06-08 16:10  低小调  阅读(534)  评论(0编辑  收藏  举报