unity 事件(委托)

头段时间做NGUI的时候,老大给我优化了很多,用到了C#的事件。由于我之前不是学C#的,下来花了点时间看了一下事件。我老大主要把事件用于对UI界面的切换。下面我们来看看代码吧。我的例子很简单的。

 

EventManager.cs  

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class EventArgs
  5. {
  6.  
  7. }
  8.  
  9. public class EventManager
  10. {
  11.     static EventManager _instance;
  12.     public static EventManager Instance
  13.     {
  14.         get
  15.         {
  16.             if (_instance == null)
  17.             {
  18.                 _instance = new EventManager();
  19.             }
  20.  
  21.             return _instance;
  22.         }
  23.     }
  24.  
  25.     public delegate void EventDelegate<T>(T e) where T : EventArgs;
  26.  
  27.     readonly Dictionary<Type, Delegate> _delegates = new Dictionary<Type, Delegate>();
  28.  
  29.     public void AddListener<T>(EventDelegate<T> listener) where T : EventArgs
  30.     {
  31.         Delegate d;
  32.         if (_delegates.TryGetValue(typeof(T), out d))
  33.         {
  34.             _delegates[typeof(T)] = Delegate.Combine(d, listener);
  35.         }
  36.         else
  37.         {
  38.             _delegates[typeof(T)] = listener;
  39.         }
  40.     }
  41.  
  42.     public void RemoveListener<T>(EventDelegate<T> listener) where T : EventArgs
  43.     {
  44.         Delegate d;
  45.         if (_delegates.TryGetValue(typeof(T), out d))
  46.         {
  47.             Delegate currentDel = Delegate.Remove(d, listener);
  48.  
  49.             if (currentDel == null)
  50.             {
  51.                 _delegates.Remove(typeof(T));
  52.             }
  53.             else
  54.             {
  55.                 _delegates[typeof(T)] = currentDel;
  56.             }
  57.         }
  58.     }
  59.  
  60.     public void Raise<T>(T e) where T : EventArgs
  61.     {
  62.         if (e == null)
  63.         {
  64.             throw new ArgumentNullException("e");
  65.         }
  66.  
  67.         Delegate d;
  68.         if (_delegates.TryGetValue(typeof(T), out d))
  69.         {
  70.             EventDelegate<T> callback = d as EventDelegate<T>;
  71.             if (callback != null)
  72.             {
  73.                 callback(e);
  74.             }
  75.         }
  76.     }
  77. }

 

 

EventArgs.cs

 

  1. using UnityEngine;
  2. using System.Collections;
  3. public class UIEventArgs : EventArgs {
  4.    
  5.     private bool _isOpen;
  6.     public bool IsOpen
  7.     {
  8.         get
  9.         {
  10.             return _isOpen;
  11.         }
  12.         set
  13.         {
  14.             _isOpen = value;
  15.         }
  16.     }
  17. }

 

 

EventListener.cs(绑定在一个物体上)

 

  1. using UnityEngine;
  2. using System.Collections;
  3. public class EventListener : MonoBehaviour {
  4.     // Use this for initialization
  5.     void Start () {
  6.        EventManager.Instance.AddListener<UIEventArgs>(OnReceive);
  7.     }
  8.     void OnReceive(UIEventArgs e)
  9.     {
  10.         if(e.IsOpen)
  11.         {
  12.             print ("Name:"+gameObject.name);
  13.         }
  14.     }
  15.    
  16. }

 

 

SendEventMeg.cs  (绑定在一个物体上)

  1. using UnityEngine;
  2. using System.Collections;
  3. public class SendEventMeg : MonoBehaviour {
  4.     // Use this for initialization
  5.     void Start () {
  6.        
  7.     }
  8.    
  9.     void Send ()
  10.     {
  11.         var arg = new UIEventArgs()
  12.         {
  13.             IsOpen = true,
  14.         };
  15.         EventManager.Instance.Raise(arg);
  16.     }
  17.     void OnGUI()
  18.     {
  19.         if(GUILayout.Button("Send"))
  20.         {
  21.             Send();
  22.         }
  23.     }
  24. }

 

 

结果:打印出“Name:xxxxx”.

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值