Search Unity

Changing Object Type and Keep Data with attribute

Discussion in 'Immediate Mode GUI (IMGUI)' started by BinaryCats, Mar 12, 2018.

  1. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Hello,


    There is an annoying problem with unity, than if you change the type of a serialized field to an array of that type it looses it data.
    i.e.
    int a = 5

    changed to
    int[] a = new int[0]

    I think it would be great if it was possible to keep the input data, indexed at 0 in the array, i.e.
    int[] a = new int[1] {5}

    Is it possible to create a Attribute similar to [FormerlySerializedAs] To grab the serialized value and manipulate the new SerializedProperty? Is this possible with a standard PropertyAttribute?

    This could additionally be of use for changing the type of an object completely, Currently I find myself swapped with changing Strings to StringScriptableObject type. If I could utilise the above I could create new StringScriptableObjects and storing their old value in there.

    Thanks

    (I guess this might be the wrong forum, but I find this forum usually have people that know about PropertyAttribute's and knowledge of serialization)
     
  2. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    There is a feature in the works called Script Data Upgrade. It allows developers to define custom upgrade classes when they change the serialized layout of a class and perform custom actions, basically exactly what you are asking for. The new approach is probably much better than asking Unity to implement predefined behaviour such as using an int value as the first in an array when upgrading, because it all depends on our requirements. With the new system we can implement this in a few lines of code (which later can also be removed again).
     
    BinaryCats likes this.
  3. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Thank you very much