Unity 制作编辑器工具的注意点

Unity中制作Editor工具,采用的OnGUI的方式,因此比较麻烦,也有很多容易犯的错误。

GUI、GUILayout、EditorGUI、EditorGUILayout这4个类是用来绘制界面的。

GUI和EditorGUI是一组。EditorGUI是Unity封装过的,里面有很多可以直接调用的函数用来绘制特殊字段,例如Int,Object,Float等

GUILayout和EditorGUILayout是一组。Unity封装了一个布局的功能。

一、GUILayout和EditorGUILayout的使用

layout是以一个范围进行布局的,如果是自定义窗口,这个范围默认是整个窗口。

  GUILayout.BeginArea和  GUILayout.EndArea 可以自定义这个范围。

 GUILayout.Space() 可以帮助控制间隔。

 

二、GUIEditorGUI的使用

因为不会自动布局,所以需要传入rect作为绘制的范围。

x和y的间隔最好定一个全局的,然后按顺序渲染就行。

 

三、监听数据事件

在同一个Ongui函数内从后向前触发事件,就是同一个ongui函数中,绘制后面的会先触发点击事件。所以如果Ongui中有绘制重叠的交互事件可能会有错。

两种比较麻烦的办法:

1.将绘制和触发分开,然后在绘制之前,以正常的前后顺序调用相应触发判断

 public void CheckMouseEvent()
//        {
//            if (Event.current.type == EventType.MouseDown)
//            {
//                if (titleRect.Contains(Event.current.mousePosition))
//                {
//                    Event.current.Use();
//                    windows.PopTriggerPanel(this);
//                    deltapos = position.position - Event.current.mousePosition;
//                }
//                if (position.Contains(Event.current.mousePosition))
//                {
//                    windows.PopTriggerPanel(this);
//                    if(trigger.type == TriggerInteractive.StateTrigger && windows.connectStateWindow != null)
//                    {
//                        trigger.stateTrigger.state.Add(windows.connectStateWindow.state.guid);
//                        windows.connectStateWindow.isConnectLine = false;
//                        connectedWindows.Add(windows.connectStateWindow);
//                        windows.connectStateWindow = null;
//                    }
//                }
//            }

//            if (Event.current.type == EventType.MouseDrag)
//            {
//                if (titleRect.Contains(Event.current.mousePosition))
//                {
//                    Event.current.Use();
//                    position.position = deltapos + Event.current.mousePosition;
//                    trigger.pos = position.position;
//                    titleRect = new Rect(position.x, position.y, position.width / 3, 30);
//                }
//            }
//        }

2. 主动阻断交互

GUI.enabled = !(Event.current.isMouse && new Rect(position.x + position.width - 100, position.y + 30, 100, 100).Contains(Event.current.mousePosition));

在后方交互渲染前,先将GUI.enabled设置为false

你可能感兴趣的:(Unity效果实现思路)