Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Security;
  5. using UnityEngine;
  6.  
  7. public class Events : MonoBehaviour
  8. {
  9.     public GameObject[] SubscribedOnAwake;
  10.  
  11.     //-- Actions list --//
  12.     public Action OnAppStarted = delegate { };
  13.     public Action<object> OnEggPulling = delegate { }; // float
  14.     public Action<object> OnEggVelocity = delegate { }; // float
  15.     public Action OnEggStartFly = delegate { };
  16.     public Action OnEggAttached = delegate { };
  17.     public Action OnEggEaten = delegate { };
  18.     public Action OnEggFail = delegate { };
  19.  
  20.     //-- Subscribe --//
  21.     private List<GameObject> m_SubscribedGameObjects = new List<GameObject>();
  22.  
  23.     //-- Reflection --//
  24.     private List<FieldInfo> m_FieldInfoActions = new List<FieldInfo>();
  25.     private List<FieldInfo> m_FieldInfoActionsOneParameter = new List<FieldInfo>();
  26.  
  27.     private void Awake()
  28.     {
  29.         FullfillFieldsInfo();
  30.         for (int i = 0; i < SubscribedOnAwake.Length; i++)
  31.         {
  32.             Subscribe(SubscribedOnAwake[i]);
  33.         }
  34.     }
  35.  
  36.     public void Subscribe(GameObject gameObject)
  37.     {
  38.         if (m_SubscribedGameObjects.Contains(gameObject))
  39.             return;
  40.  
  41.         for (int i = 0; i < m_FieldInfoActions.Count; i++)
  42.         {
  43.             var fieldInfo = m_FieldInfoActions[i];
  44.             var action = (Action)fieldInfo.GetValue(this);
  45.             AddInvoke(ref action, () => gameObject.SendMessage(fieldInfo.Name, SendMessageOptions.DontRequireReceiver));
  46.             fieldInfo.SetValue(this, action);
  47.         }
  48.  
  49.         for (int i = 0; i < m_FieldInfoActionsOneParameter.Count; i++)
  50.         {
  51.             var fieldInfo = m_FieldInfoActionsOneParameter[i];
  52.             var action = (Action<object>)fieldInfo.GetValue(this);
  53.             AddInvoke(ref action, (v) => gameObject.SendMessage(fieldInfo.Name, v, SendMessageOptions.DontRequireReceiver));
  54.             fieldInfo.SetValue(this, action);
  55.         }
  56.  
  57.         m_SubscribedGameObjects.Add(gameObject);
  58.     }
  59.  
  60.     private void AddInvoke(ref Action action, Action invoke) => action += invoke;
  61.     private void AddInvoke<T>(ref Action<T> action, Action<T> invoke) => action += invoke;
  62.  
  63.     private void FullfillFieldsInfo()
  64.     {
  65.         try
  66.         {
  67.             BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public;
  68.             Type myTypeEvent = typeof(Events);
  69.             var fields = myTypeEvent.GetFields(myBindingFlags);
  70.  
  71.             foreach (var item in fields)
  72.             {
  73.                 if (typeof(System.MulticastDelegate) == item.FieldType.BaseType)
  74.                 {
  75.                     var value = item.GetValue(this);
  76.                     var type = value.GetType();
  77.  
  78.                     var del = (System.MulticastDelegate)value;
  79.                     var delParametrsLenght = del.Method.GetParameters().Length;
  80.  
  81.                     if (delParametrsLenght == 0) m_FieldInfoActions.Add(item);
  82.                     else if (delParametrsLenght == 1) m_FieldInfoActionsOneParameter.Add(item);
  83.                 }
  84.             }
  85.         }
  86.         catch (SecurityException e) { Debug.LogError("SecurityException :" + e.Message); }
  87.         catch (ArgumentNullException e) { Debug.LogError("ArgumentNullException : " + e.Message); }
  88.         catch (Exception e) { Debug.LogError("Exception : " + e.Message); }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement