版本: Unity 6 (6000.0)
语言英语
  • C#

Event.Use

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们会阅读用户提出的每个建议更改,并在适用的情况下进行更新。

关闭

提交失败

由于某种原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public void Use();

描述

使用此事件。

当您使用事件时,调用此方法。事件的类型将被设置为 EventType.Used,导致其他 GUI 元素忽略它。

类型为 EventType.RepaintEventType.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.PositionHandleHandles.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; } }