Skip to content

Instantly share code, notes, and snippets.

@daCyuubi
Created November 5, 2020 22:07
Show Gist options
  • Save daCyuubi/f4ae147d45d7a5b5a79bd71227a2569c to your computer and use it in GitHub Desktop.
Save daCyuubi/f4ae147d45d7a5b5a79bd71227a2569c to your computer and use it in GitHub Desktop.
public class Example : SingletonBehaviour<Example>
{
private void Awake()
{
Assign(this);
// ...
}
}
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