Hyluss

[Unity] GameObjectExtensions

Nov 29th, 2017
465
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. public class GameObjectExtensions
  2. {
  3.     private static void InvokeParameterisedAwake(GameObject newObject, object[] arg)
  4.     {
  5.         foreach (MonoBehaviour behaviour in newObject.GetComponents<MonoBehaviour>())
  6.         {
  7.             foreach (MethodInfo info in behaviour.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance))// | BindingFlags.DeclaredOnly))
  8.             {
  9.                 if (String.Compare(info.Name, "Awake") == 0)
  10.                 {
  11.                     ParameterInfo[] parameters = info.GetParameters();
  12.                     if (parameters.Length == arg.Length)
  13.                     {
  14.                         bool typesMatch = true;
  15.                         for (int i = 0; i < (parameters.Length)  (typesMatch == true); i++)
  16.                         {
  17.                             if (parameters[i].ParameterType != arg[i].GetType())
  18.                             {
  19.                                 typesMatch = false;
  20.                             }
  21.  
  22.                         }
  23.  
  24.                         if (typesMatch == true)
  25.                         {
  26.                             info.Invoke(behaviour, arg);
  27.                         }
  28.  
  29.                     }
  30.  
  31.                 }
  32.             }
  33.         }
  34.     }
  35.     public static GameObject Instantiate(GameObject O, params object[] arg)
  36.     {
  37.         GameObject newObject = GameObject.Instantiate(O) as GameObject;
  38.  
  39.         InvokeParameterisedAwake(newObject, arg);
  40.         return newObject;
  41.     }
  42.  
  43. }
  44.  
  45.  
  46. public class AwakeTest : MonoBehaviour
  47. {
  48.     public GameObject target;
  49.  
  50.     void Start()
  51.     {
  52.         GameObject go1 = GameObjectExtensions.Instantiate(target, 10);
  53.         GameObject go2 = GameObjectExtensions.Instantiate(target, "Hello world!");
  54.         GameObject go3 = GameObjectExtensions.Instantiate(target, 10.2f, 20.5f, 30);
  55.     }
  56.  
  57. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment