- Home /
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
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.
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.
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
Follow this Question
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