unity 事件(委托)

来源:互联网 发布:ubuntu怎么安装jdk 编辑:程序博客网 时间:2024/05/02 01:54

头段时间做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”.

 

 

 

 

 

 

 

原创粉丝点击