Unity UI - prefab scaling problem

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);
            }
        }

    }

}

‘Bump’.
The icons scales to 18 in x,y,z. While it’s suppose to be 0.43 in x,y,z.
Assume this is because it’s pasted into the room, before it’s made as a child to canvas. Any ideas how to get around this issue?

Fixed it, not sure if it’s a good way but … at least I tried and it works for me atm.
What did I do?

  • I added a new line within the loop to set the vector value.
newSlot.gameObject.transform.localScale = new Vector3(tempScale,tempScale,tempScale);
  • I also had to change the localPosision because it was to far off.
slotRect.localPosition = inventoryRect.localPosition +  new Vector3(slotPaddingLeft * (x+1) + ((slotSize*tempScale) * x), -slotPaddingTop * (y+1)- ((slotSize*tempScale) * y));
  • Than to create my inventory UI so it matches the inventory slots perfectly , I set the width/height manually.
    For this game it doesn’t matter if it’s set to a static value because the bag will always contain 4x4 slots.
        inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, /*inventoryWidth*/253);
        inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, /*inventoryHeight*/327);

Use SetParent(transform, false) to retain the original local transform.

2 Likes

I have had similar problems creating prefabs and adding them to a UI. It likely is the SetParent call that’s to blame, but I DO want the new prefab to inherit a new position, just not the scale.

thanks, I believe this solves MANY peoples UI prefab scaling issues, not sure why it not widely known, I just found out on another thread.

Thanks.This helped me. My original prefab scale was overridden when I was instantiating inside list content. It solved the issue.