在绘制自定义窗口控件的时候,有时会需要监听鼠标在当前控件上的操作,这时候一般是使用Event类来实现,演示代码如下:
class 技能编辑器 : EditorWindow
{
[MenuItem("工具/技能编辑器")]
static void 技能编辑()
{
GetWindow<技能编辑器>(false, "技能编辑器");
}
Rect testArea = new Rect(200,200,100,100);
private void OnGUI()
{
GUI.Box(testArea, "测试区域", new GUIStyle("ButtonMid"));
int boxHashCode = "BoxId".GetHashCode();
int boxID = GUIUtility.GetControlID(boxHashCode, FocusType.Passive);
Event current = Event.current;
if (current.rawType == EventType.MouseUp)//鼠标抬起事件
{
if (GUIUtility.hotControl == boxID)
{
Debug.Log("鼠标抬起事件");
GUIUtility.hotControl = 0;
current.Use();
}
}
Vector2 mousePosition = new Vector2(current.mousePosition.x, current.mousePosition.y);
if