Search Unity

TextMesh Pro TMP_Settings are readonly

Discussion in 'UGUI & TextMesh Pro' started by dadude123, Jul 27, 2018.

  1. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    I generate a custom spriteatlas at runtime,
    after that I create a TMP_SpriteAsset so it can be used in TextMeshPro.

    It works just fine when I directly assign it to an existing text instance.
    But now I want every text in the game to have that TMP_SpriteAsset set.

    I can't set it as default in the tmp settings at edit time because the atlas is dynamic.
    And modifying TMP_Settings is not possible.

    What can I do?
    Do I have to maintain a list of literally every object that has a TMP_Text component and set the spriteasset?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I have had a few users looking to do something similar.

    If you are using the version of TextMesh Pro provided via the package manager (TMP UPM version), then you have access to source code and could certainly modify the necessary properties in the TMP_Settings.cs file to allow you to assign a different sprite asset.

    However, even with this change, the text objects won't have any idea the properties of the TMP Settings have been changed. In Editor, TextMesh Pro has various callbacks to let the text objects know that properties of things like font assets, sprite assets, settings, etc. have been changed but not in the runtime.

    Thinking out loud, after your make the assignment change of the sprite asset, the text objects need to be notified of this change so they can be regenerated. As such, either your code or some new code on my end would need to end up calling SetAllDirty() on those text objects. When it comes down to it, someone needs to track all these text objects and if you are going to track these text objects, then you may as well simply assign the new sprite asset to these object directly.

    I'll think about this but I believe the best solution, might be for you to create some text object manager where each newly created text object registers itself. Then when you change the sprite asset, you would call a function part of this manager that would set the sprite asset property accordingly.
     
  3. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    I see, that makes sense, I guess it really is the easiest way to do it.
    I might be able to change my scene setup a bit so the atlas generator runs first, then I can at least change all prefabs, and only have to take care of text elements that already exist in the scene.

    Changing the code / creating a sort of text manager is actually a pretty good idea.
    That would pretty much do what I need, making all text objects register themselves with the manager no matter how they are created. Thanks!
     
    Stephan_B likes this.