For the first time I really want to make a mobile game (iOs, Android, Tablets) and I stuck in the beginning. I have to solve an issue with responsiveness both in UI and in game
Problem:
Canvas lives in canvas-space with anchors and all this responsive stuff, GameObjects live in 2D/3D world and rely on position in world-space. I want to make my game look good without any clippings and stretches on variety of different mobile platforms. These platforms have different aspect-ratios, sometimes it’s more square-ish when it comes to tablets and sometimes it’s quite tall and aspect ratio varies from 4:3 to 16:9 (and some phones are even taller like iPhone 15 which AR is 19.5:9). I can fix canvas target resolution, make my UI controls anchored to some driving points, make sure they not overlap on reasonable aspect ratios, and adopt their size with respect to current device aspect ratio, but I don’t want to write gameplay logic and game objects using UI elements in 2D game which forces to use anchors instead of good old 3D positions. The issue is that Canvas lives in their own space, so that to map borders on my controls to world-point I have to do a lot of work with setting up canvas transforms (to represent edges of controls), map them onto world space via Camera screen point to ray APIs, it seems like quite a lot of work. I also had to write a terrible Sprite scaler that tries to scale game background image (done with Sprite Renderer) keeping its original aspect ratio and keeping some “anchor” points on this image to be snapped to world points so that any device has fixed left and right point of this background sprite the same in relation to borders (UI control and screen borders). It does the job quite bad because I can’t simply control rect of the sprite (like in in-editor Rect tool) instead I pondering on how much scale should I set to met these conditions. I also didn’t yet mention device safe zones in relation to which I should do all this math. Another issue is that I want my characters to move the same amount of space on each device, no matter what aspect ratio of the device screen is. I will quickly get lost in all these factors and multipliers to make character scaled correctly in relation to background size.
With that being said, I feel that it should not be that complex task because the engine is targeting mobile devices as well and a lot of mobile games are already made on it out there. What approach do you use when you need your UI to be boundaries of the game world and how do you fit it into safe area?
I also thought about making my mini UI-framework based on GameObjects so that I can zoom in/out my camera to clip it between needed bounds