Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity UI Shop UI Tutorial - Scale issue on dynamically added items

Discussion in 'UGUI & TextMesh Pro' started by Gohloum, Jan 22, 2019.

  1. Gohloum

    Gohloum

    Joined:
    Nov 29, 2018
    Posts:
    12
    I'm fairly new to Unity, but have worked through a dozen or so tutorials with no issues and for older version files, I've been able to make the necessary upgrades to 2018.3 fairly easily.

    However, I've been doing the Shop UI Tutorial and I have found a behavior that might be a bug?

    // Following with the tutorial:
    1 - To reproduce, just setup the ScrollShopList as directed. Create the prefab for the SampleButton.
    2 - Make sure SampleButton prefab gives the expected results in sizing. To confirm, duplicate a few instances in the Hierarchy manually as shown in the video. (All works as expected)
    3 - Remove Instances from Hierarchy, add a few item elements in the inspector and populate.
    4 - Run the build.

    What I see in play mode is the SampleButton instances scale up on the X and Y to about 2.1. However, I was able to go into the Setup() function and force set the localScale on the transform of the SampleButton to a new Vector3 (1f, 1f, 1f) and now I am getting the results I expected.

    Code (CSharp):
    1.  
    2. // Inside SampleButton script
    3. public void Setup(Item currentItem, ShopScrollList currentScrollList)
    4.     {
    5.         item = currentItem;
    6.         nameLabel.text = item.itemName;
    7.         priceLabel.text = item.price.ToString();
    8.         iconImage.sprite = item.icon;
    9.  
    10.         scrollList = currentScrollList;
    11.      
    12.         // Workaround to fix scale anomoly on dynamically added button
    13.         button.transform.localScale = new Vector3(1f, 1f, 1f);
    14.     }
    15.  
    Does anyone know if the issue I am seeing has anything to do with settings in my version of Unity, or might this be some sort of bug that needs to be reported?

    The main difference I see between my version and the version in the video are the check boxes in the Vertical Layout Group. I have both "Child Controls Size" and "Child Force Expand" where he has just the "Force Expand". I've tried various different combinations of both this and ContentSizeFilter and LayoutElement. Nothing fixes this issue, just the line of code.

    Thanks for your help!
     
    Last edited: Jan 22, 2019