Search Unity

Scroll view is setting scale factor on items when added in code?

Discussion in 'UGUI & TextMesh Pro' started by Sinbad3D, Mar 1, 2017.

  1. Sinbad3D

    Sinbad3D

    Joined:
    Oct 11, 2016
    Posts:
    17
    This is driving me nuts. I have a Scroll view set up which works fine when I add instances of a prefab item in the editor, either before running or in-flight. However when I instantiate that prefab and add it in code, the items are scaled down in a weird way, meaning they don't fit the area and there are big gaps between them. If I select all the items during runtime and change the scale back to 1 they look fine, and if I drag/drop a new prefab on to the content area that's fine too.

    The setup:
    1. Standard Scroll View with Scroll Rect component -> Viewport -> Content
    2. Content has a Vertical Layout Group on it
    3. Content has a Content Size Fitter with Vertical Fit=Preferred (so content extends vertically to number of items, this works fine)
    4. Each Item is a prefab which is a standard button, just with a Layout Element added to it to set a Minimum Width & Height. They're fixed size for simplicity while trying to track down this issue.
    Code to add each item at runtime is:

    Code (CSharp):
    1.  var button = Instantiate<Button>(prefab);
    2. // .. set text etc, not important
    3. button.transform.SetParent(content.transform);
    4.  
    Here's what it looks like if I instantiate the prefab at design time:
    Screen Shot 2017-03-01 at 17.51.41.png

    Here's when I add the prefabs at runtime:
    Screen Shot 2017-03-01 at 17.52.16.png

    And here's at runtime when I drag an extra prefab in while running in the editor:
    Screen Shot 2017-03-01 at 17.52.39.png

    It's just scaling the items but I have no idea why. Guess I could just run through and hack them back to 1 in code but I'd really like to understand why it's doing this. Any hints gratefully received!
     
    Extiward and AlexandruFilipescu like this.
  2. Sinbad3D

    Sinbad3D

    Joined:
    Oct 11, 2016
    Posts:
    17
    Oh worth saying that my Canvas is set to Scale With Screen Size and this gets worse the higher the resolution. However all my other UI elements scale with the resolution as expected, including the scroll view itself.
     
    AlexandruFilipescu likes this.
  3. Sinbad3D

    Sinbad3D

    Joined:
    Oct 11, 2016
    Posts:
    17
    Twitter came to the rescue; for anyone else hitting this the answer was:
    Code (CSharp):
    1. button.transform.SetParent(content.transform, false);
    I thought the Layout Group was supposed to take control of positioning but clearly if you don't set `worldPositionStays=false` it can screw up.
     
  4. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    you can also pass the parent to the Instantiate() method. That would be a bit faster (however: performance is probably not a problem in your case).
     
    Extiward and lustgunther like this.
  5. Sinbad3D

    Sinbad3D

    Joined:
    Oct 11, 2016
    Posts:
    17
    Thanks! That's a bit tidier as well so worth doing.
     
    AlexandruFilipescu likes this.
  6. danerduo

    danerduo

    Joined:
    Nov 14, 2017
    Posts:
    5
    Same problem, save me a lot of time, Thanks
     
  7. dsharrock

    dsharrock

    Joined:
    Aug 29, 2018
    Posts:
    2
    I also had this issue, thanks for the fix!
     
  8. Chris_St

    Chris_St

    Joined:
    Apr 25, 2018
    Posts:
    19
    Omg, you're my lifesaver, i want to kiss you! I had the same problem, only the other way round, it got bigger on lower resolution. This little "false" did it!
    Spent a whole day on this trying to figure it out.
     
  9. mrsnipey_unity

    mrsnipey_unity

    Joined:
    Sep 11, 2020
    Posts:
    3
    Thanks dude. Was driving me nuts as well.