Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

Are There Any Plans To Support HTML5 In Unity? 7 Answers

Fonts have black textures 1 Answer

How do I add a shader to a font material? 2 Answers

Pressing play Causes Re-instancing of material (Editor) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges