一、继承自MonoBehaviour
public class TestSingle : MonoBehaviour{
public static TestSingle Instance;
void Awake(){
Instance = this;
}
public void Show(){
Debug.Log("单例写法一");
}
}
函数调用
TestSingle.Instance.Show();
二、非继承自MonoBehaviour
public class TestSingleTwo{
private static TestSingleTwo instance;
public static TestSingleTwo Instance{
get
{
if(instance == null){
instance = new TestSingleTwo();
}
return instance;
}
}
public void ShowTwo(){
}
}
函数调用
TestSingleTwo.Instance.ShowTwo();