- Home /
How to trigger actual field renaming In embedded instances of ScriptableObject asset files while using [FormerlySerializedAs] attribute?
Unity advertises the [FormerlySerializedAs] attribute as the final solution for renaming serializable fields. Here are the steps that are officially recommended to do:
Add [FormerlySerializedAs] attribute with the current field name to the field.
Rename the field (I use MonoDevelop refactoring)
Re-save the assets.
Remove the [FormerlySerializedAs] attribute.
The problem I have is between (3) and (4). It doesn't quiet work. Here is my set-up:
The deepest instance's class:
[Serializable]
public class B // Before
{
[SerializeField] private string oldString1;
[SerializeField] private string oldString2;
}
The middle-depth instance's class:
[Serializable]
public class A
{
[SerializeField] private B _b;
}
The actual ScriptableObject instance's class:
[CreateAssetMenu]
public class MyAsset : ScriptableObject
{
[SerializeField] private A _a;
}
Here is how the inspector and also the actual (text-serialized) asset file look. I combined two screenshots in one because Unity Answers limits number of attachments.
Now what I do is follow the official steps recommended by Unity for the oldString1 and oldString2 fields of B class:
[Serializable]
public class B
{
[FormerlySerializedAs("oldString1")] [SerializeField] private string newString1;
[FormerlySerializedAs("oldString2")] [SerializeField] private string newString2;
}
In the inspector, as expected, what I see is the the new variables holding old values. So far so good. But the actual asset file (I used MacOS preview on pressing Space) still holds the old field names! Things I've tried more than multiple times: saving scene, saving project, refreshing assets, reimporting this asset, reimporting all assets, relaunching Unity. To no avail.
Needless to say, if I now follow the official step (4) and remove the now-supposedly-useless [FormerlySerializedAs] attributes, the values are lost. They are lost, because now there is inconsistence between field names in the script and field names in the asset file.
Have I discovered a bug, or am I missing something? Interestingly enough, while my example doesn't work, simple scenarios with game objects originates in a scene and no embedded nesting all work just fine. Both the inspector and the actual file would hold the new field names.
How to do I trigger the actual field renaming in the embedded instances of file-serialized ScriptableObject assets?
The only thing that helped is manually go into the field in Inspector, edit it and press Enter. Unfortunately it's not an option for me, because of incredible amount of assets the field renaming is involving. I want automation, not opening the inspector by hand on a thousand files.
Big thanks for a thorough answer.
The only way I know of that will guarantee that the correct value is stored is to change the value of the field. You could also write a tool that manually goes through the yaml file and changes the field's serialized name, but that's understandably not desireable.
Have you tried reimporting the asset? As in right-click/reimport?
I don't really have an good answer here. I generally just let the attribute hang around forever.
Thank you for your comment. I haven't tried it yet and will do so soon. The problem with manually editing the field is with multiple instances (and I do mean $$anonymous$$ULTIple:) The problem is that they may all have different values, which means (again, oh well) losing data.
But here is a trick I came up with: along with rena$$anonymous$$g a field and adding the [FormerlySerializedAs] attribute also add a dummy bool serialized trigger-variable. It won't interfere with meaningful values. I then multiselect all the assets of the desired type (t:$$anonymous$$yAsset) and hit the checkbox of the boolean trigger field in inspector and save project. This triggers the serialization that I need. Then I simply remove both the [FormerlySerializedAs] and the dummy boolean trigger field. I will probably post either your solution (once I tried it and if it works) or $$anonymous$$e, if nothing better gets posted.
Thanks again.
I can now confirm that the Reimport didn't work, at least in most cases I've tried. Unfortunately...
Hi, what I usually do is add an OnValidate() script to the object (with #if UNITY_EDITOR) and use EditorUtility.SetDirty(gameobject or scriptable object) to force the object to be saved. It's also possible to assign a new field with the value from an old one here and emulate the effect of the attribute like this.
Selecting the prefab/scriptableobject in the project window will trigger OnValidate so using a search + multiselecting can be enough to modify lots of objects. I've had problem setting the "dirty" flag sometimes though during OnValidate (= a Debug.Log shows the script was executed but the dirty flag didn't registered so the object is not saved, but I still couldn't find out when exactly it works or not). It's quite quick to try though :-)
Answer by jice_nolaroads · Mar 25, 2018 at 10:28 PM
better late than never ... with this snippet of code you can get manual set dirty feature (usefull to trigger re-serialization after formerlyserializedas refacto)
HowTo: make a find on your type ("t:MyAsset" in fuzzy finder), select all elements found, right click -> tools -> set dirty, then make a ctrl+s and all yours selected asset where correctly saved with refactored names.
[MenuItem("Assets/Tools/Set Dirty")]
private static void SetDirty() {
foreach (Object o in Selection.objects) {
EditorUtility.SetDirty(o);
}
}
I used this to remove my FormerlySerializedAs Field. Thank you.
Your answer
Follow this Question
Related Questions
Serializing specialized subclasses of generic classes not working 1 Answer
Make the same serialized class as a ScriptableObject? 1 Answer
Board game minimax AI implementation 0 Answers
What are the pros and cons of ScriptableObjects vs. JSON for data files? 2 Answers
"Unbroken Reference" problem when using a custom Editor to Save/Load a ScriptableObject Asset 0 Answers