Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How to modify some field when save a MonoBehavior?

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

  1. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    369
    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

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    4,138
    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.