- Home /
How to change text style of TMP in script?
I would like to change the text style(which is defined in the style sheet) of the TMP component in code. It seems that there is no method for changing text style during runtime.
Changing the name of textStyle was not working. It changes only the name of the text style.
tmp_text.textStyle.name = "H1";
How can I change TMP_Style of TMP_Text in script?
Answer by logicandchaos · Sep 08, 2021 at 02:29 PM
I copied and pasted your exact question into google:
1st result:
https://www.youtube.com/watch?v=-U29sx9x3Zk
more:
https://forum.unity.com/threads/textmesh-pro-change-bold-underline-at-runtime.515623/
google is your friend. unity has all their documentation online.
They are about changing text and font style, not 'text style'.
The reason why I have answered is that I couldn't search the answer on google.
And I have already looked into the Unity documentation which doesn't have any example and detailed description.
Answer by socialtrens · Sep 09, 2021 at 11:47 AM
Short Answer: Currently no. No easy way to do it.
Long Answer: But you can do it in a hacky way.
You can get the OpeningTags to a StyleTag by calling the following function: TMP_StyleSheet.GetStyle("[StyleName]").styleOpeningDefinition
(with TMP_StyleSheet being a reference to the used TMP-StyleSheet).
So a possible solution is to extract the StyleName from your string (e.g. "(...text) <style=Example> (text...)"
would become ("Example")
and feed it to the function above. Regular Expressions can help to extract the StyleName from your string. Then replace the whole tag with whatever the function returns (e.g. "<size=125%>"
). (Note: It returns Null if the tag does not exist). Then do the same with the closing tag.
I don't know if it's answered your question. Or maybe I'm miss understood your question.
Your answer
Follow this Question
Related Questions
Unity crashes at writing TextMesh? 0 Answers
Adjust text file reading/detection 0 Answers
Draw TextMesh Pro Text to a Texture2D at run time 0 Answers
TextMesh Pro visible in editor but not in game window. 2 Answers