Advertisement
Guest User

Unity3D Instantiating with code execution before Awake()

a guest
Nov 26th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.62 KB | None | 0 0
  1. public static GameObject Rez(string prefabPath, Action onBeforeAwake = null)
  2. {
  3.     return Rez((GameObject)Resources.Load(prefabPath, typeof(GameObject)), onBeforeAwake);
  4. }
  5.  
  6. public static GameObject Rez(GameObject prefab, Action onBeforeAwake = null)
  7. {
  8.     var active = prefab.activeSelf;
  9.     if (active)
  10.     {
  11.         prefab.SetActive(false);
  12.     }
  13.  
  14.     var newGameObject = (GameObject)Instantiate(prefab);
  15.     newGameObject.name = prefab.name;
  16.  
  17.     onBeforeAwake.SafeInvoke();
  18.        
  19.     if (active)
  20.     {
  21.         newGameObject.SetActive(true);
  22.         prefab.SetActive(true);
  23.     }
  24.  
  25.     return newGameObject;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement