Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Renaming fields in code without losing associated inspector values

Discussion in 'Scripting' started by arturaz, Nov 12, 2014.

  1. arturaz

    arturaz

    Joined:
    Mar 28, 2013
    Posts:
    30
    Hi there.

    So now we have this problem with unity - once you name your serialized field, the name is set in stone, unless you want to lose all the values you set on your objects in the inspector.

    The problem lies in fact that unity uses field name as identifier for the key -> value pair in the metadata. For example if I look at

    Code (CSharp):
    1.   public class ProjectileWeaponBinding : WeaponBinding {
    2.     [SerializeField, UsedImplicitly] float destroyProjectileAfterS;
    3.   }
    I can see this looks like this in .prefab file:

    Code (CSharp):
    1. --- !u!114 &11400000
    2. MonoBehaviour:
    3.   m_ObjectHideFlags: 1
    4.   m_PrefabParentObject: {fileID: 0}
    5.   m_PrefabInternal: {fileID: 100100000}
    6.   m_GameObject: {fileID: 100000}
    7.   m_Enabled: 1
    8.   m_EditorHideFlags: 0
    9.   m_Script: {fileID: 525689469, guid: 7ccebd5568ed1874587f234c8f3406f7, type: 3}
    10.   m_Name:
    11.   m_EditorClassIdentifier:
    12.   destroyProjectileAfterS: 5
    13.  
    Thus if I rename the variable in code, value 5 is lost, because the names do not match.

    What I suggest is an additional attribute, which if present would be used as a name for serialized field, instead of field name.

    Code (CSharp):
    1.   public class ProjectileWeaponBinding : WeaponBinding {
    2.     [UnitySerializeField("destroyProjectileAfterS"), UsedImplicitly] float anythingCanGoHereNow;
    3.   }
    The code change from unity side should be pretty trivial as well.

    What do you think?
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    That really doesn't make a lot of sense. The names in the inspector are the variable names, not abstractions. If you change the name of public variable, it needs to change in the inspector as well. Having a variable name for a variable would just cause problems. This really isn't a problem with Unity.

    If you want to abstract values in some way, just write an editor script then you can apply them however you want in the class.
     
  3. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    It's complex solution that would eat CPU cycles constantly in the editor to solve a problem that really doesn't happen a lot. I understand the pain of it, but you'd be better off writing some kind of script to do it instead of asking Unity to make the change.
     
  4. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    Someone from Unity actually mentioned it already. They are trying to detect wether you changed names or not, but I don't know what the result was.
     
  5. arturaz

    arturaz

    Joined:
    Mar 28, 2013
    Posts:
    30
    What? What unity is doing now behind the scenes is pretty much this:

    Code (CSharp):
    1. serialize(MonoBehaviour) {
    2.   foreach (public and [SerializeField] marked field as field) {
    3.     saveData(field.name, field.value);
    4.   }
    5. }
    What they would need to do is:

    Code (CSharp):
    1. serialize(MonoBehaviour) {
    2.   foreach (public and ([UnitySerializeField] or [SerializeField] marked field) as field) {
    3.     var name = (get unityserializefield).name ?? field.name;
    4.     saveData(name, field.value);
    5.   }
    6. }
    And vice-versa for deserializing. Not that hard, eh?

    Yes. But the actual storage field name in serialized data does not concern you. And it would solve the problem of losing your data when you change the variable name.

    Care to explain how it would CONSTANTLY eat CPU cycles in editor? :))
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Unity 5 has a [FormerlySerializedAs] attribute which you can use for renaming stuff.

    --Eric
     
  7. arturaz

    arturaz

    Joined:
    Mar 28, 2013
    Posts:
    30
    Neat!
     
  8. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    54
    A_Marraff and angelonit like this.
  9. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    A little necro here,
    But since we need to re-save every asset that is using [FormerlySerializedAs] to be able to remove this attribute safely, how can we re-save everything in project ?
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,611
    koirat likes this.