Unity EventSystem之美

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种输入事件

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

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

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

4. Raycasters(射线们)

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

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




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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值