Wizard_Of_Wak/Assets/1_Script/System/DontDestroy.cs

36 lines
482 B
C#

using UnityEngine;
public class DontDestroy<T> : MonoBehaviour where T : DontDestroy<T>
{
static public T Instance { get; private set; }
void Awake()
{
if (Instance == null)
{
Instance = (T)this;
DontDestroyOnLoad(gameObject);
OnAwake();
}
else
{
Destroy(gameObject);
}
}
void Start()
{
if (Instance == (T)this)
{
OnStart();
}
}
virtual protected void OnAwake()
{
}
virtual protected void OnStart()
{
}
}