物体间通信-deletgate
除了SendMessage 那几个方法以外 还可以使用delegate 来实现
Listener: using UnityEngine; using System.Collections; public class Listener : MonoBehaviour { // Use this for initialization void Start () { EventDispatcher ev = GameObject.Find("Cube").GetComponent<EventDispatcher>(); ev.ev += OnEvent;//可装载多个delegate ev.ev += OnEvent; // ev.ev = OnEvent; } void OnEvent(GameObject obj) { Debug.Log(obj.ToString() + "123" ); } // Update is called once per frame void Update () { } }
EventDispatcher: using UnityEngine; using System.Collections; public class EventDispatcher : MonoBehaviour { public delegate void EventHandle(GameObject obj); public EventHandle ev; // Use this for initialization void Start() { if (ev!=null) { ev(this.gameObject); } } // Update is called once per frame void Update() { } }