InventoryUi.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace SoftKitty.InventoryEngine
  6. {
  7. public class InventoryUi : ItemContainer
  8. {
  9. #region Variables
  10. public CurrenyInfo CurrencyPrefab;
  11. public Text WeightText;
  12. public Image WeightBar;
  13. public Transform MiniModeIcon;
  14. private float weight = -1F;
  15. private float maxWeight = -1F;
  16. private bool miniMode = false;
  17. private float fullHeight = 700F;
  18. #endregion
  19. #region MonoBehaviour
  20. public override void Update()
  21. {
  22. if (!inited) return;
  23. base.Update();
  24. UpdateWeight();
  25. }
  26. #endregion
  27. public override void Initialize(InventoryHolder _inventoryHolder, InventoryHolder _equipHolder, string _name = "Inventory")//Initialize this interface
  28. {
  29. base.Initialize(_inventoryHolder, _equipHolder, _name);
  30. fullHeight = GetComponent<RectTransform>().sizeDelta.y;
  31. Holder.CalWeight();
  32. for (int i=0;i< Holder.Currency.Count;i++) {
  33. GameObject _newItem = Instantiate(CurrencyPrefab.gameObject, CurrencyPrefab.transform.parent);
  34. _newItem.transform.localScale = Vector3.one;
  35. _newItem.SetActive(true);
  36. _newItem.GetComponent<CurrenyInfo>().Initialize(i, Holder);
  37. }
  38. inited = true;
  39. }
  40. public void ExpandSwitch()//Toggle between the mini window and normal window
  41. {
  42. SoundManager.Play2D("MenuOff");
  43. miniMode = !miniMode;
  44. GetComponent<RectTransform>().sizeDelta = new Vector2(GetComponent<RectTransform>().sizeDelta.x, miniMode?450F:fullHeight);
  45. MiniModeIcon.localEulerAngles = new Vector3(0F,0F,miniMode?180F:0F);
  46. }
  47. private void UpdateWeight()//Update the total weight value of all items.
  48. {
  49. if (weight != Holder.GetWeight() || maxWeight != Holder.MaxiumCarryWeight)
  50. {
  51. weight = Holder.GetWeight();
  52. maxWeight = Holder.MaxiumCarryWeight;
  53. WeightText.text = weight.ToString("0.0") + "/" + maxWeight.ToString("0.0");
  54. float _weightPercentage =Mathf.Clamp01( weight / maxWeight);
  55. WeightText.color = new Color(0.8F, 0.8F - Mathf.Max(0F, _weightPercentage * 0.8F - 0.6F)*4F, 0.8F - Mathf.Max(0F, _weightPercentage * 0.8F - 0.4F)*2F);
  56. WeightBar.transform.localScale = new Vector3(_weightPercentage, 1F, 1F);
  57. }
  58. }
  59. }
  60. }