ContainerBase.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace SoftKitty.InventoryEngine
  6. {
  7. public class ContainerBase : UiWindow
  8. {
  9. #region Variables
  10. public Text TitleText;
  11. public RectTransform HoverInfoAnchorPoint;
  12. public InventoryItem ItemPrefab;
  13. public Image BlockImage;
  14. protected InventoryHolder Holder;
  15. protected bool inited = false;
  16. protected InventoryItem[] Items;
  17. #endregion
  18. #region MonoBehaviour
  19. public override void Update()
  20. {
  21. if (!inited) return;
  22. base.Update();
  23. CheckBlock();
  24. }
  25. #endregion
  26. public override void Initialize(InventoryHolder _inventoryHolder, InventoryHolder _equipHolder, string _name = "Inventory")//Initialize this interface
  27. {
  28. Holder = _inventoryHolder;
  29. TitleText.text = _name.ToUpper();
  30. ItemPrefab.Outline.color = InventorySkin.instance.ItemSelectedColor;
  31. ItemPrefab.Hover.color = InventorySkin.instance.ItemHoverColor;
  32. ItemPrefab.Fav.GetComponent<Image>().color = InventorySkin.instance.FavoriteColor;
  33. Holder.CalWeight();
  34. ShowList();
  35. inited = true;
  36. }
  37. protected void ShowList()//Show Item List
  38. {
  39. Items = new InventoryItem[Holder.Stacks.Count];
  40. for (int i = 0; i < Holder.Stacks.Count; i++)
  41. {
  42. GameObject _newItem = Instantiate(ItemPrefab.gameObject, ItemPrefab.transform.parent);
  43. _newItem.transform.localScale = Vector3.one;
  44. _newItem.GetComponent<InventoryItem>().Initialize(Holder.Stacks[i]);
  45. _newItem.GetComponent<InventoryItem>().SetHolder(Holder);
  46. _newItem.GetComponent<InventoryItem>().HoverInfoAnchorPoint = HoverInfoAnchorPoint;
  47. _newItem.GetComponent<InventoryItem>().RegisterClickCallback(i, OnItemClick);
  48. _newItem.gameObject.SetActive(true);
  49. Items[i] = _newItem.GetComponent<InventoryItem>();
  50. }
  51. }
  52. public virtual void OnItemClick(int _index, int _button)//Callback for when player click an item
  53. {
  54. }
  55. private void CheckBlock() //Check if the interface should be blocked.
  56. {
  57. if (NumberInput.instance != null)
  58. {
  59. BlockImage.gameObject.SetActive(true);
  60. BlockImage.color = new Color(0F, 0F, 0F, Mathf.Lerp(BlockImage.color.a, 0.94F, Time.deltaTime * 3F));
  61. }
  62. else
  63. {
  64. if (BlockImage.color.a > 0F)
  65. {
  66. BlockImage.color = new Color(0F, 0F, 0F, Mathf.MoveTowards(BlockImage.color.a, 0F, Time.deltaTime * 2F));
  67. }
  68. else
  69. {
  70. if (BlockImage.gameObject.activeSelf) BlockImage.gameObject.SetActive(false);
  71. }
  72. }
  73. }
  74. }
  75. }