Unity3D中可中途释放的单例
使用静态类,静态变量的坏处是从程序加载后就一直占用内存,想要释放比较麻烦,可是之前使用的单例,没有提供释放的方法,那是不是也同静态的一样直到程序结束菜释放?那单例的好处是什么?
所以此处在单例中加入了可释放的方法来方便释放单例。
用途是:
用此单例管理场景物体时,在不切换场景的前提下释放掉该单例以及挂在单例游戏物体下的子物体
using UnityEngine;
public abstract class SingleBhv<T> : IMono
where T:MonoBehaviour
{
private static T m_instance;
private static object locked;
public static void SetInstance(T instance)
{
m_instance = instance;
}
public static T Instance
{
get
{
if (m_instance == null)
{
m_instance = FindObjectOfType<T>();
}
if (m_instance == null)
{
m_instance = new GameObject("_"+typeof(T).ToString()).AddComponent<T>();
}
return m_instance;
}
set
{
m_instance = value;
}
}
public override void Init()
{
m_instance = this as T;
isInited = true;
}
/// <summary>
/// 释放掉
/// </summary>
public static void Destroy()
{
if (m_instance)
{
GameObject.Destroy(m_instance.gameObject);
}
}
}
public abstract class IMono : MonoBehaviour
{
public bool isInited;
protected virtual void Start()
{
if (!isInited)
{
Init();
}
}
public virtual void Init()
{
isInited = true;
}
}
非MonoBeheavier的单例
外部调用以下代码来释放:
XX.Instance = null;
GC.Collect();
注意事项:
其他地方不要存放该类的实例,要用该类时就用XX.Instance
using UnityEngine;
/// <summary>
/// single struct
/// </summary>
/// <typeparam name="T"></typeparam>
namespace SingleInstance
{
public class SingleInstance<T> where T : new()
{
public static T m_instance ;
private static Object locked = new Object();
public static T GetInstance()
{
if (m_instance == null)
{
lock (locked)
{
if (m_instance == null)
{
m_instance = new T();
}
}
}
return m_instance;
}
public static T Instance
{
get
{
return GetInstance();
}
set
{
m_instance = value;
}
}
}
}