From 1b343d817111c603715c04a8572003addbb1dbcb Mon Sep 17 00:00:00 2001 From: Pete Goodfellow Date: Tue, 26 Mar 2024 16:47:31 +0000 Subject: [PATCH 1/3] Add preference to always hide script field. --- Alchemy/Assets/Alchemy/Editor/AlchemyEditor.cs | 7 ++++--- Alchemy/Assets/Alchemy/Editor/AlchemySettings.cs | 9 ++++++--- Alchemy/ProjectSettings/AlchemySettings.json | 3 ++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Alchemy/Assets/Alchemy/Editor/AlchemyEditor.cs b/Alchemy/Assets/Alchemy/Editor/AlchemyEditor.cs index 715f430..d7e9a22 100644 --- a/Alchemy/Assets/Alchemy/Editor/AlchemyEditor.cs +++ b/Alchemy/Assets/Alchemy/Editor/AlchemyEditor.cs @@ -61,8 +61,9 @@ void OnDestroy() } public override VisualElement CreateInspectorGUI() - { - var root = new VisualElement(); + { + var settings = AlchemySettings.GetOrCreateSettings(); + var root = new VisualElement(); var targetType = target.GetType(); if (targetType.HasCustomAttribute()) @@ -80,7 +81,7 @@ public override VisualElement CreateInspectorGUI() #endif // Add script field - if (targetType.GetCustomAttribute() == null) + if (!settings.AlwaysHideScriptField && targetType.GetCustomAttribute() == null) { var scriptField = new PropertyField(serializedObject.FindProperty(ScriptFieldName)); scriptField.SetEnabled(false); diff --git a/Alchemy/Assets/Alchemy/Editor/AlchemySettings.cs b/Alchemy/Assets/Alchemy/Editor/AlchemySettings.cs index be6bddc..1785edc 100644 --- a/Alchemy/Assets/Alchemy/Editor/AlchemySettings.cs +++ b/Alchemy/Assets/Alchemy/Editor/AlchemySettings.cs @@ -65,9 +65,10 @@ internal static SettingsProvider CreateSettingsProvider() using (var changeCheck = new EditorGUI.ChangeCheckScope()) { - EditorGUILayout.PropertyField(serializedObject.FindProperty("hierarchyObjectMode")); - EditorGUILayout.PropertyField(serializedObject.FindProperty("showHierarchyToggles"), new GUIContent("Show Toggles")); - EditorGUILayout.PropertyField(serializedObject.FindProperty("showComponentIcons")); + EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(hierarchyObjectMode))); + EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(showHierarchyToggles)), new GUIContent("Show Toggles")); + EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(showComponentIcons))); + EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(alwaysHideScriptField))); if (changeCheck.changed) { @@ -84,9 +85,11 @@ internal static SettingsProvider CreateSettingsProvider() [SerializeField] HierarchyObjectMode hierarchyObjectMode = HierarchyObjectMode.RemoveInBuild; [SerializeField] bool showHierarchyToggles; [SerializeField] bool showComponentIcons; + [SerializeField] bool alwaysHideScriptField; public HierarchyObjectMode HierarchyObjectMode => hierarchyObjectMode; public bool ShowHierarchyToggles => showHierarchyToggles; public bool ShowComponentIcons => showComponentIcons; + public bool AlwaysHideScriptField => alwaysHideScriptField; } } \ No newline at end of file diff --git a/Alchemy/ProjectSettings/AlchemySettings.json b/Alchemy/ProjectSettings/AlchemySettings.json index 4a12bca..a5d6ef5 100644 --- a/Alchemy/ProjectSettings/AlchemySettings.json +++ b/Alchemy/ProjectSettings/AlchemySettings.json @@ -1,5 +1,6 @@ { "hierarchyObjectMode": 2, "showHierarchyToggles": true, - "showComponentIcons": true + "showComponentIcons": true, + "alwaysHideScriptField": true } \ No newline at end of file From 827ccf06cb1120e6f9b65e3afd6e0ba1b729adeb Mon Sep 17 00:00:00 2001 From: Pete Goodfellow Date: Tue, 26 Mar 2024 20:54:16 +0000 Subject: [PATCH 2/3] Add a useParameters bool to the ButtonAttribute. Also add tooltip with the method signature, pretty print the method name ( doThing() -> Do Thing ), and make method buttons that do have parameters use any default parameters in the invocation foldout too. --- .../Alchemy/Editor/Elements/MethodButton.cs | 25 +++++++++++++------ .../Editor/Elements/ReflectionField.cs | 4 +-- .../Editor/Internal/InspectorHelper.cs | 4 +-- .../Runtime/Inspector/InspectorAttributes.cs | 6 ++++- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/Alchemy/Assets/Alchemy/Editor/Elements/MethodButton.cs b/Alchemy/Assets/Alchemy/Editor/Elements/MethodButton.cs index 642aaf7..6d9976b 100644 --- a/Alchemy/Assets/Alchemy/Editor/Elements/MethodButton.cs +++ b/Alchemy/Assets/Alchemy/Editor/Elements/MethodButton.cs @@ -8,22 +8,32 @@ public sealed class MethodButton : VisualElement { const string ButtonLabelText = "Invoke"; - public MethodButton(object target, MethodInfo methodInfo) + public MethodButton(object target, MethodInfo methodInfo, bool useParameters) { var parameters = methodInfo.GetParameters(); + var parameterObjects = new object[parameters.Length]; + for(int i = 0; i < parameters.Length; i++) + { + if(parameters[i].HasDefaultValue) + parameterObjects[i] = parameters[i].DefaultValue; + else + parameterObjects[i] = TypeHelper.CreateDefaultInstance(parameters[i].ParameterType); + } // Create parameterless button - if (parameters.Length == 0) + if (!useParameters || parameters.Length == 0) { - button = new Button(() => methodInfo.Invoke(target, null)) - { - text = methodInfo.Name - }; + if(parameters.Length > 0) + button = new Button(() => methodInfo.Invoke(target, parameterObjects)); + else + button = new Button(() => methodInfo.Invoke(target, null)); + + button.text = ObjectNames.NicifyVariableName(methodInfo.Name); + button.tooltip = methodInfo.ToString(); Add(button); return; } - var parameterObjects = new object[parameters.Length]; var box = new HelpBox(); Add(box); @@ -59,7 +69,6 @@ public MethodButton(object target, MethodInfo methodInfo) { var index = i; var parameter = parameters[index]; - parameterObjects[index] = TypeHelper.CreateDefaultInstance(parameter.ParameterType); var element = new GenericField(parameterObjects[index], parameter.ParameterType, ObjectNames.NicifyVariableName(parameter.Name)); element.OnValueChanged += x => parameterObjects[index] = x; element.style.paddingRight = 4f; diff --git a/Alchemy/Assets/Alchemy/Editor/Elements/ReflectionField.cs b/Alchemy/Assets/Alchemy/Editor/Elements/ReflectionField.cs index a1c3f41..04beb1b 100644 --- a/Alchemy/Assets/Alchemy/Editor/Elements/ReflectionField.cs +++ b/Alchemy/Assets/Alchemy/Editor/Elements/ReflectionField.cs @@ -20,9 +20,9 @@ public void Rebuild(object target, MemberInfo memberInfo) if (memberInfo is MethodInfo methodInfo) { - if (methodInfo.HasCustomAttribute()) + if (methodInfo.TryGetCustomAttribute(out var buttonAttribute)) { - var button = new MethodButton(target, methodInfo); + var button = new MethodButton(target, methodInfo, buttonAttribute.useParameters); Add(button); } return; diff --git a/Alchemy/Assets/Alchemy/Editor/Internal/InspectorHelper.cs b/Alchemy/Assets/Alchemy/Editor/Internal/InspectorHelper.cs index 2fa14cd..83eaed8 100644 --- a/Alchemy/Assets/Alchemy/Editor/Internal/InspectorHelper.cs +++ b/Alchemy/Assets/Alchemy/Editor/Internal/InspectorHelper.cs @@ -199,9 +199,9 @@ public static VisualElement CreateMemberElement(SerializedObject serializedObjec switch (memberInfo) { case MethodInfo methodInfo: - if (methodInfo.HasCustomAttribute()) + if (methodInfo.TryGetCustomAttribute(out var buttonAttribute)) { - return new MethodButton(target, methodInfo); + return new MethodButton(target, methodInfo, buttonAttribute.useParameters); } break; case FieldInfo: diff --git a/Alchemy/Assets/Alchemy/Runtime/Inspector/InspectorAttributes.cs b/Alchemy/Assets/Alchemy/Runtime/Inspector/InspectorAttributes.cs index acbedb1..dd16208 100644 --- a/Alchemy/Assets/Alchemy/Runtime/Inspector/InspectorAttributes.cs +++ b/Alchemy/Assets/Alchemy/Runtime/Inspector/InspectorAttributes.cs @@ -18,7 +18,11 @@ public sealed class OrderAttribute : Attribute } [AttributeUsage(AttributeTargets.Method)] - public sealed class ButtonAttribute : Attribute { } + public sealed class ButtonAttribute : Attribute + { + public ButtonAttribute(bool useParameters = true) => this.useParameters = useParameters; + public readonly bool useParameters; + } [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] public sealed class ShowInInspectorAttribute : Attribute { } From f5529c38f171941d9a5e6fe36e11317b30c8087d Mon Sep 17 00:00:00 2001 From: Pete Goodfellow Date: Sun, 31 Mar 2024 22:50:55 +0100 Subject: [PATCH 3/3] Set alwaysHideScriptField to false by default, matching Unity behaviour. --- Alchemy/ProjectSettings/AlchemySettings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Alchemy/ProjectSettings/AlchemySettings.json b/Alchemy/ProjectSettings/AlchemySettings.json index a5d6ef5..4d60e67 100644 --- a/Alchemy/ProjectSettings/AlchemySettings.json +++ b/Alchemy/ProjectSettings/AlchemySettings.json @@ -2,5 +2,5 @@ "hierarchyObjectMode": 2, "showHierarchyToggles": true, "showComponentIcons": true, - "alwaysHideScriptField": true + "alwaysHideScriptField": false } \ No newline at end of file