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

GUI splitter control

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

 1 using UnityEngine;
 2  using UnityEditor;
 3  
 4  public class GUISplitter : EditorWindow {
 5      Vector2 posLeft;
 6      Vector2 posRight;
 7      GUIStyle styleLeftView;
 8      GUIStyle styleRightView;
 9      float splitterPos;
10      Rect splitterRect;
11      Vector2 dragStartPos;
12      bool dragging;
13      float splitterWidth = 5;
14      
15      // Add menu named "My Window" to the Window menu
16      [MenuItem ("GUI/GUISplitter")]
17      static void Init () {
18             GUISplitter window = (GUISplitter)EditorWindow.GetWindow (
19                 typeof (GUISplitter));
20          window.position = new Rect(200, 200, 200,200);
21          window.splitterPos = 100;
22      }
23      
24      void OnGUI (){
25          if (styleLeftView == null)
26              styleLeftView = new GUIStyle(GUI.skin.box);
27          if (styleRightView == null)
28              styleRightView = new GUIStyle(GUI.skin.button);
29          
30          GUILayout.BeginHorizontal ();
31      
32          // Left view
33          posLeft = GUILayout.BeginScrollView (posLeft, 
34              GUILayout.Width (splitterPos), 
35              GUILayout.MaxWidth(splitterPos), 
36              GUILayout.MinWidth(splitterPos));
37              GUILayout.Box ("Left View", 
38                      styleLeftView, 
39                      GUILayout.ExpandWidth(true), 
40                      GUILayout.ExpandHeight(true));
41          GUILayout.EndScrollView ();
42          
43          // Splitter
44          GUILayout.Box ("", 
45              GUILayout.Width(splitterWidth), 
46              GUILayout.MaxWidth (splitterWidth), 
47              GUILayout.MinWidth(splitterWidth),
48              GUILayout.ExpandHeight(true));
49          splitterRect = GUILayoutUtility.GetLastRect ();
50      
51          // Right view
52          posRight = GUILayout.BeginScrollView (posRight, 
53              GUILayout.ExpandWidth(true));
54              GUILayout.Box ("Right View", 
55              styleRightView, 
56              GUILayout.ExpandWidth(true), 
57              GUILayout.ExpandHeight(true));
58          GUILayout.EndScrollView ();
59          
60          GUILayout.EndHorizontal ();
61          
62          // Splitter events
63          if (Event.current != null) {
64              switch (Event.current.rawType) {
65                  case EventType.MouseDown:
66                      if (splitterRect.Contains (Event.current.mousePosition)) {
67                          Debug.Log ("Start dragging");
68                          dragging = true;
69                      }
70                      break;
71                  case EventType.MouseDrag:
72                      if (dragging){
73                          Debug.Log ("moving splitter");
74                          splitterPos += Event.current.delta.x;
75                          Repaint ();
76                      }
77                      break;
78                  case EventType.MouseUp:
79                      if (dragging){
80                          Debug.Log ("Done dragging");
81                          dragging = false;
82                      }
83                      break;
84              }
85          }
86      }
87  }

Unity编辑器中分割线拖拽的实现_第1张图片

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

转载于:https://www.cnblogs.com/AaronBlogs/p/8985342.html

你可能感兴趣的:(Unity编辑器中分割线拖拽的实现)