- Home /
How to show numbers in unity
Hi,
I need to show winning amount on the screen. These value can be of any digit like 5,15,100 (any digit). Please tell me how to show these numbers on screen. I have images for the number 0 to 9. But I I don't know how to do this. Please help. I am new to unity. Its Urgent.
Hi alucardj($$anonymous$$ $$anonymous$$ay),
The link you have suggested is helpful but my question is little bit different. Let say user wins 30 point show it should pick two images at run time. I mean for 3 and 0. And lets say user wins 123 points then in this case it should pick texture for the numbers 1,2 and 3. And also it should display these textures to form a number 123 or 30. Problem is let say I have defined a plane to show win value. Then 1. I can not put all three or two textures in one plane. For that I need to take three planes in a parent game object. 2. How I should know that how many planes to display. I mean it is O$$anonymous$$ for temporary use but is there any other way to do this.
wow , that's a bit trickier. You need 10 textures (for the numbers), then you need to split the score to each power of 10 (123 becomes 1; 2; 3;), then you need to apply the correct texture to the correct position in the GUI. I'll have a look at it if you are still lost on this , but it is a big job (for a noob like me!) =]
i should ask ... are you going to display the points as a GUI.DrawTexture? Or changing the texture on a gameObject?
http://unity3d.com/support/documentation/ScriptReference/GUI.DrawTexture.html
I am trying to changing the texture of a gameobject. But I will do this anyway if I get the correct one. Actually I am not sure how to do this.
Answer by Bunny83 · May 02, 2012 at 02:42 PM
A custom font would be exactly what you need:
Just create a custom font in Unity by selecting create-->custom font in your project view.
You should put all your number images into one texture in a grid pattern. For numbers it's best to use just one line.
Set the font tiling (if you have all characters in one line y=1 x=10)
Your number images need to be in ascii code order: "0 1 2 3 4 5 6 7 8 9"
The ascii code for "0" is 48 so set your Ascii start offset to 48.
Create a material with your texture and assign it to the fonts default material variable.
Finally you can adjust the default kerning or add per character kerning to adjust the space of individual characters.
Keep in mind when you use this font you can only display strings which contains the numbers from 0 to 9. If you try to use any other character it will display garbage.
This font can be used in any guistyle that displays text. It's probably the best to create a custom GUISkin and create a custom style for your font. Use the skin by assigning it to GUI.skin in OnGUI and just use a GUI.Label with your custom style to display the number.
//C#
public GUISkin mySkin; // assign in the inspector
public int myNumber = 230;
void OnGUI()
{
GUI.skin = mySkin;
GUILayout.Label(""+myNumber, "myCustomStyleName");
}
edit
This is from a 2 week android project which has never been bublished. We used this font texture (created by our pixel artist ;)). It's a transparent png so the numbers don't have a background, it just looks white here but it isn't ;)
In the top inspector you see the custom font itself. We used additional characters for the colon(:), the "x"(;) and the infinity sign(<). The characters i've wrote in brackets are the characters you have to use to display the particular char (See ascii characters).
The bottom inspector shows a GUIText component which directly uses the font. As material shader we used a transparent cutout shader.
We just had to adjust the kerning for the special characters, all numbers have the default kerning of 0.5 in our case.
second edit
I've just created a custom font by using a texture i've found somewhere on the net. It's just roughly aligned and has some artefacts at the edges, but it should explain how it works ;) The image source might even be copyrighted so you should use your own.
Here's the package.
Hi, Can you please upload a demo or can you please tell the steps to follow for this. I have confusion about point 2 and 4. Actually I am new to unity so I don't know how to do this. It will be very helpful to me. I think I need something like this.
Answer by AlucardJay · May 01, 2012 at 03:12 PM
This is Not an ideal method , but it should get you going. (personally I would create one material with all the numbers on it , then swap the UV to display the correct number).
First create the textures (images/pictures) 0 - 9.
Then make 10 materials , and apply each picture to a material.
create a cube , then create a script , and attach the script to the cube.
Copy and paste the below script in your cube script.
in the cube's inspector, drag and drop each material to the correct place in the inspector at Mat 0, Mat 1, etc
Hit play =]
every 1.5 seconds , the number on the cube should change.
here is what's happening :
each material is stored in a variable : public var mat0 : Material;
etc
then an array of these materials is made : materialsArr = [mat0, mat1, mat2, mat3, mat4, mat5, mat6, mat7, mat8, mat9];
the material is being changed by this command : renderer.material = materialsArr[currentMaterial];
play around with putting different materials into variables , then change the material in the script with :
renderer.material = materialVariable ;
oh , here's the script :
public var mat0 : Material;
public var mat1 : Material;
public var mat2 : Material;
public var mat3 : Material;
public var mat4 : Material;
public var mat5 : Material;
public var mat6 : Material;
public var mat7 : Material;
public var mat8 : Material;
public var mat9 : Material;
public var materialsArr : Material[];
public var currentMaterial : int = 0;
function Start()
{
materialsArr = [mat0, mat1, mat2, mat3, mat4, mat5, mat6, mat7, mat8, mat9];
currentMaterial = 0;
renderer.material = materialsArr[currentMaterial];
InvokeRepeating("ChangeMaterial", 1.5, 1.5);
}
function ChangeMaterial()
{
currentMaterial++;
if (currentMaterial == 10) {currentMaterial = 0;}
renderer.material = materialsArr[currentMaterial];
}
[1]: http://answers.unity3d.com/storage/temp/788-setup.gif
[2]: http://answers.unity3d.com/storage/temp/789-running.gif
Hi $$anonymous$$,
Your code sounds good. But in my case what I need is, I need to display more than one object at a time. Let say win value is 3 then it need only one game object. But if is 23 then there are two object, one for 2 and next is for 3. Same way the number can be increased according to the win value. I cannot do this at one time because how can know what is the win value. I can change the texture of the object, no doubt but I can not judge the number of game object to display. Also I need to manage the alignment. Because it should be in center. But this thing I can manage. But please tell me how to display n number of game object. I think I need to create game object dynamically. Please help.
a basic way would be convert the 'win value' to a string then get the string's length. This should work. $$anonymous$$ake a new scene and attach this script to the camera. When it plays, change the number inputNumber in the Inspector, and watch what the console is saying for numberOfObjectsNeeded.
note : if you go over 999999999 it will break , but that's like displaying 999,999,999 which should be a high enough number (unless the points go above 1,000,000,000?!) =]
#pragma strict
public var inputNumber : int = 123;
private var tempInputStr : String;
public var numberOfObjectsNeeded : int;
function Update()
{
tempInputStr = inputNumber.ToString();
numberOfObjectsNeeded = tempInputStr.Length;
Debug.Log("tempInputStr = " + tempInputStr + " : numberOfObjectsNeeded = " + numberOfObjectsNeeded);
}
Answer by saied63 · Mar 05, 2013 at 06:37 AM
hi Bunny83 . i did all your tutorial levels and it worked good on guiText and 3d Text but when i try to use it in OnGUI function it show nothings . can you help me ? thank you .
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Unity running out of memory 1 Answer
Opacity from 3ds max doesn't show up 2 Answers
multiple cameras on the one screen 1 Answer
Something is wrong with my unity 1 Answer