x


How to find a font via script?

Another one that should be really easy, but which is mysteriously elusive to me. Okay, so I have imported a custom font through the TTF Importer, which was very easy. Now how do I reference that font through code in order to actually tie it to some custom GUIStyles, etc? GameObject.Find nor GetComponent seem to be the right thing.

more ▼

asked Mar 16 '10 at 09:53 PM

x4000 gravatar image

x4000
504 23 25 40

(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest
var someFont : Font;
var style : GUIStyle;
style.font = someFont;
more ▼

answered Mar 17 '10 at 12:02 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

This is just creating an empty font, yes? Unless I am misunderstanding something. I want to find a reference to an existing font that I have already imported through the Unity editor -- but I want to find access to it in code, so that I can do essentially what you are describing in your second two lines. That part is straightforward, but on that first line of your code I want to find a reference to an existing font rather than declaring a new, empty one.

Mar 17 '10 at 12:43 AM x4000

@x4000: It's not creating a font, it's creating a public variable that you use to reference the font. (I forgot to mention the part about those being public variables. ;) ) You can drag the font you want onto the public variable.

Mar 17 '10 at 01:29 AM Eric5h5

Ah, okay -- the public variable with dragging through the interface solves it. That's good enough for making an ongoing reference I can then use later. Thanks!

Mar 17 '10 at 02:26 AM x4000
(comments are locked)
10|3000 characters needed characters left

Place the font in the Resources folder and use Resources.Load to load assets dynamically.

more ▼

answered Mar 16 '10 at 11:48 PM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

That is really helpful, actually, but not quite what I am looking to do. In this case I already have the font imported just fine into the game, but I am looking to access the imported version of it that I can then render. In other words, it is called "Regular" as a font. But, I can't seem to set a Name for it in the editor that I can see (a lot of grayed out controls), and I don't see any methods on the Font class for finding a reference to an existing font. I see how GameObjects can be looked up, but that does not seem to work for fonts...

Mar 17 '10 at 12:40 AM x4000
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x241
x208

asked: Mar 16 '10 at 09:53 PM

Seen: 3557 times

Last Updated: Mar 16 '10 at 09:53 PM