Unity编辑器中分割线拖拽的实现

时间:2021-09-13 06:11:07

GUI splitter control

How can I make a GUI splitter control, similar to the splitter the console has?

 using UnityEngine;
using UnityEditor; public class GUISplitter : EditorWindow {
Vector2 posLeft;
Vector2 posRight;
GUIStyle styleLeftView;
GUIStyle styleRightView;
float splitterPos;
Rect splitterRect;
Vector2 dragStartPos;
bool dragging;
float splitterWidth = ; // Add menu named "My Window" to the Window menu
[MenuItem ("GUI/GUISplitter")]
static void Init () {
GUISplitter window = (GUISplitter)EditorWindow.GetWindow (
typeof (GUISplitter));
window.position = new Rect(, , ,);
window.splitterPos = ;
} void OnGUI (){
if (styleLeftView == null)
styleLeftView = new GUIStyle(GUI.skin.box);
if (styleRightView == null)
styleRightView = new GUIStyle(GUI.skin.button); GUILayout.BeginHorizontal (); // Left view
posLeft = GUILayout.BeginScrollView (posLeft,
GUILayout.Width (splitterPos),
GUILayout.MaxWidth(splitterPos),
GUILayout.MinWidth(splitterPos));
GUILayout.Box ("Left View",
styleLeftView,
GUILayout.ExpandWidth(true),
GUILayout.ExpandHeight(true));
GUILayout.EndScrollView (); // Splitter
GUILayout.Box ("",
GUILayout.Width(splitterWidth),
GUILayout.MaxWidth (splitterWidth),
GUILayout.MinWidth(splitterWidth),
GUILayout.ExpandHeight(true));
splitterRect = GUILayoutUtility.GetLastRect (); // Right view
posRight = GUILayout.BeginScrollView (posRight,
GUILayout.ExpandWidth(true));
GUILayout.Box ("Right View",
styleRightView,
GUILayout.ExpandWidth(true),
GUILayout.ExpandHeight(true));
GUILayout.EndScrollView (); GUILayout.EndHorizontal (); // Splitter events
if (Event.current != null) {
switch (Event.current.rawType) {
case EventType.MouseDown:
if (splitterRect.Contains (Event.current.mousePosition)) {
Debug.Log ("Start dragging");
dragging = true;
}
break;
case EventType.MouseDrag:
if (dragging){
Debug.Log ("moving splitter");
splitterPos += Event.current.delta.x;
Repaint ();
}
break;
case EventType.MouseUp:
if (dragging){
Debug.Log ("Done dragging");
dragging = false;
}
break;
}
}
}
}

Unity编辑器中分割线拖拽的实现

原文链接:https://answers.unity.com/questions/461391/gui-splitter-control.html