- Home /
How to check if inspector object field was changed
I draw in the inspector property to ScriptableObject like this:
EditorGUILayout.PropertyField(myScriptableObject);
How can I detect when reference was added/removed in the inspector?
For eg. when I remove reference with DEL key, I would like to call an event or do sth.
Answer by Kamilche_ · Apr 24, 2016 at 05:12 PM
@metroidsnes , you could do the following:
EditorGUI.BeginChangeCheck();
EditorGUI.PropertyField(r, sp, GUIContent.none);
if (EditorGUI.EndChangeCheck())
{
// Do something when the property changes
}
Answer by NotHalfBrad · Sep 16, 2015 at 12:29 PM
You can use MonoBehavior.OnValidate()
It runs whenever there is a change to a property of the component script it is in (I don't know how it behaves when interacting with custom inspectors). It works in both runtime and design-time.
From there, you could check if the value has changed to null, and I suspect that would be similar to checking for a deleted value like you want.
I couldn't get OnValidate working for custom inspectors. But the following is effectively the same and does work :) EditorGUI.BeginChangeCheck(); EditorGUI.PropertyField(r, sp, GUIContent.none); if (EditorGUI.EndChangeCheck()) { ((MonoBehaviour)target).OnValidate(); }(I don't know how it behaves when interacting with custom inspectors)
Answer by c8theino · May 04, 2021 at 11:01 AM
Here is a simple system which let's you keep track of changed variables and call functions if value is changed.
using UnityEditor;
[CustomEditor(typeof(Foo))]
public class FooEditor : Editor {
public SerializedProperty value;
void OnEnable() {
value = serializedObject.FindProperty("value");
}
public override void OnInspectorGUI() {
// Get value before change
int previousValue = value.intValue;
// Make all the public and serialized fields visible in Inspector
base.OnInspectorGUI();
// Load changed values
serializedObject.Update();
// Check if value has changed
if (previousValue != value.intValue) {
// Do something...
Foo foo = (Foo)target;
foo.CallPublicFunction();
}
serializedObject.ApplyModifiedProperties();
}
}
Answer by JigneshKoradiya · Mar 25, 2015 at 07:13 PM
you can check it with,check manual for more reference
if(GUI.changed) {
}
GUI.changed will be true for any changed inspector control. I need to know when only the ScriptableObject field was changed.
private void DrawPathDataAssetField(Action callback = null) {
serializedObject.Update();
// Remember current pata data field value.
var prevPathData = pathData.objectReferenceValue;
EditorGUILayout.PropertyField(
pathData,
new GUIContent(
"Path Asset",
"Asset containing all path data."));
serializedObject.Apply$$anonymous$$odifiedProperties();
Debug.Log("prev: " + prevPathData + " new: pathData.objectReferenceValue" + );
// If path data was changed, execute callback.
if (!ReferenceEquals(pathData.objectReferenceValue, prevPathData)) {
if (callback != null) callback();
}
}
Callback never gets executed. When there's no reference assigned and I add one, this is the result:
Problem solved, it was my fault. Ins$$anonymous$$d of changing the said field, I used a custom button to create new asset file, so I was checking something that didn't even change.
@metroidsnes Could u elaborate on what the solution u found was? I'm quite close to finishing the property drawer I need with one hiccup:
When a value is placed in to the property field slot, I can get the reference But when the value is removed from the slot, it is as your screenshot showed, I can't get the reference back of the property that was just removed (because I need to update something on THAT removed object) Ins$$anonymous$$d I just get null for both Essentially I need a way to get a "cache" of the previous property value reference, it is the LAST missing piece of the puzzle I need to complete my drawer :P I'd be VERY grateful if you could help out! Thank you in advance :)
Sorry, I don't remember anymore how this stuff works. I can only give you a link to my project, maybe you can find solution there.
Project: AnimationPath Animator. Editor class: PathAnimatorEditor.cs
Your answer
Follow this Question
Related Questions
Unable to draw propertyfield in inspector for some classes, findProperty returns null. 0 Answers
Why are the children of my Serialized Property not being drawn? 1 Answer
How to indent EditorGUILayout.BeginToggleGroup() 1 Answer
C# public - dropdown selection? 3 Answers
Making a character's idle animation loop. (model and animation created in blender) 0 Answers