Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Unity UI Button child resizing within one Canvas

Discussion in 'Unity UI & TextMesh Pro' started by Antistone, Oct 11, 2018.

  1. Antistone


    Feb 22, 2014
    I've got a complex scene with a bunch of UI in it. A certain group of buttons (all based on the same prefab) have mysteriously changed (in the editor, not at runtime) so that the child gameobject containing a TextMeshProUGUI (the button's label) is a different size.

    Specifically, the object with text has its anchors set to stretch on both axes with an offset of all zeroes (so it's always the same size and position as the parent), but the offsets are changing to be -100 on left and right and -25 on top and bottom, so that it's much larger than the parent. Since the text is fixed-size and centered, this doesn't affect the appearance; however, since the text is also a raycast target, it causes the button's clickable area to become MUCH larger than its visible area, which causes users to constantly click it without intending to.

    The prefab does not change; it still had offsets that are all zeroes. But a bunch of buttons all change so that they are suddenly overriding the size of the prefab.

    This is the second time I've noticed this happen. (The first time I went to every button manually, one at a time, and reset the text to use the prefab's values for its RectTransform.)

    I don't know exactly when this happened, because there is no visual change, so I don't realize anything has gone wrong until someone notices incorrect behavior during testing. As such, I have no clue how to reproduce it. But it's deeply worrying that my UI could completely break at any moment with no visible sign.

    The only patterns I've noticed are:

    1. All the affected buttons are using a prefab (I have two different button prefabs that were affected, but they're nearly identical--just different size and different sprites).
    2. But not ALL the buttons using those prefabs get broken--only those on a single Canvas.

    Following optimization advice from Unity, I'm using multiple Canvases to try to limit the amount of stuff that needs to update when something changes. In this particular part of my UI, I have:

    1. "Canvas": The main, top-level Canvas (with a Canvas Scaler attached)
    2. "Overlays": A child Canvas that displays HUD-style information and controls. This is on a gameobject that is a grandchild of the "Canvas" gameobject (but the intermediate game object does not have a canvas)
    3. "Panels": A direct child of "Overlays"

    The buttons that are under the "Overlays" canvas are affected, but the ones under the "Panels" canvas are not--the extra canvas component seems to be insulating them from the bug somehow. Buttons that are part of "Canvas" but not inside of "Overlays" are also not affected.

    I am running Unity 2017.1.1f1.

    Any theories why this is happening or how to prevent it?