Added DetectInspectorChanges

This commit is contained in:
2026-01-23 19:05:46 +00:00
parent b742651e62
commit 36052b32ca
9 changed files with 91 additions and 14 deletions

View File

@@ -3,7 +3,7 @@ using System;
namespace PashaBibko.Pacore.Editor.Drawers namespace PashaBibko.Pacore.Editor.Drawers
{ {
public class DisabledGUIBlock : IDisposable public sealed class DisabledGUIBlock : IDisposable
{ {
public DisabledGUIBlock() => GUI.enabled = false; public DisabledGUIBlock() => GUI.enabled = false;
public void Dispose() => GUI.enabled = true; public void Dispose() => GUI.enabled = true;

View File

@@ -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();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6a62b785aacd4f1b80370036cf8bb087
timeCreated: 1769190711

View File

@@ -5,20 +5,18 @@ using UnityEngine;
namespace PashaBibko.Pacore.Editor.Drawers namespace PashaBibko.Pacore.Editor.Drawers
{ {
[CustomPropertyDrawer(typeof(InspectorReadOnlyAttribute))] [CustomPropertyDrawer(typeof(InspectorReadOnlyAttribute))]
public class InspectorReadOnlyDrawer : PropertyDrawer public sealed class InspectorReadOnlyDrawer : PropertyDrawer
{ {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) 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()) using (new DisabledGUIBlock())
{ {
label.text = roAttribute.Name ?? label.text; // Uses custom name if it exists label.text = readOnlyAttribute.Name ?? label.text; // Uses custom name if it exists
EditorGUI.PropertyField(position, property, label); EditorGUI.PropertyField(position, property, label);
} }
} }
} }
} }
}

View File

@@ -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;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7b814b9ed1e8467084b8448ccdaf4ff6
timeCreated: 1769190547

View File

@@ -6,18 +6,18 @@ namespace PashaBibko.Pacore.Shared.Attributes
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
[MeansImplicitUse, AttributeUsage(validOn: AttributeTargets.Field)] [MeansImplicitUse, AttributeUsage(validOn: AttributeTargets.Field)]
public class InspectorReadOnlyAttribute : PropertyAttribute public sealed class InspectorReadOnlyAttribute : PropertyAttribute
{ {
public string Name { get; } public string Name { get; }
public InspectorReadOnlyAttribute(string _name = null) public InspectorReadOnlyAttribute(string name = null)
{ {
Name = _name; Name = name;
} }
} }
#else // #if UNITY_EDITOR #else // #if UNITY_EDITOR
[MeansImplicitUse, AttributeUsage(validOn: AttributeTargets.Field)] [MeansImplicitUse, AttributeUsage(validOn: AttributeTargets.Field)]
public class InspectorReadOnlyAttribute : Attribute public sealed class InspectorReadOnlyAttribute : Attribute
{ {
public InspectorReadOnlyAttribute(string _name = null) { } public InspectorReadOnlyAttribute(string _name = null) { }
} }

View File

@@ -272,6 +272,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: a411a69d284bfd04bafc210a81b43e88, type: 3} m_Script: {fileID: 11500000, guid: a411a69d284bfd04bafc210a81b43e88, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
go: {fileID: 0}
Test: 2147483647
--- !u!1 &410087039 --- !u!1 &410087039
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@@ -4,4 +4,12 @@ using UnityEngine;
public class TestMonoBehaviour : MonoBehaviour public class TestMonoBehaviour : MonoBehaviour
{ {
[InspectorReadOnly, SerializeField] private GameObject go; [InspectorReadOnly, SerializeField] private GameObject go;
[DetectInspectorChanges("OnTestChange")]
public int Test;
private void OnTestChange()
{
Debug.Log($"New value: {Test}");
}
} }