赞
踩
using UnityEngine;
using UnityEditor;
public class ExampleEditorWindow : EditorWindow
{
private Vector2 lScrollPosition, rScrollPosition;
//分割线宽度
private const float splitterWidth = 2f;
//分割线位置
private float splitterPos;
private Rect splitterRect;
//是否正在拖拽分割线
private bool isDragging;
[MenuItem("Tools/Example Editor Window")]
public static void Open() {
GetWindow<ExampleEditorWindow>().Show();
}
private void OnEnable() {
splitterPos = position.width * .3f;
}
internal void OnGUI() {
GUILayout.BeginHorizontal();
{
lScrollPosition = GUILayout.BeginScrollView(lScrollPosition , GUILayout.Width(splitterPos) , GUILayout.MaxWidth(splitterPos) , GUILayout.MinWidth(splitterPos));
OnLeftGUI();
GUILayout.EndScrollView();
//分割线
GUILayout.Box(string.Empty , GUILayout.Width(splitterWidth) , GUILayout.MaxWidth(splitterWidth) , GUILayout.MinWidth(splitterWidth) , GUILayout.ExpandHeight(true));
splitterRect = GUILayoutUtility.GetLastRect();
rScrollPosition = GUILayout.BeginScrollView(rScrollPosition , GUILayout.ExpandWidth(true));
OnRightGUI();
GUILayout.EndScrollView();
}
GUILayout.EndHorizontal();
if (Event.current != null) {
//光标
EditorGUIUtility.AddCursorRect(splitterRect , MouseCursor.ResizeHorizontal);
switch (Event.current.rawType) {
//开始拖拽分割线
case EventType.MouseDown:
isDragging = splitterRect.Contains(Event.current.mousePosition);
break;
case EventType.MouseDrag:
if (isDragging) {
splitterPos += Event.current.delta.x;
//限制其最大最小值
splitterPos = Mathf.Clamp(splitterPos , position.width * .2f , position.width * .8f);
Repaint();
}
break;
//结束拖拽分割线
case EventType.MouseUp:
if (isDragging)
isDragging = false;
break;
}
}
}
private void OnLeftGUI() {
for (int i = 0 ; i < 10 ; i++) {
if (GUILayout.Button("左侧按钮")) {
Debug.Log("左侧按钮按下");
}
}
}
private void OnRightGUI() {
if (GUILayout.Button("右侧按钮")) {
Debug.Log("右侧按钮按下");
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。