GUIUtility.hotControl
IMGUI中每个可点击(交互)的控件都会有一个controlID, 一般在mouseDown的时候设置, mouseUp的时候清除。
#if UNITY_EDITOR using UnityEditor; using UnityEngine; public class TestHotControlIdWindow : EditorWindow { [MenuItem("MyTools/TestHotControlIdWindow")] public static void ShowWindow() { EditorWindow.GetWindow<TestHotControlIdWindow>(false, "TestHotControlIdWindow", true).Show(); } private Vector3 m_Scale = Vector3.one; private Quaternion m_Rot = Quaternion.identity; private float m_Radius = 2.0f; private void OnEnable() { SceneView.onSceneGUIDelegate -= OnMySceneGUI; SceneView.onSceneGUIDelegate += OnMySceneGUI; } private void OnDisable() { SceneView.onSceneGUIDelegate -= OnMySceneGUI; } private void OnMySceneGUI(SceneView sceneView) { var curEvt = Event.current; var hotControl1 = GUIUtility.hotControl; var evtType1 = curEvt.type; //控制柄控件 Handles.color = Color.blue; var pos = new Vector3(-5, 0, 0); float handleSize = HandleUtility.GetHandleSize(pos); m_Scale = Handles.ScaleHandle(m_Scale, pos, Quaternion.identity, handleSize); //缩放控制柄 pos.x -= 6; m_Rot = Handles.RotationHandle(m_Rot, pos); //旋转控制柄 pos.x -= 6; m_Radius = Handles.RadiusHandle(Quaternion.identity, pos, m_Radius); //半径控制柄 //gui控件 GUILayout.BeginArea(new Rect(40, 20, 200, 300), "test"); GUILayout.Button("Button"); GUILayout.Label("Label Text"); GUILayout.EndArea(); if (evtType1 == EventType.MouseMove) { curEvt.Use(); //调用后, curEvtType会变为Used } var hotControl2 = GUIUtility.hotControl; var evtType2 = curEvt.type; if (evtType1 == EventType.Repaint || evtType1 == EventType.Layout) { //no log } else { if (evtType1 == EventType.MouseDown) { Debug.Log($"hotControl 111:{hotControl1}, {hotControl2}; evtType:{evtType1}, {evtType2}"); } else if (0 != hotControl1) { Debug.Log($"hotControl 222:{hotControl1}, {hotControl2}; evtType:{evtType1}, {evtType2}"); } } } } #endif
在Scene视图中点击空白位置, hotControl也是有值的, 是Scene视图本身就属于一个控件么?