using System;
using UnityEngine;
public class SingletonMonobehaviour<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance;
public SingletonMonobehaviour()
{
if (SingletonMonobehaviour<T>._instance != null)
{
throw new Exception(typeof(T).ToString() + "singleton!");
}
SingletonMonobehaviour<T>._instance = this as T;
}
private static T ins
{
get
{
if (SingletonMonobehaviour<T>._instance == null)
{
SingletonMonobehaviour<T>._instance = Activator.CreateInstance<T>();
}
return SingletonMonobehaviour<T>._instance;
}
}
public static T Ins
{
get
{
return ins;
}
}
}
Unity单例模式
最新推荐文章于 2024-08-06 20:58:11 发布