Action 最简单的委托消息传送机制

****************************************************消息触发机制,委托的定义********************************************

using System;

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

public class MyInput : MonoBehaviour {

    public Action<int> OnInput;
void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            int j = UnityEngine.Random.Range(0, 10);
            OnInput(j);
        }
}

}

**********************************************消息执行机制,读取消息**************************************************

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

public class TestAction : MonoBehaviour {
   public MyInput act_MyInput;
    // Use this for initialization
    void Start()
    {
        act_MyInput.OnInput = OutPut;
        act_MyInput.OnInput += OutEvnPut;
    }
    void OutPut(int arg)
    {
        Debug.Log(arg);
    }
    void OutEvnPut(int arg)
    {
        Debug.Log("Evn"+arg );
    }
  
    void OnDestroy()
    {
        act_MyInput.OnInput -= OutEvnPut;
    }

}

完成消息在脚本之间的传输

posted @ 2018-06-07 17:25  低小调  阅读(61)  评论(0编辑  收藏  举报