关于unity中委托等的用法
不多说,直接上代码,不足之处,多多指教
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using UnityEngine; 5 using UnityEngine.Events; 6 7 public class ActionAndUnityEvent : MonoBehaviour { 8 9 public delegate int m_delegate(int a,int b, int c); 10 m_delegate m_mydelegate; 11 event m_delegate m_event; 12 13 public Action<int, int> m_action; 14 15 public UnityAction<int, int> m_unityAction; 16 17 public UnityEvent m_unityEvent = new UnityEvent(); 18 void Start() 19 { 20 21 22 m_unityEvent.AddListener(() => ListenerAdd(10,10)); 23 m_unityEvent.Invoke(); 24 25 26 27 m_mydelegate += Add; 28 if (m_mydelegate != null) 29 { 30 m_mydelegate(1, 2, 3); 31 } 32 33 34 35 m_event += Add; 36 if (m_event!=null) 37 { 38 m_event(1,3,5); 39 } 40 41 42 43 m_unityAction += Add; 44 if (m_unityAction!=null) 45 { 46 m_unityAction(3, 5); 47 } 48 49 50 51 m_action += Add; 52 if (m_action!=null) 53 { 54 m_action(2, 3); 55 } 56 } 57 58 59 public void Add(int a, int b) 60 { 61 Debug.Log(a + b); 62 } 63 64 65 66 public int Add(int a, int b, int c) 67 { 68 Debug.Log(a + b + c); 69 return a + b + c; 70 } 71 72 73 74 75 public void ListenerAdd(int a,int b) 76 { 77 Debug.Log(a + b); 78 } 79 }