使用该事件。
在您已使用了一个事件时调用此方法。该事件的类型将被设置为 EventType.Used,这会导致其他 GUI 元素忽略此类型。
不应使用类型为 EventType.Repaint 和 EventType.Layout 的事件。
尝试在此类事件上调用此方法将会发出警告。
The following example demonstrates how events are consumed and used up. Copy this code into a script, and open the Example Window this sample creates from the Window menu.
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(); } } }
The following example demonstrates how handles such as Handles.PositionHandle and Handles.FreeMoveHandle might use events.
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; } }