ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Unity EventSystem之美

2019-07-28 21:44:27  阅读:221  来源: 互联网

标签:ExecuteEvents EventSystem 物体 之美 IEventSystemHandler Unity EventSystems Message p


Unity EventSystem

  • Message System
  • Input Modules
  • Supported Events
  • Raycasters

1. Message System(改进的消息系统)

基本上可以看成是以前SendMessage的升级版。

使用方法(照抄官网):

step1. 声明一个接口,继承自IEventSystemHandler

public interface ICustomMessageTarget : IEventSystemHandler
{
    // functions that can be called via the messaging system
    void Message1();
    void Message2();
}

step2. 实现这个接口 , 把这个脚本挂在某个物体上,这里假设为物体AAA

public class CustomMessageTarget : MonoBehaviour, ICustomMessageTarget
{
    public void Message1()
    {
        Debug.Log ("Message 1 received");
    }

    public void Message2()
    {
        Debug.Log ("Message 2 received");
    }
}

step3. 在任何脚本中使用ExecuteEvents静态类发送Message,来执行接口中定义的方法

//target should be AAA
ExecuteEvents.Execute<ICustomMessageTarget>(target, null, (x,y)=>x.Message1());

注意 : step3里的Excute泛型方法,有3个参数,第一个参数是发送message的gameobject对象,只有当对象上有IEventSystemHandler实现类的时候才可以,这个例子中自然就是AAA物体。
还要注意 : ExecuteEvents静态类还有其他方法:

## Static Functions

EventSystems.ExecuteEvents.CanHandleEvent    Can the given GameObject handle the IEventSystemHandler of type T. 
EventSystems.ExecuteEvents.Execute     Execute the event of type T : IEventSystemHandler on GameObject.
EventSystems.ExecuteEvents.ExecuteHierarchy  Recurse up the hierarchy calling Execute<T> until there is a GameObject that can handle the event. 
EventSystems.ExecuteEvents.GetEventHandler  Traverse the object hierarchy starting at root, and return the GameObject which implements the event handler of type <T> 
EventSystems.ExecuteEvents.ValidateEventData  Attempt to convert the data to type T. If conversion fails an ArgumentException is thrown.

名字解释都比较直白,就不翻译了。比如那个EventSystems.ExecuteEvents.ExecuteHierarchy, 是递归寻找适合的gameobject,并执行方法。

说实话,比以前的SendMessage科学了不少,以前只能在Hierarchy里上下求索,现在是有目的的寻找了。
但....我看来也就仅此而已了,SendMessage我在实际工程中从来都没用过,这个估计也不会用。为什么?有了System.Action谁还用这个...

2. Input Modules

此部分略,大致就是unity支持所有的输入方式,包括键盘啦,手柄啦,触摸啦等等,balabala...

3. Supported Events(支持的输入事件)

这部分就比较重要了,unity事件系统支持以下17种输入事件

事件接口 含义
IPointerEnterHandler pointer进入
IPointerExitHandler pointer离开
IPointerDownHandler pointer按下
IPointerUpHandler pointer抬起
IPointerClickHandler pointer按下和抬起
IInitializePotentialDragHandler 可拖拽物体被发现,可用来初始化一些变量
IBeginDragHandler 开始拖拽
IDragHandler 拖拽中
IEndDragHandler 拖拽结束时 (when)
IDropHandler 拖拽结束位置(where)
IScrollHandler 鼠标滚轮
IUpdateSelectedHandler 选中物体时,持续发送
ISelectHandler 物体变为被选择
IDeselectHandler 物体变为取消选择
IMoveHandler 物体移动(左右上下等)
ISubmitHandler submit(提交)按钮按下
ICancelHandler cancel(取消)按钮按下

注意: 这里的“pointer”可以是鼠标、touch等一切unity支持的类型

那也就意味着,我们终于可以在PC和移动端共用一套代码了

4. Raycasters(射线们)

这也是另外一个重要的点:决定了unity对何种输入方式进行响应:

射线类型 含义
Graphic Raycaster UI使用
Physics 2D Raycaster 2D 物体组件使用,如 BoxCollider2D等
Physics Raycaster 3D物体使用(UI其实也能使用)




链接:https://www.jianshu.com/p/229d9abc7bd9
 

标签:ExecuteEvents,EventSystem,物体,之美,IEventSystemHandler,Unity,EventSystems,Message,p
来源: https://blog.csdn.net/weixin_44350205/article/details/97619811

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有