- 最后登录
- 2019-12-25
- 注册时间
- 2012-8-24
- 阅读权限
- 90
- 积分
- 71088
- 纳金币
- 52336
- 精华
- 343
|
Unity委托简单例子- public class EventDispatcher : MonoBehaviour
- {
- public delegate void EventHandler(GameObject e);//定义委托
- public event EventHandler MouseOver;
- void Start()
- {
- OnMouseOver();
- }
- void OnMouseOver()
- {
- if (MouseOver != null)
- {
- MouseOver(this.gameObject);//传递参数
- }
- }
- }
- public class EventDis : MonoBehaviour {
- public GameObject game;
- EventDispatcher EventHand;
- void Start () {
- EventHand = game.GetComponent<EventDispatcher>();
- }
- void Listener(GameObject g)
- {
- Debug.Log(g.name);
- }
- void OnMouseDown()
- {
- EventHand.MouseOver += Listener;//实现委托
- }
- }
复制代码 |
|