• 【Unity程序技巧】公共Update管理器


    在这里插入图片描述


    👨‍💻个人主页@元宇宙-秩沅

    👨‍💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!

    👨‍💻 本文由 秩沅 原创

    👨‍💻 收录于专栏Unity基础实战

    🅰️




    前言


    🎶(W 公共Update管理器


    1.涉及知识点

    在这里插入图片描述

    2.未优化的管理器特点

    • 为事件添加Mono的帧更新功能
    • 激活需要拖拽和add
    • 要继承Mono
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Events;
    
    //作用为事件添加Mono的帧更新功能--激活需要拖拽和add
    public class UpdateController : MonoBehaviour {
    
        private event UnityAction updateEvent;
    
    	void Start ()
        {
            DontDestroyOnLoad(this.gameObject); //过场景不移除
    	}
    	
    	void Update ()
        {
            if (updateEvent != null)
                updateEvent();
        }
    
        public void AddUpdateListener(UnityAction fun) //添加帧更新事件
        {
            updateEvent += fun;
        }
    
        public void RemoveUpdateListener(UnityAction fun)//移除帧更新事件
        {
            updateEvent -= fun;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    3.优化后的管理器特点

    • 为事件添加Mono的帧更新功能,也可以添加协程功能等
    • 激活不需要拖拽和add
    • 不用继承Mono
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using UnityEngine;
    using UnityEngine.Events;
    
    //作用为事件添加Mono的帧更新功能--激活不需要拖拽和add
    public class UpdateManager : SingleManager<UpdateManager>
    {
        private UpdateController  controller;
    
        public UpdateManager()
        {
            //保证了UpdateController对象的唯一性
            GameObject obj = new GameObject("UpdateController");
            controller = obj.AddComponent<UpdateController>();
        }
    
        public void AddUpdateListener(UnityAction fun)
        {
            controller.AddUpdateListener(fun);
        }
    
        public void RemoveUpdateListener(UnityAction fun)
        {
            controller.RemoveUpdateListener(fun);
        }
    
        //若需要开启协程则,直接可以将协程中的Coroutine中的反编译源码复制过来即可
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    🅰️


    【Unityc#专题篇】之c#进阶篇】

    【Unityc#专题篇】之c#核心篇】

    【Unityc#专题篇】之c#基础篇】

    【Unity-c#专题篇】之c#入门篇】

    【Unityc#专题篇】—进阶章题单实践练习

    【Unityc#专题篇】—基础章题单实践练习

    【Unityc#专题篇】—核心章题单实践练习


    你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!


    在这里插入图片描述


    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using UnityEngine;
    using UnityEngine.Events;

    //作用为事件添加Mono的帧更新功能–激活不需要拖拽和add
    public class UpdateManager : SingleManager
    {
    private UpdateController controller;

    public UpdateManager()
    {
        //保证了UpdateController对象的唯一性
        GameObject obj = new GameObject("UpdateController");
        controller = obj.AddComponent();
    }
    
    public void AddUpdateListener(UnityAction fun)
    {
        controller.AddUpdateListener(fun);
    }
    
    public void RemoveUpdateListener(UnityAction fun)
    {
        controller.RemoveUpdateListener(fun);
    }
    
    //若需要开启协程则,直接可以将协程中的Coroutine中的反编译源码复制过来即可
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    }
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Events;

    //作用为事件添加Mono的帧更新功能–激活需要拖拽和add
    public class UpdateController : MonoBehaviour {

    private event UnityAction updateEvent;
    
    void Start ()
    {
        DontDestroyOnLoad(this.gameObject); //过场景不移除
    }
    
    void Update ()
    {
        if (updateEvent != null)
            updateEvent();
    }
    
    public void AddUpdateListener(UnityAction fun) //添加帧更新事件
    {
        updateEvent += fun;
    }
    
    public void RemoveUpdateListener(UnityAction fun)//移除帧更新事件
    {
        updateEvent -= fun;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    }

  • 相关阅读:
    呼叫中心自建好还是云外呼好用?
    Consul服务注册与发现
    Dubbo之消费端服务RPC调用
    记一次血淋淋的MySQL崩溃修复案例
    ABAP VOFM定价过程的例程创建
    【Linux】进程控制
    【自然语言处理概述】文本词频分析
    Java运算符详解
    QT 使用百度语音识别--生成文本
    如何快速水出人生中的第一篇SCI,SCI收割器。
  • 原文地址:https://blog.csdn.net/m0_64128218/article/details/133969752