10
votes

(Using Unity 4.6.0b20)

I've hit a problem where prefab button size works correctly when added in editor but seems to ignore Reference Resolution when added by script.

This uses a Canvas with Reference Resolution 1280x720 and MatchWidthOrHeight. Canvas has a Panel Vertical Layout Group for the buttons. The Button has Preferred Width/Height, and it is saved as a Prefab so new instances can be created from assets at runtime.

enter image description here

In the editor I can drag the prefab to scene to add instances to the panel which also has width 102 and they stack and scale nicely:

enter image description here

But when instead I add new instances of the prefab via script to the Panel they appear with the wrong size. Looking at the dimensions my guess is the 102 pixel size is not being scaled by the Reference Resolution:

enter image description here

The script code creates the instance via GameObject.Instantiate() and adds to the Panel by setting transform.parent:

GameObject uiInstance = (GameObject)GameObject.Instantiate(Resources.Load<GameObject>(assetPath));
uiInstance.transform.parent = unitButtonsPanel.transform;

I assume either there is more that needs to be done when adding the Button to the Panel than just setting Parent, or it's a bug with the beta...

Suggestions?

3
Not sure I understand what you mean "when it is exported? - mwk
nicely spotted - you're quite right. by default, when you Instantiate, the scaling defaults to zero in the rectTransform. staggeringly stupid move on unity's behalf - but then, that's Unity. - Fattie

3 Answers

27
votes

Use the method transform.SetParent passing false in the second parameter:

transform.SetParent(transform, false);

This way you can prevent world positioning and scaling. You can find more information in the unity manual, searching for "UICreateFromScripting".

3
votes

I found the cause -- when the prefab is added by script the Scale values are set incorrectly; in this test Scale was being set to 1.186284.

But if the script sets scale to 1.0 immediately after adding the button:

GameObject uiInstance = (GameObject)GameObject.Instantiate(Resources.Load<GameObject>(assetPath));
uiInstance.transform.parent = unitButtonsPanel.transform;
// HACK force scale to 1.0
uiInstance.transform.localScale = Vector3.one;

the new button instances are sized correctly.

imho this is a Unity bug because adding the prefab instance in the editor sets scale 1.0, the prefab itself has scale 1.0, so I can't see a reason why adding it from script should act differently. But I'm kind of guessing as there's not much documentation for the new UI yet :)

3
votes

This is not a bug, its an order of execution issue. When using Unity's new layout components the values are driven by the layout components, which get calculated once at the end of the frame that a layout rebuild call is made. If you're basing anything in a script off of the new rectTransform.rect calculations which are driven by the layout elements, you'll need to wait until this calculation has been performed or call it yourself by using

LayoutRebuilder.MarkLayoutForRebuild (transform as RectTransform);

You'll still have to wait until the end of the frame for those calculations to take place before using anything from the rectTransform like scale, pos, etc. This had me stumped for a while when I first start dynamically creating UI elements and the scale was zero depending upon when I instantiated them.

You can read about that rebuild call and the order of execution of the layout calc stuff here: http://docs.unity3d.com/Manual/UIAutoLayout.html