使用此事件。
当您使用事件时,调用此方法。事件的类型将被设置为 EventType.Used,导致其他 GUI 元素忽略它。
类型为 EventType.Repaint 和 EventType.Layout 的事件不应使用。尝试对这些事件调用此方法将发出警告。
以下示例演示了如何使用和消耗事件。将此代码复制到脚本中,然后从“窗口”菜单中打开此示例创建的“示例窗口”。
using UnityEditor; using UnityEngine;
public class ExampleWindow : EditorWindow { [MenuItem("Window/Show Example Window")] public static void ShowWindow() { GetWindow(typeof(ExampleWindow)); }
private void OnGUI() { if (Event.current.type == EventType.MouseDown && Event.current.button == 0) { Debug.Log("Left clicked at: " + Event.current.mousePosition); // This if statement Uses up the current MouseDown event so that // subsequent code or GUI elements ignore this MouseDown event. Event.current.Use(); }
// This if statement does not check Event.current.button, but it only triggers // when Event.current.button is not 0 because the previous if statement will // Use up the MouseDown event if it is. if (Event.current.type == EventType.MouseDown) { Debug.Log("This only prints when we right click!"); Event.current.Use(); } } }
以下示例演示了如何处理诸如 Handles.PositionHandle 和 Handles.FreeMoveHandle 之类的句柄可能使用事件。
using UnityEditor; using UnityEngine;
public static class CustomHandle { public static bool DoHandle(Vector3 worldpos, float size, float pickSize) { int id = GUIUtility.GetControlID(FocusType.Passive); Event evt = Event.current;
bool clicked = false;
switch (evt.GetTypeForControl(id)) { case EventType.MouseDown: if (evt.button == 0 && HandleUtility.nearestControl == id) { GUIUtility.hotControl = id;
evt.Use(); // Using the MouseDown event clicked = true; } break;
case EventType.MouseMove: HandleUtility.Repaint(); evt.Use(); // Using the MouseMove event break;
case EventType.MouseUp: if (evt.button == 0 && HandleUtility.nearestControl == id) { GUIUtility.hotControl = 0; evt.Use(); // Using the MouseUp event } break;
case EventType.Layout: HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(worldpos, pickSize)); // Keep in mind Layout events should not be Used! break;
case EventType.Repaint: // Draw the handle here // Keep in mind Repaint events should not be Used! break; }
return clicked; } }