using UnityEngine;
using UnityEditor;
public class ResizablePanels : EditorWindow
{
}
using UnityEngine;
using UnityEditor;
public class ResizablePanels : EditorWindow
{
[MenuItem("Window/Resizable Panels")]
private static void OpenWindow()
{
ResizablePanels window = GetWindow();
window.titleContent = new GUIContent("Resizable Panels"); // 添加标题
}
}
using UnityEngine;
using UnityEditor;
public class ResizablePanels : EditorWindow
{
private Rect upperPanel;
private Rect lowerPanel;
[MenuItem("Window/Resizable Panels")]
private static void OpenWindow()
{
ResizablePanels window = GetWindow();
window.titleContent = new GUIContent("Resizable Panels");
}
private void OnGUI()
{
DrawUpperPanel();
DrawLowerPanel();
}
private void DrawUpperPanel()
{
}
private void DrawLowerPanel()
{
}
}
GUILayout.BeginArea(Rect rect)将创建一个矩形区域,GUILayout.EndArea()标记结束。这些区域将定义我们的面板。我还将在两个区域中添加标签,以便我们可以看到它们的外观。
using UnityEngine;
using UnityEditor;
public class ResizablePanels : EditorWindow
{
private Rect upperPanel;
private Rect lowerPanel;
[MenuItem("Window/Resizable Panels")]
private static void OpenWindow()
{
ResizablePanels window = GetWindow();
window.titleContent = new GUIContent("Resizable Panels");
}
private void OnGUI()
{
DrawUpperPanel();
DrawLowerPanel();
}
private void DrawUpperPanel()
{
upperPanel = new Rect(0, 0, position.width, position.height * 0.5f);
GUILayout.BeginArea(upperPanel);
GUILayout.Label("Upper Panel");
GUILayout.EndArea();
}
private void DrawLowerPanel()
{
lowerPanel = new Rect(0, position.height * 0.5f, position.width, position.height * 0.5f);
GUILayout.BeginArea(lowerPanel);
GUILayout.Label("Lower Panel");
GUILayout.EndArea();
}
}
using UnityEngine;
using UnityEditor;
public class ResizablePanels : EditorWindow
{
private Rect upperPanel;
private Rect lowerPanel;
private float sizeRatio = 0.5f;
[MenuItem("Window/Resizable Panels")]
private static void OpenWindow()
{
ResizablePanels window = GetWindow();
window.titleContent = new GUIContent("Resizable Panels");
}
private void OnGUI()
{
DrawUpperPanel();
DrawLowerPanel();
}
private void DrawUpperPanel()
{
upperPanel = new Rect(0, 0, position.width, position.height * sizeRatio);
GUILayout.BeginArea(upperPanel);
GUILayout.Label("Upper Panel");
GUILayout.EndArea();
}
private void DrawLowerPanel()
{
lowerPanel = new Rect(0, (position.height * sizeRatio), position.width, position.height * (1 - sizeRatio));
GUILayout.BeginArea(lowerPanel);
GUILayout.Label("Lower Panel");
GUILayout.EndArea();
}
}
接下来,我们使用EditorGUIUtility.AddCursorRect(Rect rect,MouseCursor光标) 方法来完成用鼠标调节区域大小的功能
using UnityEngine;
using UnityEditor;
public class ResizablePanels : EditorWindow
{
private Rect upperPanel;
private Rect lowerPanel;
private Rect resizer;
private float sizeRatio = 0.5f;
private bool isResizing;
private GUIStyle resizerStyle;
[MenuItem("Window/Resizable Panels")]
private static void OpenWindow()
{
ResizablePanels window = GetWindow();
window.titleContent = new GUIContent("Resizable Panels");
}
private void OnEnable()
{
resizerStyle = new GUIStyle();
resizerStyle.normal.background = EditorGUIUtility.Load("icons/d_AvatarBlendBackground.png") as Texture2D;
}
private void OnGUI()
{
DrawUpperPanel();
DrawLowerPanel();
DrawResizer();
}
private void DrawUpperPanel()
{
upperPanel = new Rect(0, 0, position.width, position.height * sizeRatio);
GUILayout.BeginArea(upperPanel);
GUILayout.Label("Upper Panel");
GUILayout.EndArea();
}
private void DrawLowerPanel()
{
lowerPanel = new Rect(0, (position.height * sizeRatio) + 5, position.width, position.height * (1 - sizeRatio) - 5);
GUILayout.BeginArea(lowerPanel);
GUILayout.Label("Lower Panel");
GUILayout.EndArea();
}
private void DrawResizer()
{
resizer = new Rect(0, (position.height * sizeRatio) - 5f, position.width, 10f); // 这里是确定位置的
GUILayout.BeginArea(new Rect(resizer.position + (Vector2.up * 5f), new Vector2(position.width, 2)), resizerStyle); // 这里是绘制调节的横条,宽度为整个面板的宽度,高度为2
GUILayout.EndArea();
EditorGUIUtility.AddCursorRect(resizer, MouseCursor.ResizeVertical);
}
}
using UnityEngine;
using UnityEditor;
public class ResizablePanels : EditorWindow
{
private Rect upperPanel;
private Rect lowerPanel;
private Rect resizer;
private float sizeRatio = 0.5f;
private bool isResizing;
private GUIStyle resizerStyle;
[MenuItem("Window/Resizable Panels")]
private static void OpenWindow()
{
ResizablePanels window = GetWindow();
window.titleContent = new GUIContent("Resizable Panels");
}
private void OnEnable()
{
resizerStyle = new GUIStyle();
resizerStyle.normal.background = EditorGUIUtility.Load("icons/d_AvatarBlendBackground.png") as Texture2D;
}
private void OnGUI()
{
DrawUpperPanel();
DrawLowerPanel();
DrawResizer();
ProcessEvents(Event.current);
if (GUI.changed) Repaint();
}
private void DrawUpperPanel()
{
upperPanel = new Rect(0, 0, position.width, position.height * sizeRatio);
GUILayout.BeginArea(upperPanel);
GUILayout.Label("Upper Panel");
GUILayout.EndArea();
}
private void DrawLowerPanel()
{
lowerPanel = new Rect(0, (position.height * sizeRatio) + 5, position.width, position.height * (1 - sizeRatio) - 5);
GUILayout.BeginArea(lowerPanel);
GUILayout.Label("Lower Panel");
GUILayout.EndArea();
}
private void DrawResizer()
{
resizer = new Rect(0, (position.height * sizeRatio) - 5f, position.width, 10f);
GUILayout.BeginArea(new Rect(resizer.position + (Vector2.up * 5f), new Vector2(position.width, 2)), resizerStyle);
GUILayout.EndArea();
EditorGUIUtility.AddCursorRect(resizer, MouseCursor.ResizeVertical);
}
private void ProcessEvents(Event e)
{
switch (e.type)
{
case EventType.MouseDown:
if (e.button == 0 && resizer.Contains(e.mousePosition)) // 鼠标左键按下且鼠标在调节条的位置处
{
isResizing = true; // 调节为true
}
break;
case EventType.MouseUp:
isResizing = false;
break;
}
Resize(e);
}
private void Resize(Event e)
{
if (isResizing) // 如果调节为true
{
sizeRatio = e.mousePosition.y / position.height; // 开始计算比例
Repaint();
}
}
}
本文参考(https://gram.gs/gramlog/creating-editor-windows-in-unity/)
效果如下