Search Unity

Unity UI - prefab scaling problem

Discussion in 'UGUI & TextMesh Pro' started by Loff, Aug 4, 2015.

  1. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    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.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Inventory : MonoBehaviour {
    6.  
    7.     private RectTransform inventoryRect;
    8.     private float inventoryWidth, inventoryHeight;
    9.     public int slots;
    10.     public int rows;
    11.     public float slotPaddingLeft,slotPaddingTop;
    12.     public float slotSize;
    13.     public GameObject slotPrefab;
    14.     private List<GameObject> allSlots;
    15.  
    16.  
    17.     void Start () {
    18.         CreateLayout ();
    19.     }
    20.  
    21.  
    22.     private void CreateLayout(){
    23.         allSlots = new List<GameObject>();
    24.         //Resize the inventory background! ----
    25.  
    26.         inventoryWidth = (slots / rows) * (slotSize + slotPaddingLeft) + slotPaddingLeft;
    27.  
    28.         inventoryHeight = rows * (slotSize + slotPaddingTop) + slotPaddingTop;
    29.  
    30.         inventoryRect = GetComponent<RectTransform>();
    31.    
    32.         inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, inventoryWidth);
    33.         inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, inventoryHeight);
    34.  
    35.         int columns = slots / rows;
    36.  
    37.         for (int y = 0; y < rows; y++) {
    38.             for (int x = 0; x < columns; x++){
    39.                 GameObject newSlot = (GameObject)Instantiate(slotPrefab);
    40.                 RectTransform slotRect = newSlot.GetComponent<RectTransform>();
    41.                 newSlot.name = "Slot";
    42.                 newSlot.transform.SetParent (this.transform.parent);
    43.                 slotRect.localPosition = inventoryRect.localPosition +  new Vector3(slotPaddingLeft * (x+1) + (slotSize * x), -slotPaddingTop * (y+1)- (slotSize * y));
    44.                 slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,slotSize);
    45.                 slotRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, slotSize);
    46.                 allSlots.Add(newSlot);
    47.             }
    48.         }
    49.  
    50.     }
    51.  
    52. }
     
    Last edited: Aug 5, 2015
  2. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    '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?
     
  3. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    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.
    Code (CSharp):
    1. newSlot.gameObject.transform.localScale = new Vector3(tempScale,tempScale,tempScale);
    - I also had to change the localPosision because it was to far off.
    Code (CSharp):
    1. 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.
    Code (CSharp):
    1.         inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, /*inventoryWidth*/253);
    2.         inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, /*inventoryHeight*/327);
     
  4. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,412
    Use SetParent(transform, false) to retain the original local transform.
     
    rameshlg and radiantboy like this.
  5. Foo-Byte

    Foo-Byte

    Joined:
    Jun 26, 2015
    Posts:
    12
    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.
     
  6. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,633
    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.
     
  7. rameshlg

    rameshlg

    Joined:
    Feb 22, 2022
    Posts:
    1
    Thanks.This helped me. My original prefab scale was overridden when I was instantiating inside list content. It solved the issue.