Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Changing Object Type and Keep Data with attribute

Discussion in 'Extensions & OnGUI' started by BinaryCats, Mar 12, 2018.

  1. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    126
    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:
    256
    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:
    126
    Thank you very much