Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by stepan-stulov · Aug 28, 2015 at 08:30 AM · serializationscriptableobjectserializable

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:

  1. Add [FormerlySerializedAs] attribute with the current field name to the field.

  2. Rename the field (I use MonoDevelop refactoring)

  3. Re-save the assets.

  4. 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.

alt text

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.

alt text

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.

1.png (27.1 kB)
2.png (31.0 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Baste · Aug 28, 2015 at 09:15 AM 1
Share

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.

avatar image stepan-stulov · Aug 28, 2015 at 09:33 AM 0
Share

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.

avatar image stepan-stulov · Aug 28, 2015 at 12:04 PM 0
Share

I can now confirm that the Reimport didn't work, at least in most cases I've tried. Unfortunately...

avatar image _dns_ · Aug 28, 2015 at 02:44 PM 1
Share

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 :-)

1 Reply

· Add your reply
  • Sort: 
avatar image
5

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);
         }
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image david-mal · Jul 10, 2020 at 07:24 AM 0
Share

I used this to remove my FormerlySerializedAs Field. Thank you.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges