50 lines
1.3 KiB
C#
50 lines
1.3 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
namespace Touka.GameLogic {
|
|
/// <summary>
|
|
/// 单例类
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public class ToukaSingleton<T> where T : MonoBehaviour
|
|
{
|
|
private static T _instance;
|
|
|
|
public static T GetInstance()
|
|
{
|
|
if (null == _instance)
|
|
{
|
|
_instance = Object.FindObjectOfType(typeof(T)) as T;
|
|
if (null != _instance) return _instance;
|
|
|
|
GameObject container = new GameObject
|
|
{
|
|
name = typeof(T).ToString()
|
|
};
|
|
container.hideFlags = HideFlags.HideInHierarchy;
|
|
_instance = container.AddComponent(typeof(T)) as T;
|
|
Object.DontDestroyOnLoad(container);
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
public abstract class ToukaSingletonMonoBehaviour<T> : MonoBehaviour where T : MonoBehaviour
|
|
{
|
|
public static T Instance
|
|
{
|
|
get { return ToukaSingleton<T>.GetInstance(); }
|
|
}
|
|
|
|
public static T GetInstance()
|
|
{
|
|
return ToukaSingleton<T>.GetInstance();
|
|
}
|
|
|
|
public virtual void Dispose()
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|