diff --git a/Assets/Pacore/Editor/Drawers/Blocks/DisabledGUIBlock.cs b/Assets/Pacore/Editor/Drawers/Blocks/DisabledGUIBlock.cs index be392de..b1619d6 100644 --- a/Assets/Pacore/Editor/Drawers/Blocks/DisabledGUIBlock.cs +++ b/Assets/Pacore/Editor/Drawers/Blocks/DisabledGUIBlock.cs @@ -3,7 +3,7 @@ using System; namespace PashaBibko.Pacore.Editor.Drawers { - public class DisabledGUIBlock : IDisposable + public sealed class DisabledGUIBlock : IDisposable { public DisabledGUIBlock() => GUI.enabled = false; public void Dispose() => GUI.enabled = true; diff --git a/Assets/Pacore/Editor/Drawers/DetectInspectorChangesDrawer.cs b/Assets/Pacore/Editor/Drawers/DetectInspectorChangesDrawer.cs new file mode 100644 index 0000000..b092b41 --- /dev/null +++ b/Assets/Pacore/Editor/Drawers/DetectInspectorChangesDrawer.cs @@ -0,0 +1,46 @@ +using PashaBibko.Pacore.Shared.Attributes; +using System.Reflection; +using UnityEditor; +using UnityEngine; + +namespace PashaBibko.Pacore.Editor.Drawers +{ + [CustomPropertyDrawer(typeof(DetectInspectorChangesAttribute))] + public sealed class DetectInspectorChangesDrawer : PropertyDrawer + { + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + /* Draws the property and checks for changes */ + EditorGUI.BeginProperty(position, label, property); + EditorGUI.BeginChangeCheck(); + + EditorGUI.PropertyField(position, property, label); + if (EditorGUI.EndChangeCheck()) // Returns true if there were changes + { + property.serializedObject.ApplyModifiedProperties(); // Applies the changes + if (attribute is DetectInspectorChangesAttribute inspectorChangesAttribute) + { + const BindingFlags BINDING_FLAGS = + BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; + + string methodName = inspectorChangesAttribute.ActionName; + + /* Fetches the method and the object to call it on */ + Object target = property.serializedObject.targetObject; + MethodInfo method = target.GetType().GetMethod(methodName, BINDING_FLAGS); + if (method == null) + { + Debug.LogError($"Method not found [{methodName}]"); + } + + else + { + method.Invoke(target, null); + } + } + } + + EditorGUI.EndProperty(); + } + } +} \ No newline at end of file diff --git a/Assets/Pacore/Editor/Drawers/DetectInspectorChangesDrawer.cs.meta b/Assets/Pacore/Editor/Drawers/DetectInspectorChangesDrawer.cs.meta new file mode 100644 index 0000000..4458cce --- /dev/null +++ b/Assets/Pacore/Editor/Drawers/DetectInspectorChangesDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6a62b785aacd4f1b80370036cf8bb087 +timeCreated: 1769190711 \ No newline at end of file diff --git a/Assets/Pacore/Editor/Drawers/InspectorReadOnlyDrawer.cs b/Assets/Pacore/Editor/Drawers/InspectorReadOnlyDrawer.cs index 7bf41af..ef8e244 100644 --- a/Assets/Pacore/Editor/Drawers/InspectorReadOnlyDrawer.cs +++ b/Assets/Pacore/Editor/Drawers/InspectorReadOnlyDrawer.cs @@ -5,19 +5,17 @@ using UnityEngine; namespace PashaBibko.Pacore.Editor.Drawers { [CustomPropertyDrawer(typeof(InspectorReadOnlyAttribute))] - public class InspectorReadOnlyDrawer : PropertyDrawer + public sealed class InspectorReadOnlyDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { - if (attribute is not InspectorReadOnlyAttribute roAttribute) + if (attribute is InspectorReadOnlyAttribute readOnlyAttribute) { - return; - } - - using (new DisabledGUIBlock()) - { - label.text = roAttribute.Name ?? label.text; // Uses custom name if it exists - EditorGUI.PropertyField(position, property, label); + using (new DisabledGUIBlock()) + { + label.text = readOnlyAttribute.Name ?? label.text; // Uses custom name if it exists + EditorGUI.PropertyField(position, property, label); + } } } } diff --git a/Assets/Pacore/Shared/Attributes/DetectInspectorChanges.cs b/Assets/Pacore/Shared/Attributes/DetectInspectorChanges.cs new file mode 100644 index 0000000..21048c0 --- /dev/null +++ b/Assets/Pacore/Shared/Attributes/DetectInspectorChanges.cs @@ -0,0 +1,17 @@ +using JetBrains.Annotations; +using UnityEngine; +using System; + +namespace PashaBibko.Pacore.Shared.Attributes +{ + [MeansImplicitUse, AttributeUsage(validOn: AttributeTargets.Field)] + public sealed class DetectInspectorChangesAttribute : PropertyAttribute + { + public string ActionName { get; } + + public DetectInspectorChangesAttribute([NotNull] string function) + { + ActionName = function; + } + } +} \ No newline at end of file diff --git a/Assets/Pacore/Shared/Attributes/DetectInspectorChanges.cs.meta b/Assets/Pacore/Shared/Attributes/DetectInspectorChanges.cs.meta new file mode 100644 index 0000000..6c1e2b2 --- /dev/null +++ b/Assets/Pacore/Shared/Attributes/DetectInspectorChanges.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 7b814b9ed1e8467084b8448ccdaf4ff6 +timeCreated: 1769190547 \ No newline at end of file diff --git a/Assets/Pacore/Shared/Attributes/InspectorReadOnly.cs b/Assets/Pacore/Shared/Attributes/InspectorReadOnly.cs index 03a5592..5b9845f 100644 --- a/Assets/Pacore/Shared/Attributes/InspectorReadOnly.cs +++ b/Assets/Pacore/Shared/Attributes/InspectorReadOnly.cs @@ -6,18 +6,18 @@ namespace PashaBibko.Pacore.Shared.Attributes { #if UNITY_EDITOR [MeansImplicitUse, AttributeUsage(validOn: AttributeTargets.Field)] - public class InspectorReadOnlyAttribute : PropertyAttribute + public sealed class InspectorReadOnlyAttribute : PropertyAttribute { public string Name { get; } - public InspectorReadOnlyAttribute(string _name = null) + public InspectorReadOnlyAttribute(string name = null) { - Name = _name; + Name = name; } } #else // #if UNITY_EDITOR [MeansImplicitUse, AttributeUsage(validOn: AttributeTargets.Field)] - public class InspectorReadOnlyAttribute : Attribute + public sealed class InspectorReadOnlyAttribute : Attribute { public InspectorReadOnlyAttribute(string _name = null) { } } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index d9e5484..665da31 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -272,6 +272,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: a411a69d284bfd04bafc210a81b43e88, type: 3} m_Name: m_EditorClassIdentifier: + go: {fileID: 0} + Test: 2147483647 --- !u!1 &410087039 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/TestMonoBehaviour.cs b/Assets/TestMonoBehaviour.cs index 0440188..ab58d0f 100644 --- a/Assets/TestMonoBehaviour.cs +++ b/Assets/TestMonoBehaviour.cs @@ -4,4 +4,12 @@ using UnityEngine; public class TestMonoBehaviour : MonoBehaviour { [InspectorReadOnly, SerializeField] private GameObject go; + + [DetectInspectorChanges("OnTestChange")] + public int Test; + + private void OnTestChange() + { + Debug.Log($"New value: {Test}"); + } }