Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Grid Layout Group completely ignores Canvas Scaler? [SOLVED]

Discussion in 'UGUI & TextMesh Pro' started by TwiiK, Nov 10, 2016.

  1. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    Edit: Okay, so it wasn't a glaring bug as I initially thought, it was just a minor road block. Check the next reply for my solution. My apologies. :)

    So I thought I would try out the new UI some more, but I feel like it takes me just a couple of hours to discover a glaring issue I feel should never be there. :/

    Here's a screenshot from my UI at reference scale:


    This is how it looks when I scale the game window up:


    And this is how it looks when I scale the game window down:


    Obviously completely wrong. Wasn't this UI released in Unity 4.6? Why hasn't this been fixed? Do I have to script it to make it respect my screen size? Why can't stuff just work? :p Wasn't that Unity's slogan at one point in time?

    When Googling this I found dozens of other people asking about this in various forms as well, but no actual answers from what I could see. Has it always been broken and nobody has bothered to fix it?

    If there actually is something I'm doing wrong here feel free to let me know, but everything else behaves intuitively as I would expect them too so I have a hard time seeing why I would suddenly use the the Grid Layout Group completely wrong. But the way it works for me now makes no sense at all. It feels like it would be harder to make it behave like this than it would be to have it properly scale.
     
    Last edited: Nov 11, 2016
  2. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    Okay, thankfully as I was creating a repro project and preparing to report this a bug I figured out what was going on. Apparently you have to reset localScale on the elements in the Grid Layout after you instantiate them through code because it gets completely messed up if not.

    This is my code now.
    Code (csharp):
    1. for (int i = 0; i < slotAmount; i++) {
    2.     slots.Add(Instantiate(inventorySlot));
    3.     slots[i].transform.SetParent(slotPanel.transform);
    4.     slots[i].transform.localScale = Vector3.one; // I had to add this line because localScale was being set to a seemingly random number otherwise.
    5. }
    So I guess I overreacted yesterday, but at least it's solved now. :)

    Not sure if the localScale thing is a bug or not though, but it was easy enough to solve so I don't care. I don't really understand why it messes with localScale if I instantiate the element through code and not when I'm adding it normally through the editor.
     
  3. Oakwarrior

    Oakwarrior

    Joined:
    Dec 10, 2013
    Posts:
    1
    Thanks for the solution. This is still an issue. How come? :/
     
  4. ncltg

    ncltg

    Joined:
    Jun 29, 2014
    Posts:
    1
    Still an issue but this solution works fine. Thanks !
     
  5. AmazingRuss

    AmazingRuss

    Joined:
    May 25, 2008
    Posts:
    933
    This is gold! Thanks!
     
  6. modernator24

    modernator24

    Joined:
    Apr 7, 2017
    Posts:
    168
    You saved my life man!
     
  7. sietse85

    sietse85

    Joined:
    Feb 22, 2019
    Posts:
    99
  8. gisinator

    gisinator

    Joined:
    Jan 23, 2021
    Posts:
    11
    Wow, would have never found this out. Thank you, @TwiiK, you saved my day!
     
  9. uncouthgames

    uncouthgames

    Joined:
    Feb 7, 2021
    Posts:
    1
    Still the solution, 6 years later. A huge thank you!