Common(五)—— 消息系统(二):如何使用
来源:互联网 发布:什么是淘宝关键词 编辑:程序博客网 时间:2024/06/08 20:01
这一篇来看看怎么用这个EventCenter
首先是一个测试脚本,把它挂到一个空对象上面去即可:
using System.Collections;using System.Collections.Generic;using UnityEngine;public class TestEventScript : MonoBehaviour { // Use this for initialization void Start () { //这里先添加监听 EventCenter.AddListener (EGameEvent.eGameEvent_UpLv, Test); } // Update is called once per frame void Update () { } void OnDestroy() { //有添加就肯定有销毁 EventCenter.RemoveListener (EGameEvent.eGameEvent_UpLv, Test); } public void Test () { Debug.Log ("没有参数的监听!!!"); }}
然后再场景中创建一个button(button总会用吧),绑定它的点击事件:
using System.Collections;using System.Collections.Generic;using UnityEngine;public class ButtonClick : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void ClickButton() { //事件分发 EventCenter.Broadcast (EGameEvent.eGameEvent_UpLv); }}
运行结果:
接下来是传递一个参数的用法:
//这里先添加监听EventCenter.AddListener<string> (EGameEvent.eGameEvent_UpLv, Test<string>);//有添加就肯定有销毁EventCenter.RemoveListener<string> (EGameEvent.eGameEvent_UpLv, Test<string>);public void Test<T> (T str1){ Debug.Log ("str1: " + str1);}public void ClickButton(){ //事件分发 EventCenter.Broadcast<string> (EGameEvent.eGameEvent_UpLv, str1);}
运行结果:
接下来是传递两个参数的方法:
//这里先添加监听EventCenter.AddListener<string, int> (EGameEvent.eGameEvent_UpLv, Test<string, int>);//有添加就肯定有销毁EventCenter.RemoveListener<string, int> (EGameEvent.eGameEvent_UpLv, Test<string, int>);public void Test<T, U> (T str1, U integer2){ Debug.Log ("str1: " + str1 + " " + "integer2:" + integer2.ToString());}public void ClickButton(){ //事件分发 EventCenter.Broadcast<string, int> (EGameEvent.eGameEvent_UpLv, str1, integer2);}
运行结果:
接下来三个参数,四个参数应该也没问题了。
阅读全文
0 0
- Common(五)—— 消息系统(二):如何使用
- Common(五)—— 消息系统(一):简单的EventCenter
- Common(二)—— CMemQueue
- 初始消息中间件(分布式系统五)
- 如何设计一门语言(五)——面向对象和消息发送
- Rabbit五种消息队列学习(二) -- 简单队列
- ActiveMQ系统之——消息持久化到MySQL数据库中(二)
- Smobiler基于.NET框架开发移动应用内部系统—消息列表功能(开发日志五)
- EJB——消息和消息驱动bean(二)
- 【消息队列】MSMQ(二)——消息处理流程
- Java使用Log日志系统(common-logging和log4j)
- Java使用Log日志系统(common-logging和log4j)
- Java使用Log日志系统(common-logging和log4j)
- 分布式消息中间件(五)——MQ基础详解
- ActiveMQ(五):多个系统间消息的使用
- 基于Python语言使用RabbitMQ消息队列(五)
- 【ROS学习】(五)ROS消息传递——自定义消息
- runtime从入门到精通(五)—— 消息发送和消息转发
- jdk动态代理源码解析
- Java 使用Axis实现WebService实例
- JAVA 桌面程序的外观风格+主题场景 (更新)
- 【Angular2】组件交互异步问题
- 数据结构与算法、Java多线程、Java集合(包括JUC)系统学习推荐博客
- Common(五)—— 消息系统(二):如何使用
- 基于Xmpp,Openfire和Asmack的聊天软件的开发(一)
- 结构体定义,初始化和赋值
- OpenGL中的glutInitDisplayMode()函数的理解
- Android基础——3:项目结构详解
- TensorFlow实现卷积神经网络CNN
- Fiddler 安装使用教程
- 《数据库系统概述》笔记(一)
- openstack 快照分析