单例模式:
如果要让一个类只有唯一的一个对象,则可以使用单例模式来写。使用的时候用“类名.Instance.成员名”的形式来访问这个对象的成员。
步骤:
1、把构造函数私有化,防止外部创建对象。
2、提供一个属性给外部访问,这个属性就相当于是这个类唯一的对象。
单例模式分为饿汉式和懒汉式两种。
1、饿汉式单例模式:
在程序一开始的时候就创建了单例对象。但这样一来,这些对象就会在程序一开始时就存在于内存之中,占据着一定的内存。
2、懒汉式单例模式:
在用到单例对象的时候才会创建单例对象。
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 不继承MonoBehaviour的单例模式基类 /// 作用:继承了这个类的类自带单例模式 /// </summary> public class SingletonPatternBase<T> where T:SingletonPatternBase<T>//泛型T必须为这个类本身或者是它的子类 { //构造方法私有化,防止外部new对象。 protected SingletonPatternBase() { } //线程锁。当多线程访问时,同一时刻仅允许一个线程访问。 private static object locker = new object(); //提供一个属性给外部访问,这个属性就相当于是单例对象。 //volatile关键字修饰的字段,当多个线程都对它进行修改时,可以确保这个字段在任何时刻呈现的都是最新的值。 private volatile static T instance; public static T Instance { get { if (instance == null) { lock (locker) { //保证对象的唯一性。 if (instance == null) { instance = Activator.CreateInstance(typeof(T), true) as T;//使用反射,调用无参构造方法创建对象。 } } } return instance; } } }
Mono管理器
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; /// <summary> /// Mono管理器 /// </summary> public class MonoManager :SingletonPatternBase<MonoManager> { //构造方法私有化,防止外部new对象。 private MonoManager() { } private MonoController monoExecuter; private MonoController MonoExecuter { get { if (monoExecuter==null) { GameObject go = new GameObject(typeof(MonoController).Name);//相当于就是"MonoController" monoExecuter = go.AddComponent<MonoController>(); } return monoExecuter; } } /// <summary> /// 让外部通过它来开启协程 /// </summary> public Coroutine StartCoroutine(IEnumerator routine) { return MonoExecuter.StartCoroutine(routine); } /// <summary> /// 让外部通过它来停止协程 /// </summary> public void StopCoroutine(IEnumerator routine) { if(routine!=null) MonoExecuter.StopCoroutine(routine); } /// <summary> /// 让外部通过它来停止协程 /// </summary> public void StopCoroutine(Coroutine routine) { if(routine!=null) MonoExecuter.StopCoroutine(routine); } /// <summary> /// 让外部通过它来停止所有协程 /// </summary> public void StopAllCoroutines() { MonoExecuter.StopAllCoroutines(); } /// <summary> /// 添加Update事件 /// </summary> public void AddUpdateListener(UnityAction call) { MonoExecuter.AddUpdateListener(call); } /// <summary> /// 移除Update事件 /// </summary> public void RemoveUpdateListener(UnityAction call) { MonoExecuter.RemoveUpdateListener(call); } /// <summary> /// 移除所有Update事件 /// </summary> public void RemoveAllUpdateListeners() { MonoExecuter.RemoveAllUpdateListeners(); } /// <summary> /// 添加FixedUpdate事件 /// </summary> public void AddFixedUpdateListener(UnityAction call) { MonoExecuter.AddFixedUpdateListener(call); } /// <summary> /// 移除FixedUpdate事件 /// </summary> public void RemoveFixedUpdateListener(UnityAction call) { MonoExecuter.RemoveFixedUpdateLinstener(call); } /// <summary> /// 移除所有FixedUpdate事件 /// </summary> public void RemoveAllFixedUpdateListeners() { MonoExecuter.RemoveAllFixedUpdateListeners(); } /// <summary> /// 添加LateUpdate事件 /// </summary> public void AddLateUpdateListener(UnityAction call) { MonoExecuter.AddLateUpdateListener(call); } /// <summary> /// 移除LateUpdate事件 /// </summary> public void RemoveLateUpdateListener(UnityAction call) { MonoExecuter.RemoveLateUpdateListener(call); } /// <summary> /// 移除所有LateUpdate事件 /// </summary> public void RemoveAllLateUpdateListeners() { MonoExecuter.RemoveAllLateUpdateListeners(); } /// <summary> /// 移除FixedUpdate、Update、LateUpdate的所有事件 /// </summary> public void RemoveAllListeners() { MonoExecuter.RemoveAllListeners(); } public class MonoController : MonoBehaviour { event UnityAction updateEvent;//在生命周期方法Update中执行的事件 event UnityAction fixedUpdateEvent;//在生命周期方法FixedUpdate中执行的事件 event UnityAction lateUpdateEvent;//在生命周期方法LateUpdate中执行的事件 void FixedUpdate() { fixedUpdateEvent?.Invoke(); } void Update() { //if (updateEvent != null) // updateEvent.Invoke(); updateEvent?.Invoke(); } void LateUpdate() { lateUpdateEvent?.Invoke(); } public void AddUpdateListener(UnityAction call) { updateEvent += call; } public void RemoveUpdateListener(UnityAction call) { updateEvent -= call; } public void RemoveAllUpdateListeners() { updateEvent = null; } public void AddFixedUpdateListener(UnityAction call) { fixedUpdateEvent += call; } public void RemoveFixedUpdateLinstener(UnityAction call) { fixedUpdateEvent -= call; } public void RemoveAllFixedUpdateListeners() { fixedUpdateEvent = null; } public void AddLateUpdateListener(UnityAction call) { lateUpdateEvent += call; } public void RemoveLateUpdateListener(UnityAction call) { lateUpdateEvent -= call; } public void RemoveAllLateUpdateListeners() { lateUpdateEvent = null; } public void RemoveAllListeners() { RemoveAllFixedUpdateListeners(); RemoveAllUpdateListeners(); RemoveAllLateUpdateListeners(); } } }