0
\$\begingroup\$

I have a GameObject with a Text Mesh Pro component.

I want to check if the Font applied to the Text Mesh Pro component is a particular font (let's say it's Comic Sans)

I can get the font name by first using Get Component and then calling Get Font.

But the output of Get Font is the font asset itself. So I can't use a simple string Equality (or Contains) to check if the font matches Comic Sans.

Can I turn the output of Text Mesh Pro Get Font into a string? Or is there another way to check if the font matches a particular font?

Thanks!

\$\endgroup\$
4
  • \$\begingroup\$ So supposed I create a copy of Comic Sans, but I rename it to "Papyrus" and then assign it to a TMPro text. Would you need to find it? Or the other way around: I take the font Papyrus, but I rename it to "Comic Sans" and then assign it to a TMPro text. Do you need to not find this? \$\endgroup\$
    – Philipp
    May 31, 2022 at 6:48
  • \$\begingroup\$ In my case, I am using Prefabs that can have text in either Comic Sans or Papyrus. Then I want to check each GameObject's text field to see which font it is set to... and if it's Papyrus, I want to programmatically vary its glow parameters so that it appears to pulsate. \$\endgroup\$
    – kanamekun
    May 31, 2022 at 7:21
  • \$\begingroup\$ This seems like a weird requirement. Isn't it rather that some text objects in your UI have a specific meaning, and that specific meaning is supposed to be visualized by the font being Papyrus AND the text being glowing? In that case it would be a good idea to have all those text objects inherit from the same prefab. Now you can style that prefab the way you want and all those changes get propagated to all the objects. When I build UIs with UnityUI, then I always create prefabs for all my UI widgets, so I can style them all at once via the prefabs. \$\endgroup\$
    – Philipp
    May 31, 2022 at 7:49
  • \$\begingroup\$ This isn't for UI... I am making an educational game to teach reading. The text is a core part of my GameObject. In any case, just trying to check the font on my Text... hoping it's possible! \$\endgroup\$
    – kanamekun
    May 31, 2022 at 7:56

1 Answer 1

1
\$\begingroup\$

You can get the name of the font asset with GetComponent<TextMeshProUGUI>().font.name. That will return the name of the font asset in the same way as it appears in the "Project" window.

\$\endgroup\$
5
  • \$\begingroup\$ How do i check if the font is say, Papyrus? I tried a string Equality and a string contains, but neither worked... \$\endgroup\$
    – kanamekun
    May 31, 2022 at 7:59
  • \$\begingroup\$ Have you tried to log the string to the console first to see what's actually in it? \$\endgroup\$
    – Philipp
    May 31, 2022 at 8:08
  • \$\begingroup\$ I don't think it's a string? When I use the Text Mesh Pro Get Font call, I think what is output is the TMP FontAsset. Here's what is output when I log the output to the console (this is the name of the font I'm using): KGRedHands SDF (TMPro.TMP_FontAsset) \$\endgroup\$
    – kanamekun
    May 31, 2022 at 8:12
  • \$\begingroup\$ Ah I think I found it! TMP_Font Asset Equals... it lets you check one TMP Font Asset against another! \$\endgroup\$
    – kanamekun
    May 31, 2022 at 8:23
  • \$\begingroup\$ As the answer says, GetComponent<TextMeshProUGUI>().font.name is the name of the font (string type) rather than the font asset, you can simply compare it with “Papyrus”. \$\endgroup\$
    – Mangata
    May 31, 2022 at 8:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .