Deozaan

Self-Creating Singleton Method for Unity

Jan 5th, 2016
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.62 KB | None | 0 0
  1. private static INSTANCE_CLASS m_Instance;
  2.  
  3. /// <summary>
  4. /// This callback is called after all Awake functions have been called by Unity.
  5. /// This makes sure that everything is initialized (a big issue in the editor)
  6. /// before creating an instance.
  7. /// </summary>
  8. [RuntimeInitializeOnLoadMethod]
  9. private static void CreateObject() {
  10.     if (m_Instance == null) {
  11.         var go = new GameObject("NAME OF OBJECT");
  12.         m_Instance = go.AddComponent<INSTANCE_CLASS>();
  13.         go.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
  14.         DontDestroyOnLoad(go);
  15.         m_Instance.Initialize();
  16.     }
  17. }
  18.  
  19. private void Initialize() {
  20.  
  21. }
Advertisement
Add Comment
Please, Sign In to add comment