UIItemStorage.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [AddComponentMenu("NGUI/Examples/UI Item Storage")]
  5. public class UIItemStorage : MonoBehaviour
  6. {
  7. public List<InvGameItem> items
  8. {
  9. get
  10. {
  11. while (this.mItems.Count < this.maxItemCount)
  12. {
  13. this.mItems.Add(null);
  14. }
  15. return this.mItems;
  16. }
  17. }
  18. public InvGameItem GetItem(int slot)
  19. {
  20. return (slot >= this.items.Count) ? null : this.mItems[slot];
  21. }
  22. public InvGameItem Replace(int slot, InvGameItem item)
  23. {
  24. if (slot < this.maxItemCount)
  25. {
  26. InvGameItem result = this.items[slot];
  27. this.mItems[slot] = item;
  28. return result;
  29. }
  30. return item;
  31. }
  32. private void Start()
  33. {
  34. if (this.template != null)
  35. {
  36. int num = 0;
  37. Bounds bounds = default(Bounds);
  38. for (int i = 0; i < this.maxRows; i++)
  39. {
  40. for (int j = 0; j < this.maxColumns; j++)
  41. {
  42. GameObject gameObject = NGUITools.AddChild(base.gameObject, this.template);
  43. Transform transform = gameObject.transform;
  44. transform.localPosition = new Vector3((float)this.padding + ((float)j + 0.5f) * (float)this.spacing, (float)(-(float)this.padding) - ((float)i + 0.5f) * (float)this.spacing, 0f);
  45. UIStorageSlot component = gameObject.GetComponent<UIStorageSlot>();
  46. if (component != null)
  47. {
  48. component.storage = this;
  49. component.slot = num;
  50. }
  51. bounds.Encapsulate(new Vector3((float)this.padding * 2f + (float)((j + 1) * this.spacing), (float)(-(float)this.padding) * 2f - (float)((i + 1) * this.spacing), 0f));
  52. if (++num >= this.maxItemCount)
  53. {
  54. if (this.background != null)
  55. {
  56. this.background.transform.localScale = bounds.size;
  57. }
  58. return;
  59. }
  60. }
  61. }
  62. if (this.background != null)
  63. {
  64. this.background.transform.localScale = bounds.size;
  65. }
  66. }
  67. }
  68. public int maxItemCount = 8;
  69. public int maxRows = 4;
  70. public int maxColumns = 4;
  71. public GameObject template;
  72. public UIWidget background;
  73. public int spacing = 128;
  74. public int padding = 10;
  75. private List<InvGameItem> mItems = new List<InvGameItem>();
  76. }