Why does a prefab of a UI component get its own canvas (environment) that can't be resized?

Why does a prefab of a UI component get its own canvas (environment) that can’t be resized? I don’t understand why it can’t be resized.


I don’t like this, because it assumes that if I put more content, it has to be squished to fit in the canvas, but when I put this prefab in the scene, there’s plenty of room below it and I want it to take up that space.

Before Unity 2018.3 there was no way to display a UI prefab (or at least I found no way). I actually wrote a prefab editor for me with a preview. I couldn’t get it running for UI objects because they need to be placed in a canvas which was not possible in a preview scene.

It is still true that a canvas is necessary in order to display a UI prefab. That is why there is this canvas (environment) object which actually does not belong to the prefab itself.
It probably has the current size of the game window (didn’t test it myself).

The reason why it doesn’t fit to your content is anchoring. If the anchors of the prefab are not all at the same point (which should be the case if you are creating a resolution independent UI), it cannot be calculated which size the object has. It is dependent on the parent.
Therefore it is simply not possible to trim the canvas to the size of the prefab.

However, what would be possible in theory is an option for the user to resize the canvas manually.
But this should not be stored inside the canvas itself. Probably that is the reason why Unity doesn’t allow this either.

1 Like