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 9aecd49..d38fe6f 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))); var showTreeMap = serializedObject.FindProperty("showTreeMap"); EditorGUILayout.PropertyField(showTreeMap); if (showTreeMap.boolValue) @@ -110,6 +111,7 @@ internal static SettingsProvider CreateSettingsProvider() [SerializeField] HierarchyObjectMode hierarchyObjectMode = HierarchyObjectMode.RemoveInBuild; [SerializeField] bool showHierarchyToggles; [SerializeField] bool showComponentIcons; + [SerializeField] bool alwaysHideScriptField; [SerializeField] bool showTreeMap; [SerializeField] Color treeMapColor = new(0.53f, 0.53f, 0.53f, 0.45f); [SerializeField] bool showSeparator; @@ -121,6 +123,7 @@ internal static SettingsProvider CreateSettingsProvider() public HierarchyObjectMode HierarchyObjectMode => hierarchyObjectMode; public bool ShowHierarchyToggles => showHierarchyToggles; public bool ShowComponentIcons => showComponentIcons; + public bool AlwaysHideScriptField => alwaysHideScriptField; public bool ShowTreeMap => showTreeMap; public Color TreeMapColor => treeMapColor; public bool ShowSeparator => showSeparator; 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 407618f..3a87c22 100644 --- a/Alchemy/Assets/Alchemy/Editor/Internal/InspectorHelper.cs +++ b/Alchemy/Assets/Alchemy/Editor/Internal/InspectorHelper.cs @@ -200,9 +200,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 { } diff --git a/Alchemy/ProjectSettings/AlchemySettings.json b/Alchemy/ProjectSettings/AlchemySettings.json index d8c4c3e..d912d53 100644 --- a/Alchemy/ProjectSettings/AlchemySettings.json +++ b/Alchemy/ProjectSettings/AlchemySettings.json @@ -2,6 +2,7 @@ "hierarchyObjectMode": 2, "showHierarchyToggles": true, "showComponentIcons": true, + "alwaysHideScriptField": false "showTreeMap": false, "treeMapColor": { "r": 0.5299999713897705,