|
| 1 | +using PurrNet.UI; |
| 2 | +using UnityEditor; |
| 3 | +using UnityEngine; |
| 4 | +using UnityEngine.EventSystems; |
| 5 | +using UnityEngine.UI; |
| 6 | + |
| 7 | +namespace PurrNet.Editor.UI |
| 8 | +{ |
| 9 | + static class PurrUIMenuItems |
| 10 | + { |
| 11 | +#if UNITY_6000_0_OR_NEWER |
| 12 | + const string UI_CREATE_PATH = "GameObject/UI (Canvas)/"; |
| 13 | +#else |
| 14 | + const string UI_CREATE_PATH = "GameObject/UI/"; |
| 15 | +#endif |
| 16 | + |
| 17 | + [MenuItem(UI_CREATE_PATH + "PurrUI Rectangle", false, 2100)] |
| 18 | + static void CreateRectangle(MenuCommand menuCommand) |
| 19 | + { |
| 20 | + CreateUIElement<RectangleGraphic>("Rectangle", menuCommand); |
| 21 | + } |
| 22 | + |
| 23 | + [MenuItem(UI_CREATE_PATH + "PurrUI Glow", false, 2101)] |
| 24 | + static void CreateGlow(MenuCommand menuCommand) |
| 25 | + { |
| 26 | + CreateUIElement<GlowGraphic>("Glow", menuCommand); |
| 27 | + } |
| 28 | + |
| 29 | + static void CreateUIElement<T>(string name, MenuCommand menuCommand) where T : Graphic |
| 30 | + { |
| 31 | + var parent = menuCommand.context as GameObject; |
| 32 | + |
| 33 | + if (parent == null || parent.GetComponentInParent<Canvas>() == null) |
| 34 | + { |
| 35 | +#if UNITY_2022_1_OR_NEWER |
| 36 | + var canvas = Object.FindAnyObjectByType<Canvas>(); |
| 37 | +#else |
| 38 | + var canvas = Object.FindObjectOfType<Canvas>(); |
| 39 | +#endif |
| 40 | + if (canvas != null && canvas.gameObject.activeInHierarchy) |
| 41 | + parent = canvas.gameObject; |
| 42 | + else |
| 43 | + parent = CreateCanvas(); |
| 44 | + } |
| 45 | + |
| 46 | + var go = new GameObject(name, typeof(RectTransform), typeof(T)); |
| 47 | + GameObjectUtility.SetParentAndAlign(go, parent); |
| 48 | + |
| 49 | + var rt = go.GetComponent<RectTransform>(); |
| 50 | + rt.sizeDelta = new Vector2(100f, 100f); |
| 51 | + |
| 52 | + Undo.RegisterCreatedObjectUndo(go, "Create " + name); |
| 53 | + Selection.activeGameObject = go; |
| 54 | + } |
| 55 | + |
| 56 | + static GameObject CreateCanvas() |
| 57 | + { |
| 58 | + var canvasGo = new GameObject("Canvas", |
| 59 | + typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster)); |
| 60 | + |
| 61 | + Undo.RegisterCreatedObjectUndo(canvasGo, "Create Canvas"); |
| 62 | + |
| 63 | + var canvas = canvasGo.GetComponent<Canvas>(); |
| 64 | + canvas.renderMode = RenderMode.ScreenSpaceOverlay; |
| 65 | + |
| 66 | + EnsureEventSystem(); |
| 67 | + |
| 68 | + return canvasGo; |
| 69 | + } |
| 70 | + |
| 71 | + static void EnsureEventSystem() |
| 72 | + { |
| 73 | +#if UNITY_2022_1_OR_NEWER |
| 74 | + if (Object.FindAnyObjectByType<EventSystem>() != null) |
| 75 | + return; |
| 76 | +#else |
| 77 | + if (Object.FindObjectOfType<EventSystem>() != null) |
| 78 | + return; |
| 79 | +#endif |
| 80 | + |
| 81 | + var esGo = new GameObject("EventSystem", |
| 82 | + typeof(EventSystem), typeof(StandaloneInputModule)); |
| 83 | + |
| 84 | + Undo.RegisterCreatedObjectUndo(esGo, "Create EventSystem"); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments