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 08, 2013 at 06:07 AM · c#javascriptfontcasting

Fonts not displaying properly when loaded through script

Hi guys,

After resolving the font parsing issue that I asked about over here a few days ago, I've noticed that depending on whether I load my new fonts in C# or Javascript, the loaded fonts either display properly, or as distorted text.

Here is the C# script that I use for setting the fonts of my GUIText objects:

 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));

The above code works perfectly fine. All of my game's @2x font files are stored in Assets/Fonts/Resources/ and thus can be found by Resources.Load. Although the material that I use for all of my fonts is located in Assets/Fonts/Materials, it shouldn't be a factor since the code works as intended when implemented in C#.

Javascript, however, is a different story. This is the JavaScript code that I'm using:

 scoreLabel.font = Resources.Load(scoreLabel.font.name + "@2x", Font) as Font;
 craneUseLabel.font = Resources.Load(craneUseLabel.font.name + "@2x", Font) as Font;
 timerLabel.font = Resources.Load(timerLabel.font.name + "@2x", Font) as Font;
 bonusTimerLabel.font = Resources.Load(bonusTimerLabel.font.name + "@2x", Font) as Font;

The above code results in the text that is supposed to be displayed in the loaded font being completely distorted and illegible. It's as if the cast isn't working right, or the font is somehow not interfacing with the material properly when JavaScript is used. Is the issue with the casting method that I've used in my JavaScript, or is it due to something else?

MachCUBED

Comment
Add comment · Show 5
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 Loius · Apr 08, 2013 at 07:48 AM 1
Share

no idea :(

if all else fails, you can make a C# function that loads up all your fonts for your JS function to call into. hooray, workarounds.

avatar image MachCUBED · Apr 08, 2013 at 05:24 PM 0
Share

.font of the various labels is the font member (Font font) of GUIText. And how do I make sure that the following line declares the right-hand side as a Font ins$$anonymous$$d of an Object?

 scoreLabel.font = Resources.Load(scoreLabel.font.name + "@2x", Font) as Font;

$$anonymous$$aybe the syntax for something is off.

avatar image Loius · Apr 08, 2013 at 05:55 PM 0
Share

As far as I know that's how all my Resources loads look. :(

avatar image MachCUBED · Apr 08, 2013 at 11:40 PM 0
Share

This doesn't seem to work either when casting the retrieved fonts:

 scoreLabel.font = Resources.Load(scoreLabel.font.name + "@2x") as Font;
 craneUseLabel.font = Resources.Load(craneUseLabel.font.name + "@2x") as Font;
 timerLabel.font = Resources.Load(timerLabel.font.name + "@2x") as Font;
 bonusTimerLabel.font = Resources.Load(bonusTimerLabel.font.name + "@2x") as Font;

Is there any other way to cast a variable in UnityScript, other than adding "as Datatype" to the end?

avatar image MachCUBED · Apr 09, 2013 at 09:41 PM 0
Share

I commented out the Resources.Load code in LevelStatus.js. Ins$$anonymous$$d, it now handles changing all the fonts from within the same C# script, GameStore.cs.

This is the section that works right:

 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));

This section still shows the font textures in a funny and distorted manner that is completely illegible":

 scoreLabel.font = (Font)Resources.Load(scoreLabel.font.name + "@2x", typeof(Font));
 craneUseLabel.font = (Font)Resources.Load(craneUseLabel.font.name + "@2x", typeof(Font));
 timerLabel.font = (Font)Resources.Load(timerLabel.font.name + "@2x", typeof(Font));
 bonusTimerLabel.font = (Font)Resources.Load(bonusTimerLabel.font.name + "@2x", typeof(Font));

The two sections of code are from the exact same C# script file (GameStore.cs), with all of the public GUIText objects set in the inspector. The GUIText elements are:

 public GUIText oneUsePrice;
 public GUIText fiveUsePrice;
 public GUIText tenUsePrice;
 public GUIText twentyUsePrice;
     
 public GUIText scoreLabel;
 public GUIText craneUseLabel;
 public GUIText timerLabel;
 public GUIText bonusTimerLabel;

So I investigated the GUIText objects in the inspector. The ones that don't seem to be working right all had their materials set in the inspector. Solution? Delete material assignment and see what happens.

Result? Setting the material value to none in the inspector for the problem GUIText objects solved the problem. Lesson learned? Don't set GUIText materials in the inspector when targeting both retina and non-retina devices.

0 Replies

· Add your reply
  • Sort: 

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

11 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Best place to learn Marching Cubes Algorithm? 2 Answers

Calling c# function from a JS 1 Answer

Need Help - Last Problem Converting PC to Android - Using JS to read CS 2 Answers


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