Scale contents to fit parent

I’m trying to get a prefab to scale to fit its parent. My prefab looks something like this, in the prefab editor:

As you can see it has an aspect ratio fitter component, which surely works, but all it does is scale the rect transform, it does not change the scale of the contents, so I can only see the center of my prefab:

What I’d like to have instead is the actual contents scaled to fit the parent, like this:

Anyone have any ideas? I’ve attached the sample project.

3833179–323482–uicontentscalefit.unitypackage (8.09 KB)

In UI you do not (or should not) scale. And the UI components usually do not touch any scale.

Instead you need to set the anchors of the elements to have relative coordinates.
in your case you could also work with layout groups (horizontal or vertical) which will do the sizing for you.
If you buy my Asset “Better UI” (see signature) you could also use the “Better Grid Layout Group”.

Actually in this case I do want to scale. The example above is a bit contrived, in reality this is an animation that should play inside a panel. Using relative positions would make this a nightmare to author.

The animation is created as a prefab, and while creating it I don’t know the size of the panel. If I did, it wouldn’t be a problem as I could set that up in the prefab editor, but as it is now I can’t see any better option than just to create it at the size of the canvas in the prefab editor.

I solved this by just calculating and applying the scale when I instantiate the prefab from script, but if there is a better way I’d love to hear it.

1 Like