公共模块

如果有很多类使用Update()函数,会导致性能浪费

如果函数统一在一个Update()中执行

代码



上述,需要将脚本挂载到物体上,

故新建了一个管理类MonoMgr



Mgr另外的作用

可以提供给外部添加协程的方法





代码汇总

MonoController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events; public class MonoController : MonoBehaviour
{
// Start is called before the first frame update
private event UnityAction updateEvent;
void Start()
{
DontDestroyOnLoad(this.gameObject);
} // Update is called once per frame
void Update()
{
if(updateEvent!=null){
updateEvent();
}
}
public void AddUpdateListener(UnityAction fun){
updateEvent += fun;
}
public void RemoveUpdateListener(UnityAction fun){
updateEvent -= fun;
}
}

MonoMgr

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class MonoMgr : Singleton<MonoMgr>
{
// Start is called before the first frame update
private MonoController controller;
public MonoMgr()
{
GameObject obj = new GameObject("MonoController");
controller = obj.AddComponent<MonoController>();
} // Update is called once per frame
public void AddUpdateListener(UnityAction fun){
controller.AddUpdateListener(fun);
}
public void RemoveUpdateListener(UnityAction fun){
controller.RemoveUpdateListener(fun);
}
public void AddUpdateListener(UnityAction fun){
controller.AddUpdateListener(fun);
}
public void RemoveUpdateListener(UnityAction fun){
controller.RemoveUpdateListener(fun);
}
public Coroutine StartCoroutine(IEnumerator routine){
return controller.StartCoroutine(routine);
}
public Coroutine StartCoroutine(string methodName){
return controller.StartCoroutine(methodName);
}
public void StopCoroutine(IEnumerator routine){
controller.StopCoroutine(routine);
} }

用法示范

  1. 新建一个继承至MonoBehaviour的类

  2. 在新建一个需要执行的类

  3. 将第一个类挂载到物体上,即可。

最新文章

  1. FFT 快速傅里叶变换浅析
  2. 页面位置 top、postop、scrolltop、offsetTop、scrollHeight、offsetHeight、clientHe
  3. ruby发送邮件方法
  4. linq 之 Distinct的使用
  5. Eclipse编译去除svn文件夹
  6. Address localhost:1099 is already in use 的错误
  7. 做SSH练习的时候,JUunit运行遇到报错信息
  8. Careerup上的简历模板
  9. Android journey3 @点击事件的4种写法
  10. SQLite数据库简介(转)
  11. css盒子模型、文档流、相对与绝对定位、浮动与清除模型
  12. 构建高可用web站点学习--前言
  13. eclipse的SVN插件的配置
  14. 使用freemarker模板生成word文档
  15. 201621123068 Week02-Java基本语法与类库
  16. 使用可变对象作为python函数默认参数引发的问题
  17. Joone
  18. MySQL查看SQL语句执行效率
  19. python反向生成数据库模型类
  20. python 缺少module

热门文章

  1. Linux中一切皆文件
  2. 2_稳定性分析_极点_Stability
  3. 浏览器视图层级中的“根”:&lt;html&gt;和&lt;body&gt;的属性研究
  4. CCF201409-3 字符串匹配
  5. SpringMVC的数据响应方式-页面跳转
  6. python的for循环基本用法
  7. Java开发中关于资源路径获取问题
  8. PHP入门-Window 下利用Nginx+PHP 搭建环境
  9. EF Core忽略某个属性保存
  10. [DEBUG] QAT Nginx for docker 部署时&quot;--with-ld-opt&quot;出错