com.pakforge.inspectorbutton 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "Core.Utilities.InspectorButton",
3
+ "rootNamespace": "PakForge.InspectorButton",
4
+ "references": [],
5
+ "includePlatforms": [],
6
+ "excludePlatforms": [],
7
+ "allowUnsafeCode": false,
8
+ "overrideReferences": false,
9
+ "precompiledReferences": [],
10
+ "autoReferenced": true,
11
+ "defineConstraints": [],
12
+ "versionDefines": [],
13
+ "noEngineReferences": false
14
+ }
@@ -0,0 +1,7 @@
1
+ fileFormatVersion: 2
2
+ guid: 85c9b4b2c6508714d81a141492f5bc02
3
+ AssemblyDefinitionImporter:
4
+ externalObjects: {}
5
+ userData:
6
+ assetBundleName:
7
+ assetBundleVariant:
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "Core.Utilities.InspectorButton.Editor",
3
+ "rootNamespace": "PakForge.InspectorButton.Editor",
4
+ "references": [
5
+ "GUID:85c9b4b2c6508714d81a141492f5bc02"
6
+ ],
7
+ "includePlatforms": [
8
+ "Editor"
9
+ ],
10
+ "excludePlatforms": [],
11
+ "allowUnsafeCode": false,
12
+ "overrideReferences": false,
13
+ "precompiledReferences": [],
14
+ "autoReferenced": true,
15
+ "defineConstraints": [],
16
+ "versionDefines": [],
17
+ "noEngineReferences": false
18
+ }
@@ -0,0 +1,7 @@
1
+ fileFormatVersion: 2
2
+ guid: 7ae48aa5143e1fc488f2356211b263ff
3
+ AssemblyDefinitionImporter:
4
+ externalObjects: {}
5
+ userData:
6
+ assetBundleName:
7
+ assetBundleVariant:
@@ -0,0 +1,183 @@
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.Reflection;
4
+ using UnityEditor;
5
+ using UnityEditor.UIElements;
6
+ using UnityEngine;
7
+ using UnityEngine.UIElements;
8
+
9
+ namespace PakForge.InspectorButton.Editor
10
+ {
11
+ [CustomEditor(typeof(MonoBehaviour), true)]
12
+ [CanEditMultipleObjects]
13
+ public class InspectorButtonEditor : UnityEditor.Editor
14
+ {
15
+ private class MethodData
16
+ {
17
+ public MethodInfo MethodInfo;
18
+ public string ButtonName;
19
+ public object[] Parameters;
20
+ public ParameterInfo[] ParameterInfos;
21
+ }
22
+
23
+ private List<MethodData> _methods = new();
24
+
25
+ private void OnEnable()
26
+ {
27
+ _methods = new List<MethodData>();
28
+ var targetType = target.GetType();
29
+ var methods = targetType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
30
+
31
+ foreach (var method in methods)
32
+ {
33
+ var attr = method.GetCustomAttribute<InspectorButtonAttribute>();
34
+ if (attr != null)
35
+ {
36
+ var paramInfos = method.GetParameters();
37
+ var methodData = new MethodData
38
+ {
39
+ MethodInfo = method,
40
+ ButtonName = string.IsNullOrEmpty(attr.ButtonName) ? ObjectNames.NicifyVariableName(method.Name) : attr.ButtonName,
41
+ ParameterInfos = paramInfos,
42
+ Parameters = new object[paramInfos.Length]
43
+ };
44
+
45
+ // Initialize default values for parameters
46
+ for (int i = 0; i < paramInfos.Length; i++)
47
+ {
48
+ var type = paramInfos[i].ParameterType;
49
+ if (type.IsValueType)
50
+ {
51
+ methodData.Parameters[i] = Activator.CreateInstance(type);
52
+ }
53
+ }
54
+
55
+ _methods.Add(methodData);
56
+ }
57
+ }
58
+ }
59
+
60
+ public override VisualElement CreateInspectorGUI()
61
+ {
62
+ var root = new VisualElement();
63
+
64
+ // Default inspector
65
+ InspectorElement.FillDefaultInspector(root, serializedObject, this);
66
+
67
+ if (_methods.Count == 0) return root;
68
+
69
+ root.Add(new ToolbarSpacer { style = { height = 10 } });
70
+
71
+ foreach (var method in _methods)
72
+ {
73
+ var methodBox = new VisualElement();
74
+ methodBox.style.marginTop = 2;
75
+ methodBox.style.marginBottom = 2;
76
+ methodBox.style.paddingTop = 5;
77
+ methodBox.style.paddingBottom = 5;
78
+ methodBox.style.borderTopLeftRadius = 5;
79
+ methodBox.style.borderTopRightRadius = 5;
80
+ methodBox.style.borderBottomLeftRadius = 5;
81
+ methodBox.style.borderBottomRightRadius = 5;
82
+ methodBox.style.backgroundColor = new Color(0.19f, 0.19f, 0.19f, 1);
83
+
84
+ var button = new Button(() =>
85
+ {
86
+ foreach (var t in targets)
87
+ {
88
+ method.MethodInfo.Invoke(t, method.Parameters);
89
+ }
90
+ })
91
+ {
92
+ text = method.ButtonName
93
+ };
94
+ methodBox.Add(button);
95
+
96
+ if (method.ParameterInfos.Length > 0)
97
+ {
98
+ var foldout = new Foldout
99
+ {
100
+ text = "Parameters",
101
+ value = false,
102
+ style =
103
+ {
104
+ marginLeft = 15
105
+ }
106
+ };
107
+
108
+ for (int i = 0; i < method.ParameterInfos.Length; i++)
109
+ {
110
+ var pIndex = i;
111
+ var pInfo = method.ParameterInfos[i];
112
+ var pType = pInfo.ParameterType;
113
+ var pName = ObjectNames.NicifyVariableName(pInfo.Name);
114
+
115
+ VisualElement field = null;
116
+
117
+ if (pType == typeof(int))
118
+ {
119
+ var f = new IntegerField(pName) { value = (int)method.Parameters[pIndex] };
120
+ f.RegisterValueChangedCallback(evt => method.Parameters[pIndex] = evt.newValue);
121
+ field = f;
122
+ }
123
+ else if (pType == typeof(string))
124
+ {
125
+ var f = new TextField(pName) { value = (string)method.Parameters[pIndex] };
126
+ f.RegisterValueChangedCallback(evt => method.Parameters[pIndex] = evt.newValue);
127
+ field = f;
128
+ }
129
+ else if (pType == typeof(float))
130
+ {
131
+ var f = new FloatField(pName) { value = (float)method.Parameters[pIndex] };
132
+ f.RegisterValueChangedCallback(evt => method.Parameters[pIndex] = evt.newValue);
133
+ field = f;
134
+ }
135
+ else if (pType == typeof(bool))
136
+ {
137
+ var f = new Toggle(pName) { value = (bool)method.Parameters[pIndex] };
138
+ f.RegisterValueChangedCallback(evt => method.Parameters[pIndex] = evt.newValue);
139
+ field = f;
140
+ }
141
+ else if (pType == typeof(Vector2))
142
+ {
143
+ var f = new Vector2Field(pName) { value = (Vector2)method.Parameters[pIndex] };
144
+ f.RegisterValueChangedCallback(evt => method.Parameters[pIndex] = evt.newValue);
145
+ field = f;
146
+ }
147
+ else if (pType == typeof(Vector3))
148
+ {
149
+ var f = new Vector3Field(pName) { value = (Vector3)method.Parameters[pIndex] };
150
+ f.RegisterValueChangedCallback(evt => method.Parameters[pIndex] = evt.newValue);
151
+ field = f;
152
+ }
153
+ else if (pType == typeof(Vector4))
154
+ {
155
+ var f = new Vector4Field(pName) { value = (Vector4)method.Parameters[pIndex] };
156
+ f.RegisterValueChangedCallback(evt => method.Parameters[pIndex] = evt.newValue);
157
+ field = f;
158
+ }
159
+ else if (pType.IsEnum)
160
+ {
161
+ var f = new EnumField(pName, (Enum)method.Parameters[pIndex]);
162
+ f.RegisterValueChangedCallback(evt => method.Parameters[pIndex] = evt.newValue);
163
+ field = f;
164
+ }
165
+ else
166
+ {
167
+ field = new HelpBox($"Parameter type {pType.Name} not supported for {pName}", HelpBoxMessageType.Warning);
168
+ }
169
+
170
+ field.style.marginRight = 15;
171
+ foldout.Add(field);
172
+ }
173
+
174
+ methodBox.Add(foldout);
175
+ }
176
+
177
+ root.Add(methodBox);
178
+ }
179
+
180
+ return root;
181
+ }
182
+ }
183
+ }
@@ -0,0 +1,2 @@
1
+ fileFormatVersion: 2
2
+ guid: 880e26d217721d04681ed57738895ddf
package/Editor.meta ADDED
@@ -0,0 +1,8 @@
1
+ fileFormatVersion: 2
2
+ guid: b3375be39de7bf84ca00edf8a0860292
3
+ folderAsset: yes
4
+ DefaultImporter:
5
+ externalObjects: {}
6
+ userData:
7
+ assetBundleName:
8
+ assetBundleVariant:
@@ -0,0 +1,17 @@
1
+ using System;
2
+ using System.Diagnostics;
3
+
4
+ namespace PakForge.InspectorButton
5
+ {
6
+ [Conditional("UNITY_EDITOR")]
7
+ [AttributeUsage(AttributeTargets.Method)]
8
+ public class InspectorButtonAttribute : Attribute
9
+ {
10
+ public string ButtonName { get; }
11
+
12
+ public InspectorButtonAttribute(string buttonName = "")
13
+ {
14
+ ButtonName = buttonName;
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 8dd08904effc4d6ba2153d6ba940310d
3
+ timeCreated: 1773861476
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "com.pakforge.inspectorbutton",
3
+ "displayName": "Inspector Button",
4
+ "description": "The InspectorButton package allows you to add an attribute to methods that will draw a button in the scripts inspector for easy testing of execution.",
5
+ "author": {
6
+ "name": "Marc Robbins",
7
+ "email": "marccrobbins@gmail.com"
8
+ },
9
+ "category": "Unity",
10
+ "unity": "6000.3",
11
+ "publicConfig": {
12
+ "registry": "http://34.124.124.81:4873/"
13
+ },
14
+ "version": "1.1.0"
15
+ }
@@ -0,0 +1,7 @@
1
+ fileFormatVersion: 2
2
+ guid: 6f7873d852627cc45ad76d8f5ec29e7e
3
+ PackageManifestImporter:
4
+ externalObjects: {}
5
+ userData:
6
+ assetBundleName:
7
+ assetBundleVariant: