Created
May 6, 2024 17:02
-
-
Save ShutovKS/4aaf894c9305140683c33ef7db7a3014 to your computer and use it in GitHub Desktop.
Example Singleton "MonoBehaviour"
This file contains 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; | |
public class MonoBehaviourSingletonExample : MonoBehaviour | |
{ | |
public static MonoBehaviourSingletonExample Instance => _instance ?? Initialize(); | |
private static MonoBehaviourSingletonExample _instance; | |
private static MonoBehaviourSingletonExample Initialize() | |
{ | |
var instance = new GameObject(); | |
_instance = instance.AddComponent<MonoBehaviourSingletonExample>(); | |
DontDestroyOnLoad(_instance); | |
return _instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment