- Home /
Change font style via Script in unity
Hi, i have script like that:
void OnGUI() {
if(selectScreen){
GUIStyle centeredStyle = GUI.skin.GetStyle("Button");
centeredStyle.alignment = TextAnchor.MiddleCenter;
// Next
if( GUI.Button(new Rect(Screen.width/2 + 65, Screen.height-100, 120, 50), "Next") )
{
activeObjectIdx++;
if( activeObjectIdx >= objects.Length )
activeObjectIdx = 0;
SetActiveObject(objects[activeObjectIdx]);
}
Can you help me how to change font style to other? Please don't write "find solution in google" etc. I was looking for that, but I didnt find the right answer. My problem is that I dont know where I should put on code in my script and what it should be?
Thank you
Well, first of all you should REALLY switch to the 'new' UI system from 4.6 ... like, really. ^^
You would be able to manipulate text-type objects with a 'font' property. You would only need this kind of code:
Adding the good library:
using UnityEngine.UI;
In your variables declaration:
public Text myTextIWannaChangeFont;
public Font myFontIWannaUse;
In any of your functions, where you want to do it:
myTextIWannaChangeFont.font = myFontIWannaUse;
For the old GUI system ... i don't know, i barely used it, sorry :D
The OnGUI system is VERY SLOW, switch to the new UI system please. OnGUI is perfect for some debugging, perhaps? Don't touch it! ;)
Your answer
Follow this Question
Related Questions
GUIStyle and setting font's 2 Answers
Unity and font licensing 0 Answers
Trouble with fonts. 0 Answers
Use Windows Fonts in Game 1 Answer
Font.CacheFontForText is 87.3% in the profiler and causes a HUGE lag spike 1 Answer