- Home /
GUI Display in game cash help
Ok so I can get the users money to be displayed in the screen, change the text when the user reaches a certain amount of money but where I am having trouble is converting the amount for the respective symbols like Thousands (K) Millions (M) Billions (B). Since the players will be working with a high amount of money throughout the game, I believe this method would work the best for reading how much money you have and since I cannot figure out how to display 10,000,000 without unity having it say 1E+07.
Lets say the player has $900M ($900 Million) then they gain $100M and now they have $1000M or $1B ($1 Billion). How would I go about displaying the number 1000 as 1 or 1789 as 1.789 but have it still acting like 1000 or 1789?
I'll put my test code in here for a reference as to what I have so far. It works but I just can't figure out the conversion part of it so I have nothing for that in there.
C# Code
using UnityEngine;
using System.Collections;
public class UpgradeWindow : MonoBehaviour {
public GUIStyle myStyle;
void OnGUI ()
{
if(GameControl.control.cash1 < 1000f)
{
GUI.Label (new Rect (60, 60, 100, 30), "Cash: $ " + GameControl.control.cash1 + " M", myStyle);
}
else if(GameControl.control.cash1 >= 1000f)
{
GUI.Label (new Rect (60, 60, 100, 30), "Cash: $ " + GameControl.control.cash1 + " B", myStyle);
}
if(GUI.Button(new Rect(100, 200, 200, 60), "Upgrade", myStyle))
{
GameControl.control.cash1 += 100.07f;
}
if(GUI.Button(new Rect(100, 300, 200, 60), "Degrade", myStyle))
{
GameControl.control.cash1 -= 100.06f;
}
}
}
EDIT
I ended up figuring it out but to me the code looks a bit messy.
I changed cash1 to 3 different variables and changed them to decimals instead of floats like this.
C# Code
GameControl Script
public decimal cashK1;
public decimal cashM1;
public decimal cashB1;
Then I made these changes to my code.
using UnityEngine;
using System.Collections;
public class UpgradeWindow : MonoBehaviour {
public GUIStyle myStyle;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if(GameControl.control.cashK1 < 0.00M)
{
GameControl.control.cashK1 = 0.00M;
GameControl.control.cashM1 = 0.00M;
GameControl.control.cashB1 = 0.00M;
//the above code makes sure you cannot get a negative value
}
}
void OnGUI ()
{
if(GameControl.control.cashK1 < 1000.00M)
{
GUI.Label (new Rect (60, 60, 100, 30), "Cash: $ " + GameControl.control.cashK1 + " K", myStyle);
}
if(GameControl.control.cashK1 >= 1000.00M && GameControl.control.cashK1 < 1000000.00M)
{
GUI.Label (new Rect (60, 60, 100, 30), "Cash: $ " + GameControl.control.cashM1 + " M", myStyle);
}
if(GameControl.control.cashK1 >= 1000000.00M)
{
GUI.Label (new Rect (60, 60, 100, 30), "Cash: $ " + GameControl.control.cashB1 + " B", myStyle);
}
if(GUI.Button(new Rect(100, 200, 200, 60), "Upgrade", myStyle))
{
//Added all three decimal variables to always keep them equivalent to each other
GameControl.control.cashK1 += 50000.00M;
GameControl.control.cashM1 += 50.00M;
GameControl.control.cashB1 += 0.05M;
}
if(GUI.Button(new Rect(100, 300, 200, 60), "Degrade", myStyle))
{
GameControl.control.cashK1 -= 50000.00M;
GameControl.control.cashM1 -= 50.00M;
GameControl.control.cashB1 -= 0.05M;
}
}
}
If someone sees that I am using too much code and know how to shorten it, I would appreciate it.
Also sorry for asking a question then figuring out how to solve it before anyone can answer. It seems like the only time I can figure something I have been stuck on for a few days is to ask the question.
Maybe typing out my question helps me actually see what I am looking for?
Answer by tanoshimi · Aug 10, 2014 at 07:09 PM
Don't store your amount of cash in three different variables - that's just horrible! To display the amount of cash with the appropriate suffix, just create a new function like this:
static string FormatCash(long num)
{
if (num >= 100000000) {
return (num / 1000000D).ToString("0.#M");
}
if (num >= 1000000) {
return (num / 1000000D).ToString("0.##M");
}
if (num >= 100000) {
return (num / 1000D).ToString("0.#k");
}
if (num >= 10000) {
return (num / 1000D).ToString("0.##k");
}
return num.ToString("#,0");
}
(Note I've put your cash as a long
- I doubt you want a decimal
, and certainly not a float
). Then, in all your OnGUI code, just call FormatCash(amountOfCash). This will return the following:
123 -> 123
1234 -> 1,234
12345 -> 12.35k
123456 -> 123.4k
1234567 -> 1.23M
12345678 -> 12.35M
123456789 -> 123.5M
Awesome Thanks. I haven't been coding for very long so the only thing I am having trouble with is adding/subtracting an amount from the gui. I can get it to show the amount with the appropriate suffix but when I try to add or subtract from that gui nothing happens.
I'm sure this probably is not the correct way to do this but this is what I am doing.
C# Code
GUI.Label (new Rect (70, 65, 100, 30), "Cash: $ " + FormatCash(0), myStyle);
if(GUI.Button(new Rect(100, 200, 200, 60), "Upgrade", myStyle))
{
FormatCash(10000);
}
Your answer
Follow this Question
Related Questions
Changing Health Display GUI 1 Answer
How to reduce number of digits in coordinate points? 2 Answers
Text Mesh Pro - Unicodes 0 Answers
Animation not showing when close up 1 Answer
Bug when hiding resolution Dialog? (Settings get reset on startup) 3 Answers