Binding to [SerializeReference] fields does not work?

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? :frowning:

Yeah, it looks like they haven’t implemented this yet. You can set it up yourself, but it requires some reflection hacks to get at some of the internals (the binding system doesn’t have a public interface). We have a mostly working implementation here: PiRhoUtilities/Assets/PiRhoUtilities/Editor/Elements/Reference/ReferenceField.cs at master · pirhosoft/PiRhoUtilities · GitHub (look at the ExecuteDefaultActionAtTarget method) but there are still a few oddities we haven’t worked out yet.

Thanks for confirming and thanks for the link – I see the possible workaround idea from your code and I am already doing a lot of reflection shenanigans so it wouldn’t be that difficult, BUT as a temporary solution I just used IMGUIContainer for every field that was “not working” and as IMGUI “binding” works, this is OK for my case now.
I am too tired to spend more hours for this now :slight_smile:

Hi @zledas_1 !

Thanks for bringing that up, ManagedReference didn’t exist when the PropertyField was made.
Could you report it as a bug? (https://support.unity3d.com/hc/en-us/articles/206336985-How-do-I-submit-a-bug-report-)

Thanks!

Thanks for confirming, @HugoBD-Unity
I will definitely submit a bug report.

Reported as “(Case 1206859) Binding UIElement to [SerializeReference] field does not work”.

1 Like

Any update on this, it still looks like PropertyField doesn’t play properly with SerializeReference. ie:

[Serializable]
public class ParamDefault
{
        [SerializeReference]
        public object Value;  // Property field doesn't display this value when bound.
}