我是Unity的初學者。實現Unity Singleton的最佳方式是什麼?
我目前在使用Singleton。
我在學習的時候有問題。
什麼是最好的方式來實現統一Singleton像標題?
我明白爲什麼使用單身人士。那就是......「 」要創建一個對象並在需要時使用它。「
我是否理解正確?
有很多方法來實現單身人士。這是正確的? 然後,許多方法的哪一部分被共同使用?
我單身代碼:
public class GameSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
protected static T instance = null;
protected virtual void Awake()
{
Debug.Log ("instance was !!!!! >>" + instance);
if(instance != null) // 최초 instance 유효성 검사
{
Debug.Log(">> Game Singleton already exist!");
return;
}
instance = FindObjectOfType<T>(); //
Debug.Log("FindObj" + "/" + "instance is >> " + instance);
}
// Use this for initialization
protected virtual void Start()
{
}
// Update is called once per frame
protected virtual void Update()
{
}
protected virtual void OnDestroy()
{
if (instance != null)
{
instance = null;
}
}
public static T Instance
{
get
{
if(instance == null)
{
GameObject go = new GameObject();
go.transform.position = Vector3.zero; // Vector3(0,0,0)
go.transform.rotation = Quaternion.identity; // 회전 없음
go.transform.localScale = Vector3.one;
go.hideFlags = HideFlags.HideAndDontSave;
instance = go.AddComponent<T>();
instance.name = (typeof(T)).ToString();
Debug.Log(">> Game Singleton instance create : " + instance);
}
return instance;
}
}
}
我們將不勝感激,如果你可以檢查代碼, 但我們也很感激幾個關鍵點。
我已經看過幾個文件,但他們只是變得複雜。 我的水平非常低。如果您能夠輕鬆解釋,我將不勝感激。