Unity UGUI的EventSystem(事件系统)组件的介绍及使用
  AzfuDM7Dy7RN 2023年11月02日 155 0

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:设置默认选中的按钮

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class DefaultButton : MonoBehaviour
{
    public Button defaultButton;

    void Start()
    {
        EventSystem.current.SetSelectedGameObject(defaultButton.gameObject);
    }
}

操作步骤:

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

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

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ButtonClick : MonoBehaviour, IPointerClickHandler
{
    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("Button clicked!");
    }
}

操作步骤:

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

例子3:拖拽物体

using UnityEngine;
using UnityEngine.EventSystems;

public class DragObject : MonoBehaviour, IDragHandler
{
    public void OnDrag(PointerEventData eventData)
    {
        transform.position = eventData.position;
    }
}

操作步骤:

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

例子4:滚动列表

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ScrollList : MonoBehaviour, IScrollHandler
{
    public ScrollRect scrollRect;

    public void OnScroll(PointerEventData eventData)
    {
        scrollRect.verticalNormalizedPosition += eventData.scrollDelta.y * 0.1f;
    }
}

操作步骤:

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

例子5:按键导航

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Navigation : MonoBehaviour, ISelectHandler
{
    public Button nextButton;

    public void OnSelect(BaseEventData eventData)
    {
        EventSystem.current.SetSelectedGameObject(nextButton.gameObject);
    }
}

操作步骤:

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

注意事项

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

参考资料

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  NPQODODLqddb   2024年05月17日   69   0   0 .NET
  mVIyUuLhKsxa   2024年05月17日   52   0   0 .NET
  XkHDHG7Y62UM   2024年05月17日   45   0   0 .NET
  f18CFixvrKz8   2024年05月18日   86   0   0 .NET
  rBgzkhl6abbw   2024年05月18日   78   0   0 .NET
  MYrYhn3ObP4r   2024年05月17日   41   0   0 .NET
  S34pIcuyyIVd   2024年05月17日   60   0   0 .NET
  gKJ2xtp6I8Y7   2024年05月17日   50   0   0 .NET
  MYrYhn3ObP4r   2024年05月17日   39   0   0 .NET
AzfuDM7Dy7RN