Unity MonoBehaviour单例

using UnityEngine;

namespace Framework {
	/// 
	/// MonoBehaviour单例
	/// 
	/// 单例类
	public abstract class MonoSingleton : MonoBehaviour where T : MonoSingleton {
		private static T s_Instatnce;
		/// 
		/// 实例
		/// 
		public static T Instance {
			get {
				if (s_Instatnce == null) {
					s_Instatnce = FindObjectOfType () ?? new GameObject ().AddComponent ();
				}
				return s_Instatnce;
			}
		}

		protected virtual void Awake () {
			gameObject.name = GetType ().Name;
			transform.SetParent (Core.Instance.transform, false);
			transform.hideFlags = HideFlags.NotEditable;
		}
	}
}

 

你可能感兴趣的:(Unity基础框架)