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

HandleUtility.PickGameObject

提出修改

成功!

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

关闭

提交失败

由于某些原因,无法提交您提出的修改。请在几分钟后<a>重试</a>。感谢您花时间帮助我们提升 Unity 文档的质量。

关闭

取消

声明

public static GameObject PickGameObject(Vector2 position, out int materialIndex);

声明

public static GameObject PickGameObject(Vector2 position, GameObject[] ignore, out int materialIndex);

声明

public static GameObject PickGameObject(Vector2 position, GameObject[] ignore, GameObject[] selection, out int materialIndex);

声明

public static GameObject PickGameObject(Vector2 position, bool selectPrefabRoot);

声明

public static GameObject PickGameObject(Vector2 position, bool selectPrefabRoot, GameObject[] ignore);

声明

public static GameObject PickGameObject(Vector2 position, bool selectPrefabRoot, GameObject[] ignore, GameObject[] filter);

声明

public static GameObject PickGameObject(Vector2 position, bool selectPrefabRoot, GameObject[] ignore, GameObject[] filter, out int materialIndex);

参数

selectPrefabRoot 选择预制件。.
materialIndex 返回位置最接近 Renderer 组件的材质数组的索引。
position 屏幕坐标系中的位置。窗口的左上角为 (0,0),右下角为 (Screen.width, Screen.height)。
ignore 选择最近的 GameObject 时将不被考虑的 GameObject 数组。
filter 一组可被独选中进行考虑的游戏对象。如果为空,则所有开放场景中的游戏对象均会适合进行选择。
选择 一组可被独选中进行考虑的游戏对象。如果为空,则所有开放场景中的游戏对象均会适合进行选择。

结果

游戏对象 当前位置下的游戏对象。

描述

选择与指定位置最接近的游戏对象。

HandleUtility.PickGameObject 不应在重新绘制时调用。在大多数情况下,适宜在 EventType.MouseDownEventType.MouseUp 期间调用。

using UnityEditor;
using UnityEngine;

static class ShowHovered { static GameObject m_LastHoveredObject, m_LastClickedObject;

[InitializeOnLoadMethod] static void Init() { SceneView.duringSceneGui += OnSceneGUI; }

static void OnSceneGUI(SceneView view) { var evt = Event.current;

// Register a control so that if no other handle is engaged, we can use the event. var pickingControlId = GUIUtility.GetControlID(FocusType.Passive); HandleUtility.AddDefaultControl(pickingControlId);

switch (evt.type) { case EventType.MouseMove: { var picked = HandleUtility.PickGameObject(evt.mousePosition, false);

if (picked != m_LastHoveredObject) { m_LastHoveredObject = picked; SceneView.RepaintAll(); }

break; }

case EventType.MouseDown: { // Make sure to check Tools.viewToolActive before consuming a mouse event, otherwise Scene navigation // controls will not work. if (!Tools.viewToolActive && HandleUtility.nearestControl == pickingControlId) { GUIUtility.hotControl = pickingControlId; evt.Use(); }

break; }

case EventType.MouseUp: { // If the hotControl is not pickingControlId, that means another control has the rights to this event. if (GUIUtility.hotControl != pickingControlId) return;

var picked = HandleUtility.PickGameObject(evt.mousePosition, false);

if (picked != m_LastClickedObject) { m_LastClickedObject = picked; SceneView.RepaintAll(); }

// Make sure to Use() and reset the hotControl the event if we are the active control ID. evt.Use(); GUIUtility.hotControl = 0; break; } }

Handles.BeginGUI(); GUILayout.BeginVertical(EditorStyles.helpBox, GUILayout.ExpandWidth(false)); GUILayout.Label($"Last Hovered Object: {m_LastHoveredObject}"); GUILayout.Label($"Last Clicked Object: {m_LastClickedObject}"); GUILayout.EndVertical(); Handles.EndGUI(); } }