简介
Event.rawType
就是初始的type,可以通过Event.Use()
将rawType修改为EventType.Used
。
Event.type
是经过GUIClip过滤的rawType,如果事件触发时,鼠标位置位于当前GUIClip中,返回rawType,如果鼠标不在当前GUIClip中,则设置为EventType.Ignore
Event.GetTypeForControl
相对于Event.type
在GUIClip过滤的基础上,增加了hotControl的判断,如果是hotControl,也直接返回rawType。
验证代码
鼠标点击浅色区域,此时鼠标位置位于GUIClip区域之外,GUICLip内的代码调用rawType会是MouseDown
,type
会是Ignore
,由于设置了hotControl,GetTypeForControl
会是MouseDown
。
在我们重置hotControl为0时,GetTypeForControl
的值也变成Ignore
.
鼠标点击深色区域,此时鼠标位置位于GUIClip之内,此时 rawType, type 以及 GetTypeForControl 的结果都是MouseDown
public class EventDemoEditorWindow : EditorWindow
{
[MenuItem("Demos/EventDemoEditorWindow")]
static void ShowWindow()
{
var window = GetWindow<EventDemoEditorWindow>();
window.titleContent = new GUIContent("EventDemoEditorWindow");
window.Show();
}
private Event eventCache;
private int buttonControlId;
private void OnEnable()
{
}
private void OnGUI()
{
//Event.GetTypeForControl
eventCache = Event.current;
if (MGUILayout.Button($"按钮1"))
{
Debug.Log("点击 按钮1");
}
var buttonRect = GUILayoutUtility.GetLastRect();
buttonControlId = GetLastControlId();
GUIUtility.hotControl = buttonControlId;
//GUIUtility.hotControl = 0;
//鼠标需要点击 ClipRect 的上方区域
var clipRect = Rect.MinMaxRect(0, buttonRect.yMax+100, position.width, position.height);
//if(eventCache.type == EventType.Repaint)
GUI.Box(clipRect, string.Empty);
//buttonControlId = GetLastControlId();
//GUIUtility.hotControl = buttonControlId;
GUI.BeginClip(clipRect, default(Vector2), default(Vector2), false);
{
if (eventCache.rawType == EventType.MouseDown)
{
EventType type;
type = eventCache.rawType;
Debug.Log($"rawType = {type}");
type = eventCache.GetTypeForControl(buttonControlId);
Debug.Log($"GetTypeForControl={type}");
type = eventCache.type;
Debug.Log($"type={type}");
GUIUtility.hotControl = 0;
Debug.Log("---------------- Set HotControl=0 ----------------- ");
type = eventCache.rawType;
Debug.Log($"rawType = {type}");
type = eventCache.GetTypeForControl(buttonControlId);
Debug.Log($"GetTypeForControl={type}");
type = eventCache.type;
Debug.Log($"type={type}");
eventCache.Use();
Debug.Log("------------------ Event.Use() ------------------- ");
type = eventCache.rawType;
Debug.Log($"rawType = {type}");
type = eventCache.type;
Debug.Log($"type={type}");
}
}
GUI.EndClip();
GUIUtility.hotControl = 0;
}
private int GetLastControlId()
{
var type = typeof(EditorGUIUtility);
var field = type.GetField("s_LastControlID", BindingFlags.Static | BindingFlags.NonPublic);
var lastControlID = (int)field.GetValue(null);
return lastControlID;
}
}
标签:rawType,eventCache,unity,GetTypeForControl,Debug,搞懂,type,Event,Log
From: https://www.cnblogs.com/dewxin/p/18669534