using UnityEngine;
using System.Collections;
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T instance;
/**
Returns the instance of this singleton.
*/
public static T Instance
{
get
{
if (instance == null)
{
instance = (T)FindObjectOfType(typeof(T));
if (instance == null)
{
Debug.LogWarning("An instance of " + typeof(T) + " is needed in the scene, but there is none.");
}
}
return instance;
}
}
protected virtual void Awake()
{
instance = this as T;
}
protected virtual void OnDestroy()
{
if (instance == this)
{
instance = null;
}
}
}
Unity3D 单利模式
最新推荐文章于 2022-12-08 17:06:45 发布