unity---公共模块MonoController
公共模块
如果有很多类使用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的类

- 在新建一个需要执行的类

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

unity---公共模块MonoController的更多相关文章
- Atitit 图像处理 公共模块 矩阵扫描器
Atitit 图像处理 公共模块 矩阵扫描器 1.1. 调用说明对矩阵像素遍历处理调用1 2. 矩阵扫描器主题结构1 2.1. 主要说明 从像素点开始填充矩阵1 2.2. 得到模板中心点所对应的图像坐 ...
- 谈谈CommonsChunkPlugin抽取公共模块
引言 webpack插件CommonsChunkPlugin的主要作用是抽取webpack项目入口chunk的公共部分,具体的用法就不做过多介绍,不太了解可以参考webpack官网介绍: 该插件是we ...
- Anatomy of a Database System学习笔记 - 公共模块、结语
公共模块 1. 使用基于上下文的内存分配器进行内存分配 除了教材里常提到的buffer pool,数据库还会为其他任务分配大量内存,例如,Selinger-style查询优化需要动态的规划查询:has ...
- Thinkphp5笔记六:公共模块common的使用
common模块属于公共模块,Thinkphp框架,默认就能调用. 实际用处:任何模块都可能用到的模型.控制.事件提取出来放到公共模块下. 一.公共事件 apps\common\common.php ...
- Vue设置导航栏为公共模块并在登录页不显示
1.公共模块的内容可以放在App.vue中但是通常登录页面是不需要导航的,那么就需要规避登录页这时,就可以采用keep-alive结合$route.meta来实现这个功能.keep-alive 是 V ...
- html引入公共模块
如果没有母版页,那么大量相同布局的页面会有很多相同的代码,那么这就提到了一个概念,叫重用性:可以将相同布局的代码放在一个单独的文件,里面写一些公共模块,那么在其他页面只需要在指定位置引入他们就可以了写 ...
- Ionic4.x 中自定义公共模块
1.创建公共模块以及组件 ionic g module module/slide ionic g component module/slide 2.公共模块 slide.module.ts 中暴露对应 ...
- Spring Cloud微服务(一):公共模块的搭建
本demo后台采用spring cloud微服务,前端选用vue,进行前后端分离搭建.具体项目见git:光头才能强 创建文件夹,并分别创建以下jar工程 创建公共模块(后续有需要,还会增加).无论是d ...
- Python_selenium PO模式下 Tesecase 的相同执行代码做成selenium_base_case公共模块及调用
作用: PO模式下 Tesecase 的相同执行代码做成selenium_base_case公共模块及调用,提高代码简洁度,实现同样效果. 框架结构: 代码简单实践: common模块下 seleni ...
随机推荐
- c语言中的字面量
在计算机科学中,字面量(literal)是用于表达源代码中一个固定值的表示法(notation). 几乎所有计算机编程语言都具有对基本值的字面量表示,诸如:整数.浮点数以及字符串: 而有很多也对布尔类 ...
- 制作任意波形发生器MAX038
- Linux网络配置:Nat和桥接模式详解
Linux网络配置:Nat和桥接模式详解 一.我们首先说一下VMware的几个虚拟设备: Centos虚拟网络编辑器中的虚拟交换机: VMnet0:用于虚拟桥接网络下的虚拟交换机: VMnet1:用于 ...
- HTML5中dialog元素尝鲜
对话框(别称模态框,浮层)是web项目中用于用户交互的重要部分,我们最常见的就是js中 alert(),confirm(),但是这个对话框的不美观,也不能自定义样式,所以在开发的过程中,一般根据自己自 ...
- jquery 常用选择器和方法以及遍历(超详细)
jQuery 常用选择器和和方法 学习总结 一.JQuery 介绍 1. 什么是 jQuery 2. jQuery 版本介绍 3. jQuery 文件介绍 二.jQuery 对象 1. jQuery ...
- nodejs制作爬虫程序
在nodejs中,可以通过不断对服务器进行请求,以及本身的fs =>filesystem 模块和clientRequest模块对网站的资源进行怕取,目前只做到了对图片的趴取!视频文件格式各异, ...
- Linux安装Redis步骤和make遇到的坑
Linux安装Redis服务步骤 1.获取redis资源 cd /usr/local wget https://mirrors.huaweicloud.com/redis/redis-6 ...
- Spring-Bean依赖注入(引用数据类型和集合数据类型)
为什么使用spring依赖注入详见–>依赖注入分析 1.创建实体类User类 package com.hao.domain; public class User { private String ...
- Apache Zookeeper 使用-安装
Apache Zookeeper 使用-安装 官方网站 https://zookeeper.apache.org/ 官方文档 下载地址 Zookeeper 是什么? ZooKeeper is a ce ...
- Codeforces Round #705 (Div. 2) B. Planet Lapituletti(镜像时钟) 思维
传送门: https://codeforces.com/contest/1493/problem/B 题目: Example input 5 24 60 12:21 24 60 23:59 90 8 ...




