Unity5,Java高级面试题

如果场景中有多个射线检测源:If multiple Raycasters are used then they will all have casting happen against them and the results will be sorted based on distance to the elements.

四、响应事件

1、输入模块可以检测到的事件

StandaloneInputModule和TouchInputModule两个组件会检测一些输入操作,以事件的方式(message系统)通知目标对象,那么这两个组件支持的事件主要有以下:

  • IPointerEnterHandler - OnPointerEnter - Called when a pointer enters the object

  • IPointerExitHandler - OnPointerExit - Called when a pointer exits the object

  • IPointerDownHandler - OnPointerDown - Called when a pointer is pressed on the object

  • IPointerUpHandler - OnPointerUp - Called when a pointer is released (called on the original the pressed object)

  • IPointerClickHandler - OnPointerClick - Called when a pointer is pressed and released on the same object

  • IInitializePotentialDragHandler - OnInitializePotentialDrag - Called when a drag target is found, can be used to initialise values

  • IBeginDragHandler - OnBeginDrag - Called on the drag object when dragging is about to begin

  • IDragHandler - OnDrag - Called on the drag object when a drag is happening

  • IEndDragHandler - OnEndDrag - Called on the drag object when a drag finishes

  • IDropHandler - OnDrop - Called on the object where a drag finishes

  • IScrollHandler - OnScroll - Called when a mouse wheel scrolls

  • IUpdateSelectedHandler - OnUpdateSelected - Called on the selected object each tick

  • ISelectHandler - OnSelect - Called when the object becomes the selected object

  • IDeselectHandler - OnDeselect - Called on the selected object becomes deselected

  • IMoveHandler - OnMove - Called when a move event occurs (left, right, up, down, ect)

  • ISubmitHandler - OnSubmit - Called when the submit button is pressed

  • ICancelHandler - OnCancel - Called when the cancel button is pressed

只要目标对象的mono脚本实现了以上接口,那么输入模块会将检测到的事件通过这些接口通知给目标对象。参考:http://docs.unity3d.com/Manual/SupportedEvents.html

如果你自定义了自己的输入模块,那么以上这些事件肯定是不能用的了。

2、接收输入事件的方式

1)、自行继承接口实现监听

在mono脚本中继承输入模块提供的事件接口,如下图。接口的定义方式也可以查下官方手册,http://docs.unity3d.com/ScriptReference/EventSystems.IBeginDragHandler.html 这边有每一个接口的定义方式,放心大胆地点进去。另外,添加ObjChooseEvent组件的对象,一定要有Collider哦。

2)、 通过EventTrigger组件监听事件

这是一个官方组件。在需要监听事件的对象上,挂上这个组件,然后在Inspector面板展开配置,你会看到这个组件提供了所有输入模块支持的事件类型的监听,如下图。

![](http://mmbiz.qpic.cn/mmbiz/rh7Nru3hbNMN4GibU3YewkznLnj5gliaaKic4cABP1Ve0aSe2S9RYmHO3

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

rTBIJYHdT4ic0VuTL5ib5ia5HPtibhUc6tMw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1)

这种方式的优点是,当你选中一个你要监听的类型,你可以为这个事件类型添加多个监听接口,统一管理,可以清楚的知道到底哪些地方响应了这个事件呢。如果是继承Interface的方式,它将会分散在N个脚本里,一旦出现问题,那查起来一定会很酸爽。

但是这种通过配置的方式,一旦项目多人协作,项目的复杂度起来,这种拖来拽去的配置终究是会有很多问题的,比如某个组件删除,比如响应接口改了个名字~~都会导致配置丢失,而问题又不能及时发现。又或者程序的监听接口因为某些条件而不同。所以也许你会需要第三种方式。

3)、动态添加EventTrigger组件或者修改组件

其实http://www.cnblogs.com/zou90512/p/3995932.html 这位同学的博客对这三种方法都做了很详细的说明。

只不过EventTrigger对外提供的接口不是很友好,导致我们需要添加一个监听,仿佛绕了N了山路弯弯,看着就心情不愉快……反而是这位博主后面说的Button的Click事件的实现方式有点意思……如果项目有需要,也许我们也可以这么做……

  • EventSystem组件提供的一些有意思的接口

其实文档都有http://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.html 只是也许你没有注意。

点击EventSystem对象,你可以看到运行时候的一些详细数据:

变量:

firstSelectedGameObject:这个值可以在面板设置,如果你需要游戏在启动的时候自动选中某个对象,需要鼠标的那一下点击。

currentSelectedGameObject:当前选中的对象,你可以通过这个值判断当前是否鼠标点击在对象上,因为也许你有拖动摄像机的功能,但是你又不喜欢点击某些对象的时候这个功能又被响应,所以通过这个变量判断是一个很好的办法。

接口:

IsPointerOverGameObject:当前鼠标是否在事件系统可以检测的对象上。

SetSelectedGameObject:这个接口也许你会忽略,但是它很棒。因为你点击场景对象的时候,如果不调用这个接口,你的对象是收不到OnSelect事件的,currentSelectedGameObject的值也不会被设置的,必须在点击事件里调用这个接口设置选中对象!

Ex:

public void OnPointerClick(PointerEventData eventData)

{

print (“OnPointerClick…”);

currEvent.SetSelectedGameObject(gameObject);

meObject的值也不会被设置的,必须在点击事件里调用这个接口设置选中对象!

Ex:

public void OnPointerClick(PointerEventData eventData)

{

print (“OnPointerClick…”);

currEvent.SetSelectedGameObject(gameObject);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值