Skip to content

Commit 38063dd

Browse files
committed
主界面完善、重复弹出窗口完善
1 parent 853df02 commit 38063dd

File tree

4 files changed

+409
-7
lines changed

4 files changed

+409
-7
lines changed

UnityEditorExtension/MultiEditorWindow/Assets/Editor/MainWindow.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private void ShowEditorGUI()
5252
EditorGUILayout.LabelField("点击下面的按钮创建重复弹出窗口", labelStyle, GUILayout.Width(220));
5353
if (GUILayout.Button("创建窗口", GUILayout.Width(80)))
5454
{
55-
RepeateWindow.Popup();
55+
RepeateWindow.Popup(window.position.position);
5656
}
5757
GUILayout.EndVertical();
5858
GUILayout.EndArea();
@@ -61,7 +61,7 @@ private void ShowEditorGUI()
6161
private void OnDestroy()
6262
{
6363
//主界面销毁的时候,附带销毁创建出来的子界面
64-
EditorWindowMgr.RemoveEditorWindow(this);
64+
EditorWindowMgr.RemoveEditorWindow(window);
6565
EditorWindowMgr.DestoryAllWindow();
6666
}
6767

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,102 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
1+
using System;
2+
using UnityEditor;
33
using UnityEngine;
44

55
/// <summary>
66
/// 重复弹出的编辑器窗口
77
/// </summary>
8-
public class RepeateWindow : EditorWindowBase {
8+
public class RepeateWindow : EditorWindowBase
9+
{
910

10-
public static void Popup()
11+
private static Vector2 minResolution = new Vector2(300, 200);
12+
private static Rect leftUpRect = new Rect(new Vector2(0, 0), minResolution);
13+
14+
public static void Popup(Vector3 position)
1115
{
12-
RepeateWindow window = new RepeateWindow();
16+
// RepeateWindow window = new RepeateWindow();
17+
RepeateWindow window = GetWindowWithRectPrivate(typeof(RepeateWindow), leftUpRect, true, "重复弹出窗口") as RepeateWindow;
18+
window.minSize = minResolution;
19+
//要在设置位置之前,先把窗体注册到管理器中,以便更新窗体的优先级
20+
EditorWindowMgr.AddRepeateWindow(window);
21+
//刷新界面偏移量
22+
int offset = (window.Priority - 10) * 30;
23+
window.position = new Rect(new Vector2(position.x + offset, position.y + offset), new Vector2(800, 400));
1324
window.Show();
25+
//手动聚焦
26+
window.Focus();
27+
}
28+
29+
/// <summary>
30+
/// 重写EditorWindow父类的创建窗口函数
31+
/// </summary>
32+
/// <param name="t"></param>
33+
/// <param name="rect"></param>
34+
/// <param name="utility"></param>
35+
/// <param name="title"></param>
36+
/// <returns></returns>
37+
private static EditorWindow GetWindowWithRectPrivate(Type t, Rect rect, bool utility, string title)
38+
{
39+
//UnityEngine.Object[] array = Resources.FindObjectsOfTypeAll(t);
40+
EditorWindow editorWindow = null;/*= (array.Length <= 0) ? null : ((EditorWindow)array[0]);*/
41+
if (!(bool)editorWindow)
42+
{
43+
editorWindow = (ScriptableObject.CreateInstance(t) as EditorWindow);
44+
editorWindow.minSize = new Vector2(rect.width, rect.height);
45+
editorWindow.maxSize = new Vector2(rect.width, rect.height);
46+
editorWindow.position = rect;
47+
if (title != null)
48+
{
49+
editorWindow.titleContent = new GUIContent(title);
50+
}
51+
if (utility)
52+
{
53+
editorWindow.ShowUtility();
54+
}
55+
else
56+
{
57+
editorWindow.Show();
58+
}
59+
}
60+
else
61+
{
62+
editorWindow.Focus();
63+
}
64+
return editorWindow;
65+
}
66+
67+
68+
private void OnGUI()
69+
{
70+
OnEditorGUI();
71+
}
72+
73+
private void OnEditorGUI()
74+
{
75+
GUILayout.Space(12);
76+
GUILayout.BeginVertical();
77+
EditorGUILayout.LabelField("我是重复弹出的窗体", GUILayout.Width(200));
78+
if (GUILayout.Button("创建窗体", GUILayout.Width(100)))
79+
{
80+
//重复创建自己
81+
Popup(this.position.position);
82+
}
83+
GUILayout.Space(12);
84+
if (GUILayout.Button("关闭窗体", GUILayout.Width(100)))
85+
{
86+
this.Close();
87+
}
88+
GUILayout.EndVertical();
89+
}
90+
91+
private void OnDestroy()
92+
{
93+
//销毁窗体的时候,从管理器中移除该窗体的缓存,并且重新刷新焦点
94+
EditorWindowMgr.RemoveRepeateWindow(this);
95+
EditorWindowMgr.FoucusWindow();
96+
}
97+
98+
private void OnFocus()
99+
{
100+
EditorWindowMgr.FoucusWindow();
14101
}
15102
}

0 commit comments

Comments
 (0)