先来看下效果图,图中点击 Cube(EventDispatcher),Sphere(EventListener)以及 Capsule(EventListener)会做出相应的变化,例子中的对象相互之间没有引用,也没有父子关系。

Demo 事件触发者(EventDispatcher)CubeObject.cs,挂载在 Cube 对象上

using UnityEngine;
using System.Collections; public class CubeObject : MonoBehaviour
{
void Update()
{
if (Input.GetMouseButtonDown ())
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit raycastHit = new RaycastHit();
if(Physics.Raycast(ray, out raycastHit))
{
if(raycastHit.collider.gameObject.name == "Cube")
{
// 触发事件
ObjectEventDispatcher.dispatcher.dispatchEvent(new UEvent(EventTypeName.CUBE_CLICK, "cube"), this);
}
}
}
}
}

Demo 事件侦听者(EventListener)CapsuleObject.cs,挂载在 Capsule 对象上

using UnityEngine;
using System.Collections; public class CapsuleObject : MonoBehaviour
{
private float angle;
private float targetAngle;
private float currentVelocity; void Awake()
{
// 添加事件侦听
ObjectEventDispatcher.dispatcher.addEventListener (EventTypeName.CUBE_CLICK, OnClickHandler);
} /// <summary>
/// 事件回调函数
/// </summary>
/// <param name="uEvent">U event.</param>
private void OnClickHandler(UEvent uEvent)
{
this.targetAngle = this.targetAngle == 90f ? 0f : 90f; this.StopCoroutine (this.RotationOperater ());
this.StartCoroutine (this.RotationOperater());
} IEnumerator RotationOperater()
{
while (this.angle != this.targetAngle)
{
this.angle = Mathf.SmoothDampAngle (this.angle, this.targetAngle, ref currentVelocity, 0.5f);
this.transform.rotation = Quaternion.AngleAxis(this.angle, Vector3.forward); if(Mathf.Abs(this.angle - this.targetAngle) <= ) this.angle = this.targetAngle; yield return null;
}
}
}

Demo 事件侦听者(EventListener)SphereObject.cs,挂载在 Sphere 对象上

using UnityEngine;
using System.Collections; public class SphereObject : MonoBehaviour
{
private float position;
private float targetPosition;
private float currentVelocity; void Awake()
{
// 添加事件侦听
ObjectEventDispatcher.dispatcher.addEventListener (EventTypeName.CUBE_CLICK, OnClickHandler);
} /// <summary>
/// 事件回调函数
/// </summary>
/// <param name="uEvent">U event.</param>
private void OnClickHandler(UEvent uEvent)
{
this.targetPosition = this.targetPosition == 2f ? -2f : 2f; this.StopCoroutine (this.PositionOperater ());
this.StartCoroutine (this.PositionOperater());
} IEnumerator PositionOperater()
{
while (this.position != this.targetPosition)
{
this.position = Mathf.SmoothDamp (this.position, this.targetPosition, ref currentVelocity, 0.5f);
this.transform.localPosition = new Vector3(this.transform.localPosition.x, this.position, this.transform.localPosition.z); if(Mathf.Abs(this.position - this.targetPosition) <= 0.1f) this.position = this.targetPosition; yield return null;
}
}
}

Demo 辅助类 EventTypeName.cs

using UnityEngine;
using System.Collections; public class EventTypeName
{
public const string CUBE_CLICK = "cube_click";
}

Demo 辅助类 ObjectEventDispatcher.cs

using UnityEngine;
using System.Collections; public class ObjectEventDispatcher
{
public static readonly UEventDispatcher dispatcher = new UEventDispatcher();
}

事件触发器 UEventDispatcher.cs

using System;
using System.Collections.Generic; public class UEventDispatcher
{
protected Dictionary<string, UEventListener> eventListenerDict; public UEventDispatcher()
{
this.eventListenerDict = new Dictionary<string, UEventListener>();
} /// <summary>
/// 侦听事件
/// </summary>
/// <param name="eventType">事件类别</param>
/// <param name="callback">回调函数</param>
public void addEventListener(string eventType, UEventListener.EventListenerDelegate callback)
{
if (!this.eventListenerDict.ContainsKey(eventType))
{
this.eventListenerDict.Add(eventType, new UEventListener());
}
this.eventListenerDict[eventType].OnEvent += callback;
} /// <summary>
/// 移除事件
/// </summary>
/// <param name="eventType">事件类别</param>
/// <param name="callback">回调函数</param>
public void removeEventListener(string eventType, UEventListener.EventListenerDelegate callback)
{
if (this.eventListenerDict.ContainsKey(eventType))
{
this.eventListenerDict[eventType].OnEvent -= callback;
}
} /// <summary>
/// 发送事件
/// </summary>
/// <param name="evt">Evt.</param>
/// <param name="gameObject">Game object.</param>
public void dispatchEvent(UEvent evt, object gameObject)
{
UEventListener eventListener = eventListenerDict[evt.eventType];
if (eventListener == null) return; evt.target = gameObject;
eventListener.Excute(evt);
} /// <summary>
/// 是否存在事件
/// </summary>
/// <returns><c>true</c>, if listener was hased, <c>false</c> otherwise.</returns>
/// <param name="eventType">Event type.</param>
public bool hasListener(string eventType)
{
return this.eventListenerDict.ContainsKey(eventType);
}
}

事件侦听器 UEventListener.cs

using System;

public class UEventListener
{
public UEventListener() { } public delegate void EventListenerDelegate(UEvent evt);
public event EventListenerDelegate OnEvent; public void Excute(UEvent evt)
{
if (OnEvent != null)
{
this.OnEvent(evt);
}
}
}

事件参数 UEvent.cs

using System;

public class UEvent
{
/// <summary>
/// 事件类别
/// </summary>
public string eventType; /// <summary>
/// 参数
/// </summary>
public object eventParams; /// <summary>
/// 事件抛出者
/// </summary>
public object target; public UEvent(string eventType, object eventParams = null)
{
this.eventType = eventType;
this.eventParams = eventParams;
}
}

最新文章

  1. .Net 异步方法加上“timeout”
  2. JavaScript高级程序设计之作用域链
  3. 九度OJ1486 /POJ 1029/2012北京大学研究生复试上机
  4. 【Nginx】启动报错-端口被占用
  5. git在开发中的一些使用
  6. org.w3c.dom.Node.getTextContent()方法编译错误-已解决
  7. 在思科模拟器上配置AAA认证
  8. 9款国内外垂直领域的在线作图工具:那些可以替代Visio的应用!【转】
  9. springboot2.x接口返回中文乱码
  10. 用doxygen自动生成文档
  11. C# sqlhelper 整理
  12. 红外光通信装置数字部分思路点睛 2013年国赛f题
  13. R语言实战(六)重抽样与自助法
  14. java 多线程11:volatile关键字
  15. 如今在 Internet 上流传的“真正”的程序员据说是这样的
  16. python学习笔记(二十八)日志模块
  17. 关于hashmap的排序
  18. 删除文件的第一列 -Linux
  19. lightoj 1282 &amp;&amp; uva 11029
  20. HDU1081:To The Max(最大子矩阵,线性DP)

热门文章

  1. (二)SQL Server分区创建过程
  2. CSS高效开发实战:CSS 3、LESS、SASS、Bootstrap、Foundation --读书笔记(3)线性渐变
  3. python解析git log后生成页面显示git更新日志信息
  4. ASP.NET Web API学习 (一)
  5. JavaScript——理解闭包及作用
  6. Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause
  7. ng-bind 与ng-model区别
  8. Spring Security控制权限
  9. .NET 委托
  10. 各种浏览器的Hack写法(chrome firefox ie等)