<划重点的Unity2017>事件系统

(1)事件系统概述
1.Overview 概述

the Event System itself is designed as a manager and facilitator of communication between Event System modules.
事件系统本身是作为两个事件系统模块的管理者。

The primary roles of the Event System are as follows:
基础的事件系统角色大致如下:
Manage which GameObject is considered selected:管理正在点击的物体。
Manage which Input Module is in use:管理正在使用的物体。
Manage Raycasting (if required):管理光线碰撞。
Updating all Input Modules as required:刷新所有的输入模块。

2.Input Modules 输入模块

An input module is where the main logic of how you want the Event System to behave lives, they are used for:
一个输入模块就是你如何让主要逻辑表现的。
Handling Input:手动输入。
Managing event state:管理事件状态。
Sending events to scene objects:向场景当中的物体传递事件。
Only one Input Module can be active in the Event System at a time, and they must be components on the same GameObject as the Event System component.
同一时间只有一种输入模块可以被激活。

3.Raycasters 光线碰撞

Raycasters are used for sending figuring out what the pointer is over, it is common for Input Modules to use the Raycasters configured in the scene to calculate what the pointing device is over.
光线碰撞用于传递指针。输入设备在场景当中使用光线指针配置来计算点设备是否结束。

There are 3 provided Raycasters that exist by default:
三种光线碰撞形式:
Graphic Raycaster - Used for UI elements:用于UI的图形碰撞。
Physics 2D Raycaster - Used for 2D physics elements:2D游戏用到的光线碰撞。
Physics Raycaster - Used for 3D physics elements:物理碰撞。

(2)新的事件传递系统
由于原来的事件传递系统比较辣鸡,Unity已经推出了新的事件系统。

The messaging system is generic and designed for use not just by the UI system but also by general game code. It is relatively trivial to add custom messaging events and they will work using the same framework that the UI system uses for all event handling.
事件系统不仅仅可以用于UI系统,但是也可以用于正常的游戏代码。自定义事件传递和UI系统用的是一套框架。UI系统也可以处理所有的事件。

In the UnityEngine.EventSystems namespace there is a base interface called ‘IEventSystemHandler’. Anything that extends from this can be considered as a target for receiving events via the messaging system.
注意使用新的事件系统,事件系统是基于一个叫IEventSystemHandler的接口运行的。任何从该接口继承的物体,都可以通过事件系统接收事件。

public interface ICustomMessageTarget : IEventSystemHandler
{
    // functions that can be called via the messaging system
    //以下的事件可以直接通过message系统调用
    void Message1();
    void Message2();
}
//继承自IEventSystemHandler方式

On this interface is defined then it can be implemented by a MonoBehaviour. When implemented it defines the functions that will be executed if the given message is issued against this MonoBehaviours GameObject.
一旦把声明的函数被定义,那么当这个物体受到消息的时候,就会处理这个函数。

//注意其继承的步骤
public class CustomMessageTarget : MonoBehaviour, ICustomMessageTarget
{
    public void Message1()
    {
        Debug.Log ("Message 1 received");
    }

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

Now that a script exists that can receive the message we need to issue the message. Normally this would be in response to some loosely coupled event that occurs. For example, in the UI system we issue events for such things as PointerEnter and PointerExit, as well as a variety of other things that can happen in response to user input into the application.
现在脚本已经存在,并且可以接收我们发送的信息了。通常这将会对一些低耦合的事件响应。举个例子,在UI系统处理指针的进入和离开。

To send a message a static helper class exists to do this. As arguments it requires a target object for the message, some user specific data, and a functor that maps to the specific function in the message interface you wish to target.
为了发送消息,一个静态的协助类可以胜任这项任务。作为参数的集合,它需要一个目标对象来发送消息。一些用户特定的数据和一些映射到想要发送给目标的特殊函数的函子。

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

This code will execute the function Message1 on any components on the GameObject target that implement the ICustomMessageTarget interface.
调用该段代码处理对应目标当中的函数。

(3)一些有意思的组件
当然这些不是全部,只是我认为她有意思。没有把一些无聊的组件放上来。

I.Event System Manager
This subsystem is responsible for controlling all the other elements that make up eventing. It coordinates which Input Module is currently active, which GameObject is currently considered ‘selected’, and a host of other high level Event System concepts.
这个子系统对构成事件的其他元素的控制负责。她协调拿一个输入模块正在激活,哪一个物体正在被选择。以及一些高级事件系统概念。

Each ‘Update’ the Event System receives the call, looks through its Input Modules and figures out which is the Input Module that should be used for this tick. It then delegates the processing to the modules.
该组件的每一次Update,他都会进行监听。并且将处理其委托给模块。

First Selected:The GameObject that was selected first.
被选中的第一个Object。
Send Navigation Events:Should the EventSystem allow navigation events (move / submit / cancel).
发送循径事件:事件系统是否允许寻路事件。
Drag Threshold The soft area for dragging in pixels.
Beneath the Properies table is the “Add Default Input Modules” button.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值