CurrenyInfo.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace SoftKitty.InventoryEngine
  6. {
  7. public class CurrenyInfo : MonoBehaviour
  8. {
  9. #region Variables
  10. public Image Icon;
  11. public Text ValueText;
  12. public Text ChangeText;
  13. public bool AutoCalculateChanges = true;
  14. public HintText HintScript;
  15. private int mIndex=0;
  16. private int value = -1;
  17. private int startValue = 0;
  18. private InventoryHolder Holder;
  19. private bool inited = false;
  20. #endregion
  21. #region Internal Methods
  22. private void Update()
  23. {
  24. if (!inited) return;
  25. if (value != Holder.GetCurrency(mIndex,false))
  26. {
  27. value = Holder.GetCurrency(mIndex,false);
  28. ValueText.text = value.ToString();
  29. ValueText.color = value < 0 ? Color.red : ItemManager.instance.currencies[mIndex].color;
  30. }
  31. if (ChangeText && AutoCalculateChanges) SetChangeText(value - startValue);
  32. }
  33. #endregion
  34. public void SetChangeText(int _changeValue)
  35. {
  36. if (_changeValue == 0)
  37. ChangeText.text = "";
  38. else
  39. ChangeText.text = (_changeValue >= 0 ? "<color=#469824>" : "<color=#BF3126>") + "(" + (_changeValue > 0 ? "+" : "") + _changeValue.ToString() + ")</color>";
  40. }
  41. public void Initialize(int _index, InventoryHolder _holder, bool _update=true)//_index=Currency ID, _update= if the value will auto update with the linked InventoryHolder.
  42. {
  43. mIndex = _index;
  44. Holder = _holder;
  45. Icon.sprite = ItemManager.instance.currencies[mIndex].icon;
  46. ValueText.color = ItemManager.instance.currencies[mIndex].color;
  47. if(HintScript) HintScript.HintString = ItemManager.instance.currencies[mIndex].name;
  48. SetStartValue();
  49. if(ChangeText) ChangeText.text = "";
  50. inited = _update;
  51. }
  52. public int GetCurrencyId()
  53. {
  54. return mIndex;
  55. }
  56. public int GetCurrencyValue()
  57. {
  58. return value;
  59. }
  60. public void SetStartValue()
  61. {
  62. startValue = Holder.GetCurrency(mIndex,false);
  63. } //This is useful if you want to compare the current value with the value by the point you call this method.
  64. public void SetValue(int _value)
  65. {
  66. ValueText.text = _value.ToString();
  67. }
  68. public void SetColor(Color _color)
  69. {
  70. ValueText.color = _color;
  71. }
  72. public void RevertColor()
  73. {
  74. ValueText.color = ItemManager.instance.currencies[mIndex].color;
  75. }
  76. }
  77. }