Created
October 10, 2018 22:19
-
-
Save dmitry1100/aa7ce765bc98655fc7e47c46198dbac9 to your computer and use it in GitHub Desktop.
Example on MonoBehaviour based singleton
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 characters
using UnityEngine; | |
namespace Fiftytwo | |
{ | |
public class App : MonoBehaviour | |
{ | |
public static App Instance; | |
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | |
private static void RuntimeInitializeOnLoad() | |
{ | |
var instance = Instantiate(AppSettings.Instance.AppPrefab); | |
instance.name = AppSettings.Instance.AppPrefab.name; | |
} | |
private void Awake() | |
{ | |
if (Instance != null) | |
throw new UnityException(typeof(App) + " is already instantiated"); | |
DontDestroyOnLoad(this); | |
Instance = this; | |
} | |
private void OnDestroy() | |
{ | |
Instance = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment