Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

SerializeReference Attribute?

Discussion in '2019.3 Alpha' started by SonicBloomEric, May 15, 2019.

  1. jasonm_unity3d

    jasonm_unity3d

    Unity Technologies

    Joined:
    Mar 2, 2017
    Posts:
    37
    Sorry for the late response, was out on paternity leave.

    example:

    Code (CSharp):
    1.  
    2. [Serializable] class Boo
    3. {
    4.     public int a;
    5. }
    6.  
    7. class Foo : MonoBehaviour
    8. {
    9.     [SerializeReference] public Boo boo;
    10. }
    11.  
    12. void SomeMethodSomewhere( GameObject go )
    13. {
    14.     var foo1 = go.AddComponent(typeof(Foo)) as Foo;
    15.     var foo2 = go.AddComponent(typeof(Foo)) as Foo;
    16.     var boo = new Boo() { a =4 };
    17.  
    18.     foo1.boo = boo;
    19.     foo2.boo = boo;
    20.  
    21.     // At this point, foo1.boo == foo2.boo (same instance)
    22. }
    23.  
    but once the go gets saved and reloaded, if you where to fetch those two components back from the game object into foo1 and foo2, you would have: foo1.boo != foo2.boo

    that is because [SerializeReference] are serialized As Part of the 'root' object , in this case the Foo instances, and so can't be shared between root objects.

    But if Foo had two fields of type Boo, then one instance of Foo could have two fields point to the same Boo instance and that would be preserved during serialization/deserialization.
     
    Last edited: Jul 16, 2019
    ftejada and dzamani like this.
  2. jasonm_unity3d

    jasonm_unity3d

    Unity Technologies

    Joined:
    Mar 2, 2017
    Posts:
    37
    as of today, there are no plans for it that I am aware of.
     
  3. jasonm_unity3d

    jasonm_unity3d

    Unity Technologies

    Joined:
    Mar 2, 2017
    Posts:
    37

    yes, just add [SerializeReference] to public List<Animal> animals;
     
  4. jasonm_unity3d

    jasonm_unity3d

    Unity Technologies

    Joined:
    Mar 2, 2017
    Posts:
    37
    That sounds a like a bug, please log an issue so that we can track it and resolve it asap. thanks!

    https://unity3d.com/unity/qa/bug-reporting
     
  5. taylank

    taylank

    Joined:
    Nov 3, 2012
    Posts:
    88
    Experiencing the same thing. And the editor crashes every other minute when trying to work with this feature.
     
  6. stopiccot_onthespot

    stopiccot_onthespot

    Joined:
    Oct 1, 2016
    Posts:
    58
    Do you plan guys to add support to serialize template classes without declaring specialized type?
    Code (csharp):
    1. [Serializable]
    2. public class TemplateClass<T>
    3. {
    4.     public T val;
    5. }
    6.  
    7. [Serializable]
    8. public class TemplateClassInt : TemplateClass<int> { }
    9.  
    10. public TemplateClass<int> intClass1;
    11. public TemplateClassInt intClass2;
    intClass1 is not shown in the inspector
     
  7. jasonm_unity3d

    jasonm_unity3d

    Unity Technologies

    Joined:
    Mar 2, 2017
    Posts:
    37
    At the moment, there are no plans to support generics.
     
  8. jasonm_unity3d

    jasonm_unity3d

    Unity Technologies

    Joined:
    Mar 2, 2017
    Posts:
    37
    please log bug reports so that we can address instabilities. thank you.
    https://unity3d.com/unity/qa/bug-reporting
     
  9. FabioFZynga

    FabioFZynga

    Joined:
    Sep 24, 2018
    Posts:
    7
    The doc link is broken, and searching for SerializeReference in the Manual or Scripting API pages for 2019.3 returns no results :/
     
  10. FabioFZynga

    FabioFZynga

    Joined:
    Sep 24, 2018
    Posts:
    7
    This looks beautiful! I've tried it out (exact same code) in Unity 2019.3.0a11, the Trees field appears in the Editor but it is broken! (just shows a list of labels with no ability to add or edit anything) Is that normal?
     
  11. AndrewKaninchen

    AndrewKaninchen

    Joined:
    Oct 30, 2016
    Posts:
    80
    Might I ask why? It really bothers me, as even Unity falls into this trap from time to time. Addressables and UnityEvents, for example, have clunky workarounds because of that. And we know it is possible from a usability standpoint. Odin Serializer is very much capable of working in tandem with Unity's serializer where the second can't, so I can't imagine why Unity's serializer couldn't use a hybrid approach to it, even if using the current way to do it is theoretically impossible. You'd just have to do it a different way wherever it is required.

    Using generics in Unity is a nightmare the way it currently works (as in, it doesn't). In my last project I had to do so many workarounds to be able to serialize everything I needed that in the end I just gave up and refactored everything to go with Odin Serializer so my code could look like it should.
     
    taylank, Jes28, M_R and 1 other person like this.
  12. stopiccot_onthespot

    stopiccot_onthespot

    Joined:
    Oct 1, 2016
    Posts:
    58
    Such a shame in 2019...
     
  13. mykillk

    mykillk

    Joined:
    Feb 13, 2017
    Posts:
    50
    This usage case is very close to what I am using this for. However, I could not get it to work at first. The elements in m_Trees were serializing references correctly, but the [SerializeReference] fields in the derived classes were not being serialized at all.

    My Node class did not derive from anything, so noticing that in this example the base class was an interface I tried doing that and converting all my references in the derived classes to interface references. Then all the serialization worked as I had hoped!

    Is it really the case that a class can't serialize its own type reference? It's not a problem to add the extra interface but I was wondering why using an interface "middle-man" makes this work.
     
    Last edited: Aug 10, 2019