- Home /
How to load font into GUIStyle at runtime (not using inspector)?
Hello friends, I'm wondering: I am using a GUIStyle (private, not public). I want to use a specific font, but I am not certain on how to load this font from a folder within my project. I know I can use something like
GUIStyle style1;
void Awake()
{
style1.font = "This is where I am not sure what to do."
}
I know this is probably something simple, but that's why I am here, to learn. Thanks in advance for any help.
Answer by clunk47 · Aug 20, 2013 at 04:05 AM
Have a look at Resources.Load
Make sure you load as a font like so:
style1.font = (Font)Resources.Load("FontName");
You need to create a folder named 'Resources' in your project for this to work. The above statement assumes the font is named 'FontName' and the font style is in the root of your resources folder. If you have a folder named 'Fonts' inside your Resources folder, you would use
style1.font = (Font)Resources.Load("Fonts/FontName");
Thanks for the prompt reply! All I need was the reference to Resource.Load, but thank you for sharing an example anyway. Voted up and checked answer as correct. Thanks again @clunk47!
Always happy to help. +1 as a welcome to the community, and for asking a question with detail and proper formatting. Happy Developing!
I have done exactly like this but got : error CS0120: An object reference is required to access non-static member `UnityEngine.GUIStyle.font' do you know what should i do ?
Your answer
Follow this Question
Related Questions
GUIStyle.CalcSize giving wrong answer 0 Answers
Outline Text From TTF. 2 Answers
How can I make a Font from a TTF file stored in memory? 0 Answers
How do I display Burmese Unicode & Zawgyi in Unity? 0 Answers
Replacing font of TextMesh issue 1 Answer