事件/委托机制(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应用UI设计流程
Android应用UI设计流程 设计原理 1.在移动设计中,使用环境是最关键的因素.原型设计方法必须考虑尺寸因素 2.用户测试必须涵盖运动.声音和多点触控等方面: 进行移动设计和测试时,请将你知道的有 ...
- Android开发基础规范(二)
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52614696 前言:Androi ...
- 利用CocoaHTTPServer实现wifi局域网传输文件到iphone
背景 近日在做一个代码阅读器,其中涉及到代码文件的上传,之前看到过许多app支持局域网传文件,因此就通过查询和研究实现了此功能,我是用的框架是CocoaHTTPServer. 原理 CocoaHTTP ...
- Effective C++ ——实现
条款26:尽可能延后变量定义式的出现时间 当你定义一个变量的时候就要保证这个变量能够在程序中使用到,不要定义无意义的变量,这样就要求我们最好是在变量使用到的时候才做定义,因为如果一个变量定义了却不使用 ...
- ubuntu安装水星MW150US无线网卡8188eu驱动
买了一个无线网卡插在ubuntu系统的电脑上,却不能识别出来.lsusb,可以看到下面的结果: Bus 002 Device 002: ID 0bda:8179 Realtek Semiconduct ...
- 03安卓TextView
一 TextView 父类 : View >概念:文本控件 :文本内容的显示 默认配置不可编辑 子类EditText可以编辑 *********************** ...
- was unable to start within 45 seconds. If the server requires more time, try increasing the timeout
在eclipse启动tomcat时遇到超时45秒的问题: Server Tomcat v7.0 Server at localhost was unable to startwithin 45 sec ...
- android 网络通信框架volly
1. 什么是Volley 在这之前,我们在程序中需要和网络通信的时候,大体使用的东西莫过于AsyncTaskLoader,HttpURLConnection,AsyncTask,HTTPClient( ...
- Java进阶(三十) 判断字符串编码类型
java 判断字符串编码类型 public static String getEncoding(String str) { String encode = "GB2312"; tr ...
- 【翻译】如何在Ext JS 6中使用Fashion美化应用程序
原文:How to Style Apps with Fashion in Ext JS 6 在Ext JS 6,一个最大的改变就是框架合并,使用一个单一的代码库,就可以为每一种设备开发各具有良好体验的 ...