Created
November 5, 2020 22:07
-
-
Save dd01da0465b4542f8f8af4ecedc149ed/f4ae147d45d7a5b5a79bd71227a2569c to your computer and use it in GitHub Desktop.
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
public class Example : SingletonBehaviour<Example> | |
{ | |
private void Awake() | |
{ | |
Assign(this); | |
// ... | |
} | |
} |
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; | |
public abstract class SingletonBehaviour<T> : MonoBehaviour | |
{ | |
public static T Instance { get; private set; } | |
protected void Assign(T instance) | |
{ | |
if (Instance != null) | |
Destroy(gameObject); | |
else | |
{ | |
Instance = instance; | |
DontDestroyOnLoad(gameObject); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment