Question: does binding to [SerializeReference] fields with UIElements in CustomEditor and/or CustomPropertyDrawer work?
If yes, then what I am doing wrong in the example:
using System;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
namespace RecreatingBug {
public interface ICommand {
}
[Serializable]
public class DebugLogCommand: ICommand {
public string thisDoesNotGetSaved = "text to print";
public DebugLogCommand() {
}
public DebugLogCommand(string message) {
this.thisDoesNotGetSaved = message;
}
}
public class EXAMPLE: MonoBehaviour {
public string thisGetsSaved;
[SerializeReference]
public ICommand command = new DebugLogCommand("asd");
}
[CustomEditor(typeof(EXAMPLE), true)]
public class CommandExecutorEditor: UnityEditor.Editor {
private VisualElement rootElement;
private void OnEnable() {
rootElement = new VisualElement();
}
public static void UpdateVisuals(SerializedObject serializedObject, VisualElement container, bool skipBinding = false) {
container.Clear();
// iterates through fields and constructs them
var iterator = serializedObject.GetIterator();
if (iterator.NextVisible(true)) {
do {
var propertyField = new PropertyField(iterator.Copy()) { name = "PropertyField:" + iterator.propertyPath };
if (iterator.propertyPath == "m_Script" && serializedObject.targetObject != null)
propertyField.SetEnabled(value: false);
container.Add(propertyField);
} while (iterator.NextVisible(false));
}
if (!skipBinding) {
container.Bind(serializedObject);
}
}
public override VisualElement CreateInspectorGUI() {
UpdateVisuals(serializedObject, rootElement, true);
return rootElement;
}
}
}
In the
UnityCsReference/Editor/Mono/UIElements/Controls/PropertyField.cs at 2019.3 · Unity-Technologies/UnityCsReference · GitHub I see that CreateFieldFromProperty()
does not have case for SerializedPropertyType.ManagedReference
– does this mean that it is not implemented yet?