Created
July 29, 2020 08:00
-
-
Save jscMR/e1d866b52f6e788eae70a7a0b858e0d5 to your computer and use it in GitHub Desktop.
Revisions
-
jscMR created this gist
Jul 29, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour { private static readonly Lazy<T> LazyInstance = new Lazy<T>(CreateSingleton); public static T Instance => LazyInstance.Value; private static T CreateSingleton() { var ownerObject = new GameObject($"{typeof(T).Name} (singleton)"); var instance = ownerObject.AddComponent<T>(); DontDestroyOnLoad(ownerObject); return instance; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ public class GameManager : Singleton<GameManager> { ... }