26 lines
603 B
C#
26 lines
603 B
C#
using UnityEngine;
|
|
|
|
public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
|
|
{
|
|
private static T instance;
|
|
|
|
public static T Instance
|
|
{
|
|
get => instance;
|
|
}
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
//已经存在一个对应的单例模式对象了 不需要在有一个了
|
|
if (instance != null)
|
|
{
|
|
Destroy(this);
|
|
return;
|
|
}
|
|
instance = this as T;
|
|
//我们挂载继承该单例模式基类的脚本后 依附的对象过场景时就不会被移除了
|
|
//就可以保证在游戏的整个生命周期中都存在
|
|
DontDestroyOnLoad(instance.gameObject);
|
|
}
|
|
}
|