👨💻个人主页:@元宇宙-秩沅
👨💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
👨💻 本文由 秩沅 原创
👨💻 收录于专栏:Unity基础实战
⭐🅰️⭐文章目录 ⭐🅰️⭐⭐前言⭐🎶(==W==) 公共Update管理器 1.涉及知识点2.未优化的管理器特点3.优化后的管理器特点 ⭐🅰️⭐
⭐前言⭐
🎶(W) 公共Update管理器
1.涉及知识点
2.未优化的管理器特点
为事件添加Mono的帧更新功能激活需要拖拽和add要继承Mono using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.Events;//作用为事件添加Mono的帧更新功能--激活需要拖拽和addpublic 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;}} 3.优化后的管理器特点
为事件添加Mono的帧更新功能,也可以添加协程功能等激活不需要拖拽和add不用继承Mono using System.Collections;using System.Collections.Generic;using System.ComponentModel;using UnityEngine;using UnityEngine.Events;//作用为事件添加Mono的帧更新功能--激活不需要拖拽和addpublic 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中的反编译源码复制过来即可} ⭐🅰️⭐
⭐【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<UpdateController>();}public void AddUpdateListener(UnityAction fun){controller.AddUpdateListener(fun);}public void RemoveUpdateListener(UnityAction fun){controller.RemoveUpdateListener(fun);}//若需要开启协程则,直接可以将协程中的Coroutine中的反编译源码复制过来即可} 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;}}