As I started working on my game there were one or two things that I came across that I didn’t fully understand. Okay, there were a lot of those. But one or two I got to a “good enough” state and moved on. Well, that came to bite me in the ass recently. Specifically with the Canvas Scaler. Most of my game’s visual elements are UI objects, as such they scale to the camera to always be in a specified relative space on the screen. But how they scale is determined by a multitude of factors, starting with the Canvas Scaler.
I won’t get into the minutia of the Canvas Scaler here. But put simply you can choose to set a maximum size, minimum size, or a scaling style for the UI elements. Took me a while to decide what was best for my project, and I am liable to change it again. Before my recent dive into the Canvas Scaler, some objects would scale strangely and end up off screen. And then I figured out why… and it was dumb.
A common way to organize Unity project game assets is to group them under “empty game objects”. These objects are 1 dimensional points in space with no physics, but are useful for grouping objects to make the scene more manageable. They can also be used to apply a single transform (movement, scaling, rotation) to an entire group. I was using these empty game objects for both purposes. Making my scenes easier to manage and to move objects as a group. Unfortunately, UI elements and normal game objects don’t play nice together. And by grouping UI elements under an empty game object I disconnected their scaling from the Canvas Scaler. I needed a new solution.
Thankfully the solution was staring me in the face. I had already been moving UI elements independent of an empty game object, so I just needed to find a UI element that could stand in for one. And I did. It has a few kinks to work out (mostly from adopting it so late), but it shows promise.