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();
        }




    }



}
  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值