当前位置: 首页 > wzjs >正文

做论坛网站需要备案惠州营销网站建设

做论坛网站需要备案,惠州营销网站建设,做网站注意,wordpress短代码页面在Unity游戏开发中,高效地管理游戏循环和协程是至关重要的。今天将深入探讨三个相关的脚本,它们分别实现了单例模式的MonoSystem类、协程工具类以及一个测试脚本,让我们一起来揭开它们的神秘面纱😎! 1. MonoSystem类&a…

在Unity游戏开发中,高效地管理游戏循环和协程是至关重要的。今天将深入探讨三个相关的脚本,它们分别实现了单例模式的MonoSystem类、协程工具类以及一个测试脚本,让我们一起来揭开它们的神秘面纱😎!

1. MonoSystem类:实现不继承MonoBehaviour使用Unity生命周期函数🎮

1.1 概述

在Unity开发里,通常只有继承了MonoBehaviour的类才能使用诸如UpdateLateUpdateFixedUpdate等生命周期函数。而MonoSystem类的出现,就是为了让不继承MonoBehaviour的类也能使用这些重要的生命周期函数。它通过单例模式和事件委托的方式,统一管理这些生命周期函数的调用,使得其他类可以方便地注册和移除对这些函数的监听,同时还提供了协程的管理功能。

1.2 代码解析

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;namespace JKFrame
{/// <summary>/// 整个游戏只有一个Update、LateUpdate等/// </summary>public class MonoSystem : MonoBehaviour{private MonoSystem() { }private static MonoSystem instance;private Action updateEvent;private Action lateUpdateEvent;private Action fixedUpdateEvent;public static void Init(){instance = JKFrameRoot.RootTransform.GetComponent<MonoSystem>();instance.updateEvent = null;instance.lateUpdateEvent = null;instance.fixedUpdateEvent = null;}#region 生命周期函数/// <summary>/// 添加Update监听/// </summary>/// <param name="action"></param>public static void AddUpdateListener(Action action){instance.updateEvent += action;}/// <summary>/// 移除Update监听/// </summary>/// <param name="action"></param>public static void RemoveUpdateListener(Action action){instance.updateEvent -= action;}/// <summary>/// 添加LateUpdate监听/// </summary>/// <param name="action"></param>public static void AddLateUpdateListener(Action action){instance.lateUpdateEvent += action;}/// <summary>/// 移除LateUpdate监听/// </summary>/// <param name="action"></param>public static void RemoveLateUpdateListener(Action action){instance.lateUpdateEvent -= action;}/// <summary>/// 添加FixedUpdate监听/// </summary>/// <param name="action"></param>public static void AddFixedUpdateListener(Action action){instance.fixedUpdateEvent += action;}/// <summary>/// 移除FixedUpdate监听/// </summary>/// <param name="action"></param>public static void RemoveFixedUpdateListener(Action action){instance.fixedUpdateEvent -= action;}private void Update(){updateEvent?.Invoke();}private void LateUpdate(){lateUpdateEvent?.Invoke();}private void FixedUpdate(){fixedUpdateEvent?.Invoke();}#endregion#region 协程private Dictionary<object, List<Coroutine>> coroutineDic = new Dictionary<object, List<Coroutine>>();private static ObjectPoolModule poolModule = new ObjectPoolModule();/// <summary>/// 启动一个协程序/// </summary>public static Coroutine Start_Coroutine(IEnumerator coroutine){return instance.StartCoroutine(coroutine);}/// <summary>/// 启动一个协程序并且绑定某个对象/// </summary>public static Coroutine Start_Coroutine(object obj, IEnumerator coroutine){Coroutine _coroutine = instance.StartCoroutine(coroutine);if (!instance.coroutineDic.TryGetValue(obj, out List<Coroutine> coroutineList)){coroutineList = poolModule.GetObject<List<Coroutine>>();if (coroutineList == null) coroutineList = new List<Coroutine>();instance.coroutineDic.Add(obj, coroutineList);}coroutineList.Add(_coroutine);return _coroutine;}/// <summary>/// 停止一个协程序并基于某个对象/// </summary>public static void Stop_Coroutine(object obj, Coroutine routine){if (instance.coroutineDic.TryGetValue(obj, out List<Coroutine> coroutineList)){instance.StopCoroutine(routine);coroutineList.Remove(routine);}}/// <summary>/// 停止一个协程序/// </summary>public static void Stop_Coroutine(Coroutine routine){instance.StopCoroutine(routine);}/// <summary>/// 停止某个对象的全部协程/// </summary>public static void StopAllCoroutine(object obj){if (instance.coroutineDic.Remove(obj, out List<Coroutine> coroutineList)){for (int i = 0; i < coroutineList.Count; i++){instance.StopCoroutine(coroutineList[i]);}coroutineList.Clear();poolModule.PushObject(coroutineList);}}/// <summary>/// 整个系统全部协程都会停止/// </summary>public static void StopAllCoroutine(){// 全部数据都会无效foreach (List<Coroutine> item in instance.coroutineDic.Values){item.Clear();poolModule.PushObject(item);}instance.coroutineDic.Clear();instance.StopAllCoroutines();}#endregion}
}
1.2.1 单例模式

通过私有构造函数和静态实例instance,确保MonoSystem类在游戏中只有一个实例。在Init方法中,从JKFrameRoot.RootTransform获取该实例,并初始化事件委托。这样做的好处是,整个游戏中只有一个MonoSystem实例来管理生命周期函数和协程,避免了多个实例可能带来的冲突。

1.2.2 生命周期函数管理

提供了AddUpdateListenerRemoveUpdateListener等方法,允许不继承MonoBehaviour的其他类注册和移除对UpdateLateUpdateFixedUpdate方法的监听。在对应的生命周期方法中,通过事件委托调用注册的方法。例如,其他类可以通过调用MonoSystem.AddUpdateListener方法,将自己的Update逻辑注册到MonoSystem中,从而实现不继承MonoBehaviour也能使用Update函数。

1.2.3 协程管理

使用Dictionary<object, List<Coroutine>>来管理协程,允许协程与某个对象绑定。提供了启动、停止单个协程以及停止某个对象的全部协程和整个系统的全部协程的方法。这使得协程的管理更加灵活,方便开发者根据不同的需求控制协程的执行。

2. CoroutineTool类:高效协程工具🛠️

2.1 概述

CoroutineTool类是一个静态类,提供了多种等待时间和帧的方法,旨在避免协程中产生不必要的垃圾回收(GC)。

2.2 代码解析

using System.Collections;
using UnityEngine;namespace JKFrame
{/// <summary>/// 协程工具,避免GC/// </summary>public static class CoroutineTool{private struct WaitForFrameStruct : IEnumerator{public object Current => null;public bool MoveNext() { return false; }public void Reset() { }}private static WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();private static WaitForFixedUpdate waitForFixedUpdate = new WaitForFixedUpdate();public static WaitForEndOfFrame WaitForEndOfFrame(){return waitForEndOfFrame;}public static WaitForFixedUpdate WaitForFixedUpdate(){return waitForFixedUpdate;}public static IEnumerator WaitForSeconds(float time){float currTime = 0;while (currTime < time){currTime += Time.deltaTime;yield return new WaitForFrameStruct();}}public static IEnumerator WaitForSecondsRealtime(float time){float currTime = 0;while (currTime < time){currTime += Time.unscaledDeltaTime;yield return new WaitForFrameStruct();}}public static IEnumerator WaitForFrame(){yield return new WaitForFrameStruct();}public static IEnumerator WaitForFrames(int count = 1){for (int i = 0; i < count; i++){yield return new WaitForFrameStruct();}}}
}
2.2.1 避免GC的设计

通过使用静态的WaitForEndOfFrameWaitForFixedUpdate对象,避免每次调用时创建新的对象,从而减少GC。同时,自定义WaitForFrameStruct结构体作为等待帧的返回值,也有助于减少内存分配。

2.2.2 多种等待方法

提供了WaitForSecondsWaitForSecondsRealtimeWaitForFrameWaitForFrames等方法,满足不同的等待需求。

3. TestMonoSystem类:测试脚本🚀

3.1 概述

TestMonoSystem类是一个测试脚本,演示了如何使用MonoSystem类和CoroutineTool类,实现不继承MonoBehaviourUpdate和协程逻辑。

3.2 代码解析

using System.Collections;
using System.Collections.Generic;
using JKFrame;
using UnityEngine;public class TestMonoSystem : MonoBehaviour
{private TestMono testMono;void Start(){testMono = new TestMono();testMono.Init();}// Update is called once per framevoid Update(){}
}public class TestMono
{public void Init(){//注册Update逻辑MonoSystem.AddUpdateListener(Update);}private Coroutine coroutine;private void Update(){if (Input.GetKeyDown(KeyCode.W)){MonoSystem.RemoveUpdateListener(Update);}//开启关闭协程if (Input.GetKeyDown(KeyCode.S)){Debug.Log("按下s键 开始使用协程");coroutine = this.StartCoroutine(Do());}if (Input.GetKeyDown(KeyCode.A)){Debug.Log("按下a键 关闭使用协程");this.StopCoroutine(coroutine);}}private IEnumerator Do(){//协程工具//原版//yield return null;  //null 会被包装,也会产生GCwhile (true){yield return CoroutineTool.WaitForSeconds(2f);Debug.Log("又过了2帧");}}
}
3.2.1 注册Update逻辑

TestMono类的Init方法中,通过MonoSystem.AddUpdateListener方法注册Update逻辑。

3.2.2 协程控制

Update方法中,通过按键W移除Update监听,按键S启动协程,按键A停止协程。协程中使用CoroutineTool.WaitForSeconds方法等待2秒,并输出日志。

总结🌟

通过这三个脚本,我们实现了一个高效的游戏循环和协程管理系统。MonoSystem类让不继承MonoBehaviour的类也能使用Unity的生命周期函数,CoroutineTool类提供了避免GC的协程等待方法,TestMonoSystem类则演示了如何使用这些功能。希望这篇文章能帮助你更好地理解和应用Unity中的协程和游戏循环管理😘!


文章转载自:

http://Dyx46XfI.xntwk.cn
http://arJDJRdg.xntwk.cn
http://ZFIzub5i.xntwk.cn
http://EOxRYW7w.xntwk.cn
http://menMC5v8.xntwk.cn
http://7CUYhKiD.xntwk.cn
http://qyiqARzI.xntwk.cn
http://6UgavGUc.xntwk.cn
http://frRwtp8q.xntwk.cn
http://Lcgz4p1k.xntwk.cn
http://AKRTsYSR.xntwk.cn
http://i6f8v9Z0.xntwk.cn
http://euredsYw.xntwk.cn
http://Si0QnsZA.xntwk.cn
http://WIEa4Sr0.xntwk.cn
http://RZUWrNav.xntwk.cn
http://3qrUN87A.xntwk.cn
http://C5uqU9FJ.xntwk.cn
http://BVmznDTT.xntwk.cn
http://MUASmAJ3.xntwk.cn
http://Yh4hRov8.xntwk.cn
http://apBOnTiA.xntwk.cn
http://3RT7kUJh.xntwk.cn
http://2ZfMrMjE.xntwk.cn
http://9fGMErmF.xntwk.cn
http://UDMB3OAr.xntwk.cn
http://dcMQDLHL.xntwk.cn
http://pVNVdgUu.xntwk.cn
http://WyILJ77J.xntwk.cn
http://xXBnYwY7.xntwk.cn
http://www.dtcms.com/wzjs/771760.html

相关文章:

  • 佛山北京网站建设公司电话外呼系统
  • 内部门户网站建设方案东莞网页设计费用
  • 长沙正规制作网站公司wordpress 添加角色
  • 建网站基础知识做特卖的网站雅美盛典
  • 瑞丽网站建设在哪个网站上做实验仪器比较好
  • 句容住房和城乡建设局网站百度词条优化工作
  • 西宁网站建设推广东莞网站建设价格价格
  • 东莞外贸网站推广建设专业东莞网站建设报价
  • 做一个商城网站多少钱知名品牌形象策划公司
  • 网站没有备案是假的吗重庆做网站建设的公司
  • 青岛的网站建设公司北京建设工程二级市场网站
  • 常德做网站专业公司哪家好触摸屏网站开发
  • 交互设计个人网站潍坊做网站的公司
  • 深圳微信网站php租车网站
  • 网站静态首页模板wordpress nikkon
  • 职业生涯规划大赛项目名称手机360网站seo优化
  • 如何网站建设全包衡水做外贸网站
  • 免费发布黄页广告网站河南建筑信息平台
  • 伊春市网站建设个人的网站怎么备案表
  • h5制作的炫酷个人网站淘宝天猫优惠券网站建设费用
  • 网站界面怎么做一个网站源码值多少钱
  • 镇江网站建设哪家好网站开发流程联系方式
  • 钢结构网站.net电影网站开发
  • 域名提交收录seo专员工资一般多少
  • 自己搞网站做外贸福田做网站怎么样
  • 盗版视频网站怎么做的权威的合肥网站推广
  • 网站模版怎么样wordpress发表的文章百度抓取失败
  • 广元网站建设新公司做网站多少钱
  • 网站集约化建设论坛网站html模板
  • 辣条网站建设书asp国外网站