//Used when called from LevelEventManager. public void AddSlotsToSystem(MerchantSlotScript[,] slots) { //Combine the two slot arrays. MerchantSlotScript[,] newSlotArray = new MerchantSlotScript[slotArray.GetLength(0) + slots.GetLength(0), slots.GetLength(1)]; //Add the old array first. for (int y = 0; y < slotArray.GetLength(0); y++) { for (int x = 0; x < slotArray.GetLength(1); x++) { newSlotArray [y, x] = slotArray [y, x]; } } //Add the new array last. for (int y = 0; y < slots.GetLength(0); y++) { for (int x = 0; x < slots.GetLength(1); x++) { newSlotArray [y + slotArray.GetLength(0), x] = slots [y, x]; } } //Set the old slot array to the new slot array. slotArray = newSlotArray; //Make sure that it is initialized. initialized = true; }
protected MerchantSlotScript[,] InitializeSlots() { //Create the "Slots" parent. Has to be used so that InventoryHideShow does not end its coroutine. Transform slots = transform.Find("Slots"); slots.SetParent(transform, false); slots.transform.localPosition = Vector3.zero; // Get sizes of the inventory panel and the slot prefab. inventoryPanelSize = slots.GetComponent <RectTransform> ().sizeDelta; slotPanelSize = slotPrefab.GetComponent <RectTransform> ().sizeDelta; // Determine the maximum number of slots that can fit on either axis of the inventory panel. int maxNumberOfXSlots = (int)((inventoryPanelSize.x - (inventoryPanelSize.x % slotPanelSize.x)) / (slotPanelSize.x)); int maxNumberOfYSlots = (int)((inventoryPanelSize.y - (inventoryPanelSize.y % slotPanelSize.y)) / (slotPanelSize.y)); // Determine the remaining area unaccounted for by the slots themselves. float additionalPanelXSizeRemaining = inventoryPanelSize.x % slotPanelSize.x; float additionalPanelYSizeRemaining = inventoryPanelSize.y % slotPanelSize.y; //Using the previous step, determine the excess slot space that should be used for each group. float paddingPerXSlot = additionalPanelXSizeRemaining / (maxNumberOfXSlots + 1); float paddingPerYSlot = additionalPanelYSizeRemaining / (maxNumberOfYSlots + 1); //MerchantSlotScript 2d array. MerchantSlotScript[,] createdUISlots = new MerchantSlotScript[maxNumberOfYSlots, maxNumberOfXSlots]; //For every column, for (int y = 1; y < maxNumberOfYSlots + 1; y++) { //For every row, for (int x = 1; x < maxNumberOfXSlots + 1; x++) { //Create the slot GameObject createdSlot = (GameObject)Instantiate(slotPrefab); createdSlot.transform.SetParent(slots, false); //Determine the coordinates and offset. Equal to the previous padding for x/y and slot panel size (x-1) and the current size. float rectTransformXCoordinate = (float)((x - 1) * (paddingPerXSlot + slotPanelSize.x) + (paddingPerXSlot + .5 * slotPanelSize.x)); float rectTransformYCoordinate = (float)((y - 1) * (paddingPerYSlot + slotPanelSize.y) + (paddingPerYSlot + .5 * slotPanelSize.y)); //Determine the displacement vector (equal to half of the size of the inventory panel). Vector2 displacementVector = (.5f * inventoryPanelSize); createdSlot.GetComponent <RectTransform> ().anchoredPosition = new Vector2(rectTransformXCoordinate, rectTransformYCoordinate) - displacementVector; createdSlot.name = "Slot " + x + "." + y; createdUISlots [y - 1, x - 1] = createdSlot.GetComponent <MerchantSlotScript> (); } } //Return created slot array. return(createdUISlots); }
//Assigns a new item to the best possible slot. public bool AssignNewItemToBestSlot(ResourceReferenceWithStackAndPrice item) { //Has to be here for the return statement bool successfullyAssigned = false; //Make sure that the prerequisites are met. if (initialized && item != null) { MerchantSlotScript bestAvailableSlot = FindBestAvailableSlot(item); if (bestAvailableSlot != null) { //Set successfully assigned. successfullyAssigned = true; //Add the new stack to the current item stack. bestAvailableSlot.ModifyCurrentItemStack(item.mainContentReference.stack); Debug.Log("Assigned " + item.mainContentReference.uiSlotContent.itemScreenName + " to slot with items of same type."); } else { Debug.Log("Could not stack item: Attempting to add to an empty slot"); bestAvailableSlot = FindBestAvailableNullSlot(); if (bestAvailableSlot != null) { successfullyAssigned = true; bestAvailableSlot.AssignNewItem(item); } else { Debug.LogError("No slots are empty!"); } } } else { if (initialized == false && item == null) { Debug.LogError("Not initialized and item is null"); } else if (initialized == false) { Debug.LogError("Not initialized"); } else { Debug.LogError("Item is null"); } } return(successfullyAssigned); }