1

Can anyone explain why, in the editor I get:

enter image description here

while, on my Galaxy S6, I get:

enter image description here

My Panel UI element (which is the parent) has the GridLayoutGroup component, as well as the ContentSizeFitter. The child elements have a LayoutElement component with a FlexibleWidth/Height of 120 and all anchors are set to stretch in all directions.

The Canvas is set to Scale with Screen size, with a reference res of 320x480 and screen mode is to match height.

Regardless of the amount of squares (which is dynamic) the editor perfectly resizes child elements within the parent, but my device makes them tiny. I'm really confused.

There is no setting of sizes or anchors within the script, purely an instantiate and an assignment of parent, like so:

    for(int i=0; i < amount; i++)
    {
        GameObject emptyCell = (GameObject)Instantiate(emptyCellObject);
        emptyCell.transform.SetParent(cellHolder);
        emptyCell.GetComponent<RectTransform>().localPosition = new Vector3(0f, 0f, 0f);
        emptyCells.Add(emptyCell);
    }

I've also checked all docs related to: http://docs.unity3d.com/Manual/comp-UIAutoLayout.html - And even UI tutorial movies, as well as some blog posts about the 'new' UI tools, but nothing changes the dimensions on the physical device, and seems to change nothing in the editor as that remains the same.

I'm sure I'm doing something silly, but I've exhausted all avenues and was hoping someone had come across this oddity previously.

laminatefish
  • 5,197
  • 5
  • 38
  • 70

1 Answers1

6

Figured this out in the end. Was due to the parent object scaling the child elements (based upon screen space I'm assuming), which was counter-productive in this instance. My work around:

emptyCell.GetComponent<RectTransform>().localScale = Vector3.one;

...was to manually override the scaling myself and it works lovely on all devices now. Hope this helps someone out in the future :)

letroll
  • 1,059
  • 1
  • 14
  • 36
laminatefish
  • 5,197
  • 5
  • 38
  • 70
  • 1
    You can also fix this by doing `emptyCell.transform.SetParent(cellHolder, false);`. What's going on is that when you SetParent, it tries to keep the same size it was in world space even while being parented. It does that by modifying the localScale. Once you pass that `false` it will just keep the localScale numbers as they were (i.e. maintain 1,1,1) instead – Daniel May 13 '17 at 14:14