Skip to content

Commit dab3c79

Browse files
committed
fix: add component and create menu item
1 parent 914cff4 commit dab3c79

5 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

Assets/PurrUI/Editor/PurrUIMenuItems.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/PurrUI/Runtime/ProceduralUI/GlowGraphic.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
namespace PurrNet.UI
55
{
6+
#if UNITY_6000_0_OR_NEWER
7+
[AddComponentMenu("UI (Canvas)/PurrUI/Glow Graphic")]
8+
#else
9+
[AddComponentMenu("UI/PurrUI/Glow Graphic")]
10+
#endif
611
public class GlowGraphic : SignedDistanceFieldGraphic
712
{
813
const string GLOW_SHADER_NAME = "Hidden/PurrUI/GlowRenderer";

Assets/PurrUI/Runtime/ProceduralUI/GlowModifier.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
namespace PurrNet.UI
44
{
55
[ExecuteAlways]
6+
#if UNITY_6000_0_OR_NEWER
7+
[AddComponentMenu("UI (Canvas)/PurrUI/Glow Modifier")]
8+
#else
9+
[AddComponentMenu("UI/PurrUI/Glow Modifier")]
10+
#endif
611
public class GlowModifier : MonoBehaviour
712
{
813
[SerializeField, HideInInspector] GlowGraphic _glowGraphic;

Assets/PurrUI/Runtime/ProceduralUI/RectangleGraphic.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace PurrNet.UI
44
{
5+
#if UNITY_6000_0_OR_NEWER
6+
[AddComponentMenu("UI (Canvas)/PurrUI/Rectangle Graphic")]
7+
#else
8+
[AddComponentMenu("UI/PurrUI/Rectangle Graphic")]
9+
#endif
510
public class RectangleGraphic : SignedDistanceFieldGraphic
611
{
712
[SerializeField] bool _useMaxRoundness;

0 commit comments

Comments
 (0)