Unity 单例模式

单例模式:

如果要让一个类只有唯一的一个对象,则可以使用单例模式来写。使用的时候用“类名.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();
        }




    }



}
### Unity 中实现单例模式的方法 在 Unity 开发中,单例模式是一种常见的设计模式,用于确保某个类只有一个实例并提供全局访问点。以下是几种常见的方式以及它们的优缺点。 --- #### 方法一:基于 `MonoBehaviour` 的简单单例模式 这种方法适用于需要继承自 `MonoBehaviour` 的类,并且可以在场景之间保持状态不变的情况。 ```csharp public class MySingleton : MonoBehaviour { private static MySingleton instance; public static MySingleton Instance { get { if (instance == null) { instance = FindObjectOfType<MySingleton>(); if (instance == null) { instance = new GameObject("MySingleton").AddComponent<MySingleton>(); DontDestroyOnLoad(instance.gameObject); } } return instance; } } private void Awake() { if (instance != null && instance != this) { Destroy(gameObject); } else { instance = this; DontDestroyOnLoad(gameObject); } } } ``` 此方法通过静态属性 `Instance` 提供全局访问接口,并利用 `Awake()` 函数来确保同一时间只存在一个实例[^2]。 --- #### 方法二:泛型单例模式 对于不依赖于 `MonoBehaviour` 的类,可以使用泛型方式实现单例模式。这种方式更加通用,适合纯逻辑类。 ```csharp using System; public class Singleton<T> where T : class, new() { private static readonly Lazy<T> lazyInstance = new Lazy<T>(() => new T()); public static T Instance => lazyInstance.Value; protected Singleton() { } } // 使用示例 public class GameManager : Singleton<GameManager> { public void SomeMethod() { Console.WriteLine("GameManager Method Called"); } } ``` 上述代码定义了一个泛型单例基类 `Singleton<T>`,并通过子类 `GameManager` 来调用其唯一实例[^5]。 --- #### 方法三:带自动销毁机制的单例模式 为了防止重复创建对象,可以通过检测当前场景是否存在该单例实例来进行优化。 ```csharp public class PlayerSingletonMono : MonoBehaviour { private static PlayerSingletonMono _instance; public static PlayerSingletonMono Instance { get { if (_instance == null) { _instance = FindObjectOfType<PlayerSingletonMono>(); if (_instance == null) { _instance = new GameObject("PlayerSingletonMono").AddComponent<PlayerSingletonMono>(); DontDestroyOnLoad(_instance.gameObject); } } return _instance; } } private void OnApplicationQuit() { _instance = null; } private void OnDestroy() { _instance = null; } } ``` 这种实现方式不仅提供了线程安全的初始化过程,还能够在应用退出或对象销毁时清理资源[^4]。 --- #### 注意事项 1. 如果单例需要跨越多个场景,则应始终调用 `DontDestroyOnLoad(this)`。 2. 对于性能敏感的应用程序,建议减少不必要的单例数量,因为过多的单例可能导致内存泄漏或其他问题。 --- ### 总结 以上三种方法分别针对不同需求进行了优化。开发者可以根据实际项目情况选择最适合自己的实现方式。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值