经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » unity » 查看文章
Unity UGUI的EventSystem(事件系统)组件的介绍及使用
来源:cnblogs  作者:AlianBlank  时间:2023/7/26 9:15:27  对本文有异议

Unity UGUI的EventSystem(事件系统)组件的介绍及使用

1. 什么是EventSystem组件?

EventSystem是Unity UGUI中的一个重要组件,用于处理用户输入事件,如点击、拖拽、滚动等。它负责将用户输入事件传递给合适的UI元素,并触发相应的事件回调函数。

2. EventSystem组件的工作原理

EventSystem组件通过射线检测来确定用户输入事件发生的位置,并将事件传递给最合适的UI元素。它会根据UI元素的层级关系和射线检测结果来确定事件的目标对象。

3. EventSystem组件的常用属性

  • firstSelectedGameObject:设置默认选中的UI元素。
  • sendNavigationEvents:是否发送导航事件。
  • pixelDragThreshold:拖拽事件的像素阈值。
  • currentInputModule:当前使用的输入模块。

4. EventSystem组件的常用函数

  • SetSelectedGameObject(GameObject selected):设置当前选中的UI元素。
  • RaycastAll(PointerEventData eventData, List<RaycastResult> resultAppendList):执行射线检测,并将结果保存到指定的列表中。
  • UpdateModules():更新输入模块。

5. 完整例子代码

例子1:设置默认选中的按钮

  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using UnityEngine.UI;
  4. public class DefaultButton : MonoBehaviour
  5. {
  6. public Button defaultButton;
  7. void Start()
  8. {
  9. EventSystem.current.SetSelectedGameObject(defaultButton.gameObject);
  10. }
  11. }

操作步骤:

  1. 创建一个空物体,并将DefaultButton脚本挂载上去。
  2. 在Inspector面板中将需要默认选中的按钮赋值给defaultButton变量。

例子2:点击按钮触发事件

  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using UnityEngine.UI;
  4. public class ButtonClick : MonoBehaviour, IPointerClickHandler
  5. {
  6. public void OnPointerClick(PointerEventData eventData)
  7. {
  8. Debug.Log("Button clicked!");
  9. }
  10. }

操作步骤:

  1. 创建一个按钮,并将ButtonClick脚本挂载上去。
  2. 在ButtonClick脚本中实现OnPointerClick函数,并在函数中添加需要执行的代码。

例子3:拖拽物体

  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. public class DragObject : MonoBehaviour, IDragHandler
  4. {
  5. public void OnDrag(PointerEventData eventData)
  6. {
  7. transform.position = eventData.position;
  8. }
  9. }

操作步骤:

  1. 创建一个物体,并将DragObject脚本挂载上去。
  2. 在DragObject脚本中实现OnDrag函数,并在函数中修改物体的位置。

例子4:滚动列表

  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using UnityEngine.UI;
  4. public class ScrollList : MonoBehaviour, IScrollHandler
  5. {
  6. public ScrollRect scrollRect;
  7. public void OnScroll(PointerEventData eventData)
  8. {
  9. scrollRect.verticalNormalizedPosition += eventData.scrollDelta.y * 0.1f;
  10. }
  11. }

操作步骤:

  1. 创建一个滚动列表,并将ScrollList脚本挂载上去。
  2. 在ScrollList脚本中实现OnScroll函数,并在函数中修改滚动列表的位置。

例子5:按键导航

  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using UnityEngine.UI;
  4. public class Navigation : MonoBehaviour, ISelectHandler
  5. {
  6. public Button nextButton;
  7. public void OnSelect(BaseEventData eventData)
  8. {
  9. EventSystem.current.SetSelectedGameObject(nextButton.gameObject);
  10. }
  11. }

操作步骤:

  1. 创建多个按钮,并将Navigation脚本挂载上去。
  2. 在Navigation脚本中实现OnSelect函数,并在函数中设置下一个选中的按钮。

注意事项

  • EventSystem组件只能存在一个,多个EventSystem会导致输入事件无法正常处理。
  • EventSystem组件需要与其他UI组件配合使用,如Button、ScrollRect等。

参考资料

原文链接:https://www.cnblogs.com/alianblank/p/17580929.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号