selectPrefabRoot | 选择预制件。. |
materialIndex | 返回位置最接近 Renderer 组件的材质数组的索引。 |
position | 屏幕坐标系中的位置。窗口的左上角为 (0,0),右下角为 (Screen.width, Screen.height)。 |
ignore | 选择最近的 GameObject 时将不被考虑的 GameObject 数组。 |
filter | 一组可被独选中进行考虑的游戏对象。如果为空,则所有开放场景中的游戏对象均会适合进行选择。 |
选择 | 一组可被独选中进行考虑的游戏对象。如果为空,则所有开放场景中的游戏对象均会适合进行选择。 |
游戏对象 当前位置下的游戏对象。
选择与指定位置最接近的游戏对象。
HandleUtility.PickGameObject 不应在重新绘制时调用。在大多数情况下,适宜在 EventType.MouseDown 或 EventType.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(); } }