UnityGUI 输入和处理事件的类型。
使用它来确定 GUI 中发生了哪种类型的事件。事件类型包括鼠标点击、鼠标拖动、按钮按下、鼠标进入或退出窗口,以及滚轮以及下面提到的其他事件。
事件可用于阻止其他 GUI 元素响应该事件。请参考 Event.Use。
其他资源: Event.type, Event, GUI 脚本指南.
//Attach this script to a GameObject //This script is a basic overview of some of the Event Types available. It outputs messages depending on the current Event Type.
using UnityEngine;
public class Example : MonoBehaviour { void OnGUI() { Event m_Event = Event.current;
if (m_Event.type == EventType.MouseDown) { Debug.Log("Mouse Down."); }
if (m_Event.type == EventType.MouseDrag) { Debug.Log("Mouse Dragged."); }
if (m_Event.type == EventType.MouseUp) { Debug.Log("Mouse Up."); } } }
using UnityEngine;
public class Example : MonoBehaviour { void OnGUI() { Event m_Event = Event.current;
if (m_Event.type == EventType.MouseDown) { if (m_Event.pointerType == PointerType.Pen) //Check if it's a pen event. Debug.Log("Pen Down."); else Debug.Log("Mouse Down."); //If it's not a pen event, it's a mouse event. } } }
MouseDown | 鼠标按钮被按下。 |
MouseUp | 鼠标按钮被释放。 |
MouseMove | 鼠标移动(仅限编辑器视图)。 |
MouseDrag | 鼠标被拖动。 |
KeyDown | 键盘按键被按下。 |
KeyUp | 键盘按键被释放。 |
ScrollWheel | 滚轮被移动。 |
Repaint | 重绘事件。每帧都会发送一个。 |
Layout | 布局事件。 |
DragUpdated | 仅限编辑器:拖放操作已更新。 |
DragPerform | 仅限编辑器:拖放操作已执行。 |
DragExited | 仅限编辑器:拖放操作已退出。 |
Ignore | 事件应被忽略。 |
Used | 已处理的事件。 |
ValidateCommand | 验证特殊命令(例如复制和粘贴)。 |
ExecuteCommand | 执行特殊命令(例如复制和粘贴)。 |
ContextClick | 用户右键点击(或在 Mac 上使用 Control 键点击)。 |
MouseEnterWindow | 鼠标进入窗口(仅限编辑器视图)。 |
MouseLeaveWindow | 鼠标离开窗口(仅限编辑器视图)。 |
TouchDown | 直接操作设备(手指、笔)触碰屏幕。 |
TouchUp | 直接操作设备(手指、笔)离开屏幕。 |
TouchMove | 直接操作设备(手指、笔)在屏幕上移动(拖动)。 |
TouchEnter | 直接操作设备(手指、笔)移动到窗口内(拖动)。 |
TouchLeave | 直接操作设备(手指、笔)移动到窗口外(拖动)。 |
TouchStationary | 直接操作设备(手指、笔)静止事件(长时间触摸)。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.