• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by x4000 · Mar 16, 2010 at 09:53 PM · fontfind

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Eric5h5 · Mar 17, 2010 at 12:02 AM

var someFont : Font;
var style : GUIStyle;
style.font = someFont;
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image x4000 · Mar 17, 2010 at 12:43 AM 0
Share

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.

avatar image Eric5h5 · Mar 17, 2010 at 01:29 AM 0
Share

@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.

avatar image x4000 · Mar 17, 2010 at 02:26 AM 0
Share

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!

avatar image
2

Answer by Jaap Kreijkamp · Mar 16, 2010 at 11:48 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image x4000 · Mar 17, 2010 at 12:40 AM 0
Share

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...

avatar image
0

Answer by cedw032 · Jun 24, 2013 at 05:52 AM

var font:Font = Instantiate( Font.FindObjectsOfTypeAll( Font )[0] );

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TwoWholeWorms · Jan 30, 2015 at 04:35 PM 1
Share

Font.FindObjectsOfTypeAll() is now deprecated. As of Unity 5, the C# equivalent of the above code is as follows: Font font = Instantiate (Resources.FindObjectsOfTypeAll (typeof (Font))[0]) as Font;

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Is there a way to measure the pixel with/height of a string with a given font? 2 Answers

Finding assets from script 2 Answers

GameObject.Find() work on inactive objects 16 Answers

All subsequent instantiated objects = auto-named with suffix (Clone)? 1 Answer

Finding class where current GameObject is assigned to as a var 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges