Hello,
I’m trying to draw a inventory system but for some reason the Slots gets really big.
I follow this tutorial by the way: Link
3 screenshots of settings and how the bug looks:
Picture 1:
The bug
Picture 2:
The prefab has the size of width/height 47/47 and scale is 0.413 for x, y and z.
It seems like the object is spawned in the scene, than made into canvas. Which makes it pretty big. I might be wrong as I haven’t spawned objects like this before with the new UI system.
Picture 3:
Camera settings
Pivot point for both slot and inventory is top left, however that will cause a issue because the inventory window is scaling the window depending on X slots with the current script. My Inventory UI background is not a square with no content and it has to be more specific where to put the actuall slots. Haven’t tried to play around with this yet because the slots are bugged currently.
The Script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour {
private RectTransform inventoryRect;
private float inventoryWidth, inventoryHeight;
public int slots;
public int rows;
public float slotPaddingLeft,slotPaddingTop;
public float slotSize;
public GameObject slotPrefab;
private List<GameObject> allSlots;
void Start () {
CreateLayout ();
}
private void CreateLayout(){
allSlots = new List<GameObject>();
//Resize the inventory background! ----
inventoryWidth = (slots / rows) * (slotSize + slotPaddingLeft) + slotPaddingLeft;
inventoryHeight = rows * (slotSize + slotPaddingTop) + slotPaddingTop;
inventoryRect = GetComponent<RectTransform>();
inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, inventoryWidth);
inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, inventoryHeight);
int columns = slots / rows;
for (int y = 0; y < rows; y++) {
for (int x = 0; x < columns; x++){
GameObject newSlot = (GameObject)Instantiate(slotPrefab);
RectTransform slotRect = newSlot.GetComponent<RectTransform>();
newSlot.name = "Slot";
newSlot.transform.SetParent (this.transform.parent);
slotRect.localPosition = inventoryRect.localPosition + new Vector3(slotPaddingLeft * (x+1) + (slotSize * x), -slotPaddingTop * (y+1)- (slotSize * y));
slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,slotSize);
slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, slotSize);
allSlots.Add(newSlot);
}
}
}
}