unity---公共模块MonoController
2024-09-05 01:13:30
公共模块
如果有很多类使用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);
}
}
用法示范
- 新建一个继承至MonoBehaviour的类
- 在新建一个需要执行的类
- 将第一个类挂载到物体上,即可。
最新文章
- FFT 快速傅里叶变换浅析
- 页面位置 top、postop、scrolltop、offsetTop、scrollHeight、offsetHeight、clientHe
- ruby发送邮件方法
- linq 之 Distinct的使用
- Eclipse编译去除svn文件夹
- Address localhost:1099 is already in use 的错误
- 做SSH练习的时候,JUunit运行遇到报错信息
- Careerup上的简历模板
- Android journey3 @点击事件的4种写法
- SQLite数据库简介(转)
- css盒子模型、文档流、相对与绝对定位、浮动与清除模型
- 构建高可用web站点学习--前言
- eclipse的SVN插件的配置
- 使用freemarker模板生成word文档
- 201621123068 Week02-Java基本语法与类库
- 使用可变对象作为python函数默认参数引发的问题
- Joone
- MySQL查看SQL语句执行效率
- python反向生成数据库模型类
- python 缺少module
热门文章
- Linux中一切皆文件
- 2_稳定性分析_极点_Stability
- 浏览器视图层级中的“根”:<;html>;和<;body>;的属性研究
- CCF201409-3 字符串匹配
- SpringMVC的数据响应方式-页面跳转
- python的for循环基本用法
- Java开发中关于资源路径获取问题
- PHP入门-Window 下利用Nginx+PHP 搭建环境
- EF Core忽略某个属性保存
- [DEBUG] QAT Nginx for docker 部署时";--with-ld-opt";出错