Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

How to modify some field when save a MonoBehavior?

Discussion in 'Prefabs' started by watsonsong, Jul 31, 2019.

  1. watsonsong


    May 13, 2015
    I want to change some field of a MonoBehavior when I save it.
    For example I have a UnityEngine.UI.Text. I want to add I18N support. That mean I can switch my language and change both the text and font of the Text. But when I save the GameObject, It need change to text and font field to null. This make sure the UI will not dependent on any specify language.

    A MonoBehavior can saved in different ways. It usually saved into a prefab, but into a scene is also a reasonable way. Even more I can create a GameObject as a sub-asset of other assets.

    And after I save it, I need revert and set the font and text field correctly, in order to preview.
    I investigate some API like PrefabUtility.prefabInstanceUpdated, AssetPostProcessor, AssetModificationProcessor, ISerializeableCallback. All of them seems not suitable for this.

    Is there any suggestion?
  2. Lurking-Ninja


    Jan 20, 2015
    I wouldn't do that. First, it's an extra step you would need to do. Second, just use the defaults as fallback values. Define what is your fallback language (en/us for example), put everything in place with this language and then change these if it's needed.

    Reason: if you mess up something and you don't have translations for certain things, you will see English text not nothing. It's an inch better.