事件/委托机制(event/delegate)(Unity3D开发之十七)
猴子原创,欢迎转载。转载请注明: 转载自Cocos2Der-CSDN,谢谢!
原文地址: http://blog.csdn.net/cocos2der/article/details/46539433
Delegate作用我就不多说了,Unity中可以直接使用EventHandler实现事件委托,咱们直接事例吧。
一、场景物体移动结束后事件监听
假如PlayerControl,移动结束后触发MoveComplete事件。
using UnityEngine;
using System.Collections;
using System;
public class PlayerControl : MonoBehaviour {
public event EventHandler MoveComplete;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonUp(0)) {
// Test logic for PlayerMoveComplete
PlayerMoveComplete();
}
}
void PlayerMoveComplete()
{
if (MoveComplete != null) {
MoveComplete(this, EventArgs.Empty);
}
}
}
事件接收处理
using UnityEngine;
using System.Collections;
using System;
public class GameManager : MonoBehaviour {
public static GameManager Instance;
public PlayerControl playerControl;
void Awake ()
{
// check there isn't more than one instance of the GameManager in the scene
if(Instance != null){
Debug.LogError("More than one GameManager found in the scene");
return;
}
// set the global instance
Instance = this;
}
// Use this for initialization
void Start () {
playerControl.MoveComplete += HandleMoveComplete;
}
void HandleMoveComplete (object sender, EventArgs e)
{
Debug.Log("MoveComplete:");
}
// Update is called once per frame
void Update () {
}
}
这里由于使用的EventHandler实现,所以在事件中无法传递自定义参数。
二、自定义Event,事件中传递自定义参数
1、自定义EventArgs
using UnityEngine;
using System.Collections;
using System;
public class PlayerMoveEventArgs : EventArgs {
private string message;
public PlayerMoveEventArgs(string message)
{
this.message = message;
}
public string Message
{
get{return message;}
}
}
public delegate void MoveCompleteHandle(object sender, PlayerMoveEventArgs e);
那么我们修改下PlayerControl
using UnityEngine;
using System.Collections;
using System;
public class PlayerControl : MonoBehaviour {
public event EventHandler MoveComplete;
public event MoveCompleteHandle CustomMoveComplete;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonUp(0)) {
// Test logic for PlayerMoveComplete
PlayerMoveComplete();
}
}
void PlayerMoveComplete()
{
if (MoveComplete != null) {
MoveComplete(this, EventArgs.Empty);
}
if (CustomMoveComplete != null) {
CustomMoveComplete(this, new PlayerMoveEventArgs("Move:" + this.name));
}
}
}
处理事件的我们也修改下
using UnityEngine;
using System.Collections;
using System;
public class GameManager : MonoBehaviour {
public static GameManager Instance;
public PlayerControl playerControl;
void Awake ()
{
// check there isn't more than one instance of the GameManager in the scene
if(Instance != null){
Debug.LogError("More than one GameManager found in the scene");
return;
}
// set the global instance
Instance = this;
}
// Use this for initialization
void Start () {
playerControl.MoveComplete += HandleMoveComplete;
playerControl.CustomMoveComplete += HandleCustomMoveComplete;
}
void HandleCustomMoveComplete (object sender, PlayerMoveEventArgs e)
{
Debug.Log("HandleCustomMoveComplete:" + e.Message);
}
void HandleMoveComplete (object sender, EventArgs e)
{
Debug.Log("MoveComplete:");
}
// Update is called once per frame
void Update () {
}
}
ok,现在你可以自由的玩耍了。
事件/委托机制(event/delegate)(Unity3D开发之十七)的更多相关文章
- 【Unity3D技巧】在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信 (二) : 引入中间层NotificationCenter
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 一对多的观察者模式机制有什么缺点? 想要查看 ...
- C++模拟C#事件委托机制(一)
原文来自于http://www.cnblogs.com/netssfy/articles/1652671.html 写了一段时间的C#代码后确实发现C#的事件委托非常好用.于是便想是否在C++中也能如 ...
- Javascript事件模型系列(二)事件的捕获-冒泡机制及事件委托机制
一.事件的捕获与冒泡 由W3C规定的DOM2标准中,一次事件的完整过程包括三步:捕获→执行目标元素的监听函数→冒泡,在捕获和冒泡阶段,会依次检查途径的每个节点,如果该节点注册了相应的监听函数,则执行监 ...
- JavaScript 之默认行为 DOM2级,事件委托机制
1. 事件默认行为及阻止方式 1.1 浏览器的默认行为 JavaScript事件本身所具有的属性,例如a标签的跳转,Submit按钮的提交,右键菜单,文本框的输入等. 1.2 ...
- 一篇文章图文并茂地带你轻松学完 JavaScript 事件循环机制(event loop)
JavaScript 事件循环机制 (event loop) 本篇文章已经默认你有了基础的 ES6 和 javascript语法 知识. 本篇文章比较细致,如果已经对同步异步,单线程等概念比较熟悉的读 ...
- 转载: jQuery事件委托( bind() \ live() \ delegate()) [委托 和 绑定的故事]
转载:http://blog.csdn.net/zc2087/article/details/7287429 随着DOM结构的复杂化和Ajax等动态脚本技术的运用,事件委托自然浮出了水面.jQuery ...
- javascript事件委托机制详解
以个人前端工作面试经历来看,javascript事件委托是问的最多的一类题目之一,熟悉事件委托能够了解你对于javascript的掌握程度. 面试官可能问一下问题,现在有5个li待办事件,需要实现当点 ...
- JS与Jquery的事件委托机制
传送:http://www.ituring.com.cn/article/467 概念: 什么是事件委托:通俗的讲,事件就是onclick,onmouseover,onmouseout,等就是事件,委 ...
- js的事件委托机制
如今的JavaScript技术界里最火热的一项技术应该是‘事件委托(event delegation)’了.使用事件委托技术能让你避免对特定的每个节点添加事件监听器:相反,事件监听器是被添加到它们的父 ...
随机推荐
- Android之触摸手势检测GestureDetector使用详解
在Android中,当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing,onSingleTapConfirmed(单击),onDoubleTap(双击)等等. 一般情 ...
- Android更新UI的几种方法
在Android开发过程中,常需要更新界面的UI.比如网络请求操作.一些耗时操作都不能放在UI线程中运行的,需要放在子线程,而子线程又不能更新UI界面,这是我们需要引入一个Handler,消息处理机制 ...
- Hadoop:Hadoop单机伪分布式的安装和配置
http://blog.csdn.net/pipisorry/article/details/51623195 因为lz的linux系统已经安装好了很多开发环境,可能下面的步骤有遗漏. 之前是在doc ...
- Android之Notification-android学习之旅(二)
notification常用于下拉式的消息推送. Notification的构成 Nitification的实例 1.新建一个Builder,要选Notification.compat包. 2.然后用 ...
- 调用MediaScannerConnection 发生内存泄露的解决方法
调用MediaScannerConnection发起扫描时经常会发生内存泄露,例如: E ActivityThread: Activity FolderListActivity has leaked ...
- Android的TabHost组件-android的学习之旅(四十)
TabHost简介 虽然,官方建议用Fagment取代TabHost,但是我们还是大概的介绍一下.TabHost是一种非常简单的组件,TabHost可以很方便的在窗口放置多个标签页,每一个标签页相当于 ...
- Linux下多线程编程遇到的一些问题
今天在学习了Linux的多线程编程的基础的知识点.于是就试着做了一个简单的Demo.本以为会得到预期的结果.不成想却遇到了意想不到的问题. 代码展示 我的C 代码很简单,就是一个简单的示例程序,如下: ...
- MySQL数据库内置函数
mysql数据库中提供了很丰富的函数.mysql函数包括数学函数.字符串函数.日期和时间函数.条件判断函数.系统信息函数.加密函数.格式化函数等.通过这些函数,可以简化用户的操作. 简单介绍几类函数的 ...
- K-均值聚类算法(K-means)
K-means是一种无监督的学习,将相似的对象归到同一个簇中.可以将一批数据分为K个不同的簇,并且每个簇的中心采用簇中所含样本的均值计算而成. K-means算法的K值需要由用户指定, ...
- 《java入门第一季》之tcp协议下的网络编程
tcp协议相对于udp更加安全. 首先看一下需求:服务器端开启,多个客户端同时向服务器发送数据,看哪个客户端先到达. 说明:这里我开启三个电脑实验,一台电脑写服务器端的程序,两台电脑开客户端的程序.服 ...