Search Unity

TextMesh Pro Updating Font Size tags in TMPro Stylesheet on runtime?

Discussion in 'UGUI & TextMesh Pro' started by theembracedone, Jan 23, 2023.

  1. theembracedone

    theembracedone

    Joined:
    Jul 31, 2019
    Posts:
    22
    I have a style sheet set up in which I set up tags that set font size such as `<size=23px>`. Is it possible to somehow modify the stylesheet on runtime to change these sizes (even if I have to do it one by one)? It's for implementing a font size slider in the UI settings of the game (ideally I'd like to multiply each fontsize by a modifier).
    Thank you

    Edit: To clarify, I set the sizes via opening tags in the style sheets and I am looking to update these settings on runtime, NOT update the text size on the TextMeshProUGUI components!

    I'm able to reference my default styleSheet directly. According to docs.unity3d.com/Packages/com.unity.textmeshpro@1.3/api/… "GetStyle" retrieves "the Style matching the HashCode" (or name if I give it a string parameter). Once I get the style, however, I have very limited choice: docs.unity3d.com/Packages/com.unity.textmeshpro@1.3/api/… based on this, my best bet would be to query "styleOpeningDefinition".

    I am able to get the opening tags via styleOpeningDefinition, and then find and replace the relevant number that denotes the size in the "<size="50px">" tag but it's a readonly property so I cant set it afterwards.

    Here's the code:

    Code (CSharp):
    1. TMP_Text text = GetComponent<TMP_Text>();
    2. TMP_Style style = styleSheet.GetStyle("H1");
    3. openingTags = style.styleOpeningDefinition;
    4.  
    5. string[] size = openingTags.Split("px><cspace=");
    6. string _splitString = size[0].Replace("<size=", "");
    7. float _currentSize = float.Parse(_splitString);
    8. _currentSize *= multiplier;
    9.  
    10.  
    11. string _newTags = "<size=" + _currentSize + "px>" + size[1];
    12.  
    13. style.styleOpeningDefinition = _newTags;   //Property or indexer 'TMP_Style.styleOpeningDefinition' cannot be assigned to -- it is read only
    14. style.RefreshStyle();
    15.  
     
    Last edited: Jan 23, 2023