我是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;
}
}
}
我们将不胜感激,如果你可以检查代码, 但我们也很感激几个关键点。
我已经看过几个文件,但他们只是变得复杂。 我的水平非常低。如果您能够轻松解释,我将不胜感激。