| | 1 | | using TMPro; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// ProfileHUD sub-view that shows the user MANA balance. |
| | 7 | | /// </summary> |
| | 8 | | internal class ManaCounterView : MonoBehaviour |
| | 9 | | { |
| | 10 | | [SerializeField] internal TextMeshProUGUI balanceText; |
| | 11 | | [SerializeField] internal Button_OnPointerDown buttonManaInfo; |
| | 12 | | [SerializeField] internal Button buttonManaPurchase; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Set the amount of MANA in the HUD. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="balance">Amount of MANA.</param> |
| | 18 | | public void SetBalance(string balance) |
| | 19 | | { |
| 1 | 20 | | double manaBalance = 0; |
| 1 | 21 | | if (double.TryParse(balance, out manaBalance)) |
| 1 | 22 | | SetBalance(manaBalance); |
| 1 | 23 | | } |
| | 24 | |
|
| 2 | 25 | | public void SetBalance(double balance) { balanceText.text = FormatBalanceToString(balance); } |
| | 26 | |
|
| | 27 | | private string FormatBalanceToString(double balance) |
| | 28 | | { |
| 1 | 29 | | if (balance >= 100000000) |
| | 30 | | { |
| 0 | 31 | | return (balance / 1000000D).ToString("0.#M"); |
| | 32 | | } |
| 1 | 33 | | if (balance >= 1000000) |
| | 34 | | { |
| 0 | 35 | | return (balance / 1000000D).ToString("0.##M"); |
| | 36 | | } |
| 1 | 37 | | if (balance >= 100000) |
| | 38 | | { |
| 0 | 39 | | return (balance / 1000D).ToString("0.#K"); |
| | 40 | | } |
| 1 | 41 | | if (balance >= 10000) |
| | 42 | | { |
| 0 | 43 | | return (balance / 1000D).ToString("0.##K"); |
| | 44 | | } |
| 1 | 45 | | if (balance < 0.001) |
| | 46 | | { |
| 0 | 47 | | return "0"; |
| | 48 | | } |
| 1 | 49 | | if (balance <= 1) |
| | 50 | | { |
| 0 | 51 | | return balance.ToString("0.###"); |
| | 52 | | } |
| 1 | 53 | | if (balance < 100) |
| | 54 | | { |
| 1 | 55 | | return balance.ToString("0.##"); |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | return balance.ToString("#,0"); |
| | 59 | | } |
| | 60 | | } |