目录
4.设置窗中窗 BeginWindows和EndWindows
public class EditorToolWindow : EditorWindow {
public static EditorToolWindow ins;
[MenuItem("EditorTool/OpenWindow")]
public static void OpenWindow() {
ins = GetWindow<EditorToolWindow>();
ins.Show();
}
}
效果:
1.设置窗口位置和大小
public class EditorToolWindow : EditorWindow {
public static EditorToolWindow ins;
[MenuItem("EditorTool/OpenWindow")]
public static void OpenWindow1() {
Rect rect = new Rect(new Vector2(500, 300), new Vector2(300, 500));
ins = GetWindow<EditorToolWindow>();
ins.position = rect;
ins.Show();
}
}
tips: 另一种写法
ins = GetWindowWithRect<EditorToolWindow>(rect);
ins.Show();
效果:
2.设置窗口最大和最小
ins.maxSize = new Vector2(500, 500);
ins.minSize = new Vector2(100, 100);
3.设置窗口标题
ins.titleContent = new GUIContent("我是窗口标题");
4.设置窗中窗 BeginWindows和EndWindows
Rect winRact = new Rect(new Vector2(100, 100), new Vector2(200, 200));
private void OnGUI() {
BeginWindows();
GUI.Window(1, winRact, DoWindow, "窗中窗");
EndWindows();
}
void DoWindow(int id) {
GUILayout.Button("按钮");
}
效果:
5.添加窗中窗可拖动 GUI.DragWindow()
Rect winRact = new Rect(new Vector2(100, 100), new Vector2(200, 200));
private void OnGUI() {
BeginWindows();
winRact = GUI.Window(1, winRact, DoWindow, "窗中窗");
EndWindows();
}
void DoWindow(int id) {
GUILayout.Button("按钮");
GUI.DragWindow();
}
注:
winRact = GUI.Window(1, winRact, DoWindow, "窗中窗");
此处如果不用winRact接收返还 窗口将无法拖动
效果:
完整代码:
using UnityEngine;
using UnityEditor;
public class EditorToolWindow : EditorWindow {
public static EditorToolWindow ins;
[MenuItem("EditorTool/OpenWindow")]
public static void OpenWindow1() {
Rect rect = new Rect(new Vector2(500, 100), new Vector2(300, 500));
ins = GetWindow<EditorToolWindow>();
ins.position = rect;
ins.maxSize = new Vector2(500, 500);
ins.minSize = new Vector2(100, 100);
ins.titleContent = new GUIContent("我是窗口标题");
ins.Show();
}
Rect winRact = new Rect(new Vector2(100, 100), new Vector2(200, 200));
private void OnGUI() {
BeginWindows();
winRact = GUI.Window(1, winRact, DoWindow, "窗中窗");
EndWindows();
}
void DoWindow(int id) {
GUILayout.Button("关闭");
GUI.DragWindow();
}
}