猴子原创,欢迎转载。转载请注明: 转载自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开发之十七)的更多相关文章

  1. 【Unity3D技巧】在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信 (二) : 引入中间层NotificationCenter

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 一对多的观察者模式机制有什么缺点? 想要查看 ...

  2. C++模拟C#事件委托机制(一)

    原文来自于http://www.cnblogs.com/netssfy/articles/1652671.html 写了一段时间的C#代码后确实发现C#的事件委托非常好用.于是便想是否在C++中也能如 ...

  3. Javascript事件模型系列(二)事件的捕获-冒泡机制及事件委托机制

    一.事件的捕获与冒泡 由W3C规定的DOM2标准中,一次事件的完整过程包括三步:捕获→执行目标元素的监听函数→冒泡,在捕获和冒泡阶段,会依次检查途径的每个节点,如果该节点注册了相应的监听函数,则执行监 ...

  4. JavaScript 之默认行为 DOM2级,事件委托机制

    1. 事件默认行为及阻止方式    1.1 浏览器的默认行为       JavaScript事件本身所具有的属性,例如a标签的跳转,Submit按钮的提交,右键菜单,文本框的输入等.    1.2 ...

  5. 一篇文章图文并茂地带你轻松学完 JavaScript 事件循环机制(event loop)

    JavaScript 事件循环机制 (event loop) 本篇文章已经默认你有了基础的 ES6 和 javascript语法 知识. 本篇文章比较细致,如果已经对同步异步,单线程等概念比较熟悉的读 ...

  6. 转载: jQuery事件委托( bind() \ live() \ delegate()) [委托 和 绑定的故事]

    转载:http://blog.csdn.net/zc2087/article/details/7287429 随着DOM结构的复杂化和Ajax等动态脚本技术的运用,事件委托自然浮出了水面.jQuery ...

  7. javascript事件委托机制详解

    以个人前端工作面试经历来看,javascript事件委托是问的最多的一类题目之一,熟悉事件委托能够了解你对于javascript的掌握程度. 面试官可能问一下问题,现在有5个li待办事件,需要实现当点 ...

  8. JS与Jquery的事件委托机制

    传送:http://www.ituring.com.cn/article/467 概念: 什么是事件委托:通俗的讲,事件就是onclick,onmouseover,onmouseout,等就是事件,委 ...

  9. js的事件委托机制

    如今的JavaScript技术界里最火热的一项技术应该是‘事件委托(event delegation)’了.使用事件委托技术能让你避免对特定的每个节点添加事件监听器:相反,事件监听器是被添加到它们的父 ...

随机推荐

  1. IMDG产品功能扩展

    开源IMDG通常都提供了SPI或其他接口,供用户自行扩展.以Hazelcast为例,我们可以用一些好玩的小工具增强其查询.Map和后端持久化的功能.这些小工具虽然看起来很小,但功能也非常强大. SQL ...

  2. [Python]查看python路径以及安装包的路径

    特别是linux系统,装了多个python,有时候找不到python的绝对路径,有时候装了个django,又找不到django安装到哪里了..当然查看的方法有很多种,这里列出几种,供没有经验的人参考下 ...

  3. 并发计算模型BSP与SEDA

    1    BSP批量同步并行计算 BSP(Bulk Synchronous Parallel)批量同步并行计算用来解决并发编程难的问题.名字听起来有点矛盾,又是同步又是并行的.因为计算被分组成一个个超 ...

  4. Android简易实战教程--第二十四话《画画板》

    今天完成一个画画板. 首先来个布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...

  5. Java中循环声明变量方法

    Java循环声明变量 之前想这样做,但是网上一直搜索不到,下面是我的方式 项目中 // 得到需要查询外表的数量,然后分别创建缓存,插入数据多的时候如果编码在缓存里面,就不需要再去查询数据库了.key: ...

  6. android view事件分发机制

    首先我们先写个简单的例子来测试View的事件转发的流程~ 1.案例 为了更好的研究View的事件转发,我们自定以一个MyButton继承Button,然后把跟事件传播有关的方法进行复写,然后添加上日志 ...

  7. EBS HRMS数据表

    4.1. 人员基本息                  表            (PER_ALL_PEOPLE_F)                                          ...

  8. 保证service存活

    Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStart ...

  9. (SQL Server)有关T-SQL的10个好习惯

    转自 http://www.cnblogs.com/CareySon/archive/2012/10/11/2719598.html 1.在生产环境中不要出现Select * 这一点我想大家已经是比较 ...

  10. (NO.00004)iOS实现打砖块游戏(十四):3球道具的实现

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 反弹棒变化道具实现前面已经介绍过了,我们下面可以在小球上做些文章 ...