I'm trying to reduce the number of lines of code by condensing the below into a repeatable function rather than adding multiple if else blocks for each item.
Current working logic:
So I currently have some logic that translates my game score into easier to read values as such:
1,000+ = simple formatting
10,000+ = 10K - 999k
1,000,000+ = 1M - 999M
etc
public void ScoreDisplayLogic()
{
// Convert to x.xM
if (GameManager.score >= 1000000)
{
scoreText.text = "Score: " + (GameManager.score / 1000000).ToString("0.0")+"M";
}
// Convert to x.xK
else if (GameManager.score >= 10000)
{
scoreText.text = "Score: " + (GameManager.score / 1000).ToString("0.0")+"K";
}
// Format with x,xxx.xx
else if (GameManager.score >= 1000)
{
scoreText.text = "Score: " + GameManager.score.ToString("0,000.00");
}
}
I want to do the same thing for my cost values, however I have a lot of different things with costs.
My goal is to be able to scale this, so is it possible to condense these into a singular script without writing an if else block for each one?
Trying to avoid the below:
ex) upgrade1Cost, upgrade2Cost, upgrade3Cost, idleUpgrade1Cost, etc
public void CostDisplayLogic()
{
// Format upgrade1Cost
// Convert to x.xM
if (GameManager.upgrade1Cost>= 1000000)
{
upgrade1CostText.text = "Score: " + (GameManager.upgrade1Cost/ 1000000).ToString("0.0")+"M";
}
// Convert to x.xK
else if (GameManager.upgrade1Cost>= 10000)
{
upgrade1CostText.text = "Score: " + (GameManager.upgrade1Cost/ 1000).ToString("0.0")+"K";
}
// Format with x,xxx.xx
else if (GameManager.upgrade1Cost>= 1000)
{
upgrade1CostText.text = "Score: " + GameManager.upgrade1Cost.ToString("0,000.00");
}
// Format upgrade2Cost
// Convert to x.xM
if (GameManager.upgrade2Cost>= 1000000)
{
upgrade2CostText.text = "Score: " + (GameManager.upgrade2Cost/ 1000000).ToString("0.0")+"M";
}
// Convert to x.xK
else if (GameManager.upgrade2Cost>= 10000)
{
upgrade2CostText.text = "Score: " + (GameManager.upgrade2Cost/ 1000).ToString("0.0")+"K";
}
// Format with x,xxx.xx
else if (GameManager.upgrade2Cost>= 1000)
{
upgrade2CostText.text = "Score: " + GameManager.upgrade2Cost.ToString("0,000.00");
}
// etc, etc
}