泛型单例模式工具类Singleton.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
private static T instance;
public static T Instance
{
get { return instance; }
}
protected virtual void Awake()
{
if (instance != null)
Destroy(gameObject);
else
instance = (T) this;
}
// 当前单例类型是否生成
public static bool IsInitialized
{
get { return instance != null; }
}
// 一个场景中有多个单例模式的话需要销毁
protected virtual void OnDestroy()
{
if (instance == this)
instance = null;
}
}
版权声明:本文为wankcn原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。