- Home /
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
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.
.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.
As far as I know that's how all my Resources loads look. :(
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?
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.