Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by MachCUBED · Apr 06, 2013 at 04:57 AM · c#stringguitextfont

How do I find font by passing a string?

Hi guys,

I have a few GUIText objects that have a set font. I also have an @2x version of the same font for retina displays (the title of the font is @2x). The following code is what I've written so far, even though it results in an error:

 ...
 oneUsePrice.font = oneUsePrice.font.name + "@2x";
 fiveUsePrice.font = fiveUsePrice.font.name + "@2x";
 tenUsePrice.font = tenUsePrice.font.name + "@2x";
 twentyUsePrice.font = twentyUsePrice.font.name + "@2x";
 ...

Because it assigns strings to a font value, it results in the following error:

 error CS0029: Cannot implicitly convert type `string' to `UnityEngine.Font'

So there needs to be a way to find the font with the name specified on the right side of each assignment, then set the left side to that font. Example in partial pseudocode:

 ...
 oneUsePrice.font = Font.FontWithName(oneUsePrice.font.name + "@2x");
 fiveUsePrice.font = Font.FontWithName(fiveUsePrice.font.name + "@2x");
 tenUsePrice.font = Font.FontWithName(tenUsePrice.font.name + "@2x");
 twentyUsePrice.font = Font.FontWithName(twentyUsePrice.font.name + "@2x");
 ...

For the record, the reason for wanting to use string parsing to solve this problem (as opposed to setting a Font class variable and using it to swap the @2x font) is because I want to have code that can be reused even in cases where different GUIText objects have different fonts.

Thanks in advance,

MachCUBED

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

1 Reply

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

Answer by Waz · Apr 06, 2013 at 06:47 AM

Like all assets in Unity, fonts can be accessed either by dragging them into a variable on an object in their hierarchy, or by using Resources.Load. Otherwise, Unity would have no way to know which of your assets you actually want in your game, and in what Scene you want them.

Comment
Add comment · Show 2 · 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 MachCUBED · Apr 06, 2013 at 05:29 PM 0
Share

I referred to the Unity script reference page:

http://docs.unity3d.com/Documentation//ScriptReference/Resources.Load.html

I then modified my code to look like this:

 oneUsePrice.font = Resources.Load(oneUsePrice.font.name + "@2x", typeof(Font));
             fiveUsePrice.font = Resources.Load(fiveUsePrice.font.name + "@2x", typeof(Font));
             tenUsePrice.font = Resources.Load(tenUsePrice.font.name + "@2x", typeof(Font));
             twentyUsePrice.font = Resources.Load(twentyUsePrice.font.name + "@2x", typeof(Font));

Which resulted in this error:

 error CS0266: Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.Font'. An explicit conversion exists (are you missing a cast?)

So I tried to add some casts, like this:

 oneUsePrice.font = (Font)Resources.Load(oneUsePrice.font.name + "@2x", typeof(Font));
             fiveUsePrice.font = (Font)Resources.Load(fiveUsePrice.font.name + "@2x", typeof(Font));
             tenUsePrice.font = (Font)Resources.Load(tenUsePrice.font.name + "@2x", typeof(Font));
             twentyUsePrice.font = (Font)Resources.Load(twentyUsePrice.font.name + "@2x", typeof(Font));

Which got rid of the errors. All that's left to do now is test it on an actual device and see if the cast works.

avatar image MachCUBED · Apr 07, 2013 at 12:02 AM 0
Share

The above works! The only problem is that the text no longer has the right font face, taking on a neutral white arial face while also taking on the target size.

It doesn't seem to be loading the desired font texture, even though the text is the right size.

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Fonts not displaying properly when loaded through script 0 Answers

C# Incrementing a String Array 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