Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SerializeReference and UIElements issues

Discussion in '2019.3 Beta' started by engzs, Sep 3, 2019.

  1. engzs

    engzs

    Joined:
    Jul 29, 2017
    Posts:
    1
    Changes in PropertyField cannot be saved

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [CreateAssetMenu]
    4. public class SO : ScriptableObject
    5. {
    6.     public string a;
    7.  
    8.     [SerializeReference]
    9.     public IInt[] bbc;
    10.  
    11.     //[SerializeField]
    12.     //public IntValue[] bbc;
    13. }
    14.  
    15. public interface IInt
    16. {
    17.  
    18. }
    19.  
    20. [System.Serializable]
    21. public class IntValue : IInt
    22. {
    23.     public int Value;
    24. }
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.UIElements;
    3.  
    4. public class SOWindow : EditorWindow
    5. {
    6.     [MenuItem("Tools/Window")]
    7.     public static SOWindow GetWindow()
    8.     {
    9.         var w= GetWindow<SOWindow>();
    10.         w.Show();
    11.         return w;
    12.     }
    13.  
    14.     private SO so = null;
    15.  
    16.     void OnEnable()
    17.     {
    18.         if (so == null)
    19.         {
    20.             so = AssetDatabase.LoadAssetAtPath<SO>("Assets/so.asset");
    21.         }
    22.         so.bbc = new IInt[] { new IntValue() { Value = 100 } };
    23.         //so.bbc = new IntValue[] { new IntValue() { Value = 100 } };
    24.  
    25.         var serSO = new SerializedObject(so);
    26.         var bbc = serSO.FindProperty("bbc");
    27.         var bbcProp = bbc.GetArrayElementAtIndex(0).FindPropertyRelative("Value");
    28.         var f = new PropertyField();
    29.         f.bindingPath = bbcProp.propertyPath;
    30.         f.Bind(serSO);
    31.         rootVisualElement.Add(f);
    32.  
    33.     }
    34. }
    35.  
    36.