How to set font for user interface text in Unity 3D programmatically

In my game, I have UI text that I called a β€œlabel” and I want to set its font programmatically. I tried to do this:

label.GetComponent<Text>().font="Arial"; 

I get an error because the font attribute does not want a string except the font. So, how can I customize the font on Arial programmatically?

+6
source share
2 answers

It works:

 label.GetComponent<Text> ().font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font; 
+14
source

try creating a public variable of type Font in the editor.

 public Font myNewFont; 

then you can do something like

 label.GetComponent<Text>().font= myNewFont; 

Failed to verify this, but I think it should work, here is a very similar question ... How do I change the font type in Unity?

+2
source

Source: https://habr.com/ru/post/989225/


All Articles