|
1 | | -using System.Collections; |
2 | | -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using UnityEditor; |
3 | 3 | using UnityEngine; |
4 | 4 |
|
5 | 5 | /// <summary> |
6 | 6 | /// 重复弹出的编辑器窗口 |
7 | 7 | /// </summary> |
8 | | -public class RepeateWindow : EditorWindowBase { |
| 8 | +public class RepeateWindow : EditorWindowBase |
| 9 | +{ |
9 | 10 |
|
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) |
11 | 15 | { |
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)); |
13 | 24 | 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(); |
14 | 101 | } |
15 | 102 | } |
0 commit comments