예제 #1
0
 public void Initialize(InventoryRenderer renderer)
 {
     Renderer       = renderer;
     _rectTransform = GetComponent <RectTransform>();
     _canvas        = renderer.Canvas;
     _canvasGroup   = gameObject.AddComponent <CanvasGroup>();
 }
예제 #2
0
        /// <summary>
        /// Base constructor for Inventory, pass along InventoryRenderer if you want items to be rendered.
        /// </summary>
        /// <param name="maxItems"></param>
        /// <param name="maxStackSize"></param>
        /// <param name="renderer"></param>
        public Inventory(int maxItems, int maxStackSize, InventoryRenderer renderer = null)
        {
            if (maxStackSize <= 0)
            {
                throw new Exception("[Initialization]: Inventory max stack size cannot be zero or less.");
            }
            if (maxItems <= 0)
            {
                throw new Exception("[Initialization]: Max inventory size cannot be zero or less.");
            }

            _maxItems     = maxItems;
            _items        = new InventoryItem[_maxItems];
            _maxStackSize = maxStackSize;
            _renderer     = renderer;

            if (_renderer == null)
            {
                return;
            }

            // Methods passed along to renderer
            _renderer.GetSlotAmount = (index) =>
            {
                return(_items[index] != null ? _items[index].Amount.ToString() : string.Empty);
            };
            _renderer.SwitchListIndex = (index1, index2) =>
            {
                var temp  = _items[index1];
                var temp2 = _items[index2];
                _items[index1] = temp2;
                _items[index2] = temp;
            };
        }