I wrote this code thinking that BeginChangeCheck would notify me when a “new event” is written on the text field of the EventReference, or doubleclicked from the “Magnifying glass” context menu.
Actually, it does react when writting manually in the text field (which is expected), but it does not react when doubleclicking in the context menu showing all the events. Actually, it triggers when you CLICK the magnifying glass itself, making this code not working at all.
Sorry for the late response and thank you for sharing the code!
From what I can see, the behaviour you are getting is expected.
BeginChangeCheck/EndChangeCheck only picks up changes that happen while the field is being drawn in the Inspector.
When you select an event from the FMOD Event Browser, the value is updated afterwards, so the change doesn’t get detected by that check.
I think a more reliable approach would be to treat the EventReference like any other serialized struct and compare its current value with a cached one.
Here’s a small example showing how to detect changes when an event is chosen from the FMOD browser:
A test component with an EventReference:
using FMODUnity;
using UnityEngine;
public class TestEventComponent : MonoBehaviour
{
public EventReference eventReference;
}
A custom Inspector that detects when the event changes:
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(TestEventComponent))]
public class TestEventComponentEditor : Editor
{
private SerializedProperty eventRefProp;
private string lastEventPath;
private void OnEnable()
{
eventRefProp = serializedObject.FindProperty("eventReference");
if (eventRefProp != null)
{
var pathProp = eventRefProp.FindPropertyRelative("Path");
lastEventPath = pathProp != null ? pathProp.stringValue : null;
}
}
public override void OnInspectorGUI()
{
var component = (TestEventComponent)target;
serializedObject.Update();
// Draw EventReference
EditorGUILayout.PropertyField(eventRefProp, new GUIContent("Event Reference"));
// Read new Path
var pathProp = eventRefProp.FindPropertyRelative("Path");
string currentPath = pathProp != null ? pathProp.stringValue : null;
serializedObject.ApplyModifiedProperties();
// Detect changes
if (currentPath != lastEventPath)
{
lastEventPath = currentPath;
if (string.IsNullOrEmpty(currentPath))
{
Debug.Log("EventReference CLEARED");
}
else
{
Debug.Log("EventReference changed to: " + currentPath);
}
}
}
}
Hope this helps! Let me know if it works on your end.