怎么使用MonoBehaviour的单件
using UnityEngine;
using System;
using System.Collections.Generic;
public class SingletonBehaviour<S> : MonoBehaviour where S : MonoBehaviour
{
private static S mSingleton;
public virtual void Awake()
{
mSingleton = (S)(MonoBehaviour)this;
// Debug.LogError("SingletonBehaviour<S> Awake = " + mSingleton.GetType().ToString());
}
public static S GetSingleton()
{
return mSingleton;
}
public static S GetInst()
{
return GetSingleton();
}
public static S GetInstance()
{
return GetSingleton();
}
public static S Instance
{
get
{
return GetSingleton();
}
}
}
比如一个类A继承MonoBehaviour,当想使用这个类的实例时,不需要去动态创建了,只需让他继承SingletonBehaviour,通过A.GetInstance()就得到了这个类的实例
浙公网安备 33010602011771号