I don’t know how to solve this problem. I need to save prefab’s height and width proportions and distance between them. Hope you can help me and thanks a lot for any help
This will depend how you want to handle this for your game. I see in your Game Window you have it set to ‘Free Aspect’ the game when you build it out and test it will play at a selected resolution. I would recommend working in a popular resolution, then when you build it out, only allowing resolutions that match the aspect ratio which you created it in.
There are other tricks you can deploy too, if you want it to look the same for everyone no matter what. These can get rather complex and it might be easier to just force players to use the resolution which the game was created for - not the ideal solution. This can be worked around by forcing the camera to use the aspect ratio which you created the game with but then calculating it in a way so no matter what, the end user will be using the proper resolution / aspect ratio so they see the game how you are intending.
Also in the player settings - Edit > Project Settings > Player > Supported Aspect Ratios, you can toggle which ratios you want to support.
For a baseline, change the Game window to 16:9 - then create this script and attach it to your camera. This script will force the camera to always display 16:9 aspect ratio no matter what, even if the display is not 16:9 it will force it to fit.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraAspect : MonoBehaviour {
// Use this for initialization
void Start () {
// set the desired aspect ratio (the values in this example are
// hard-coded for 16:9, but you could make them into public
// variables instead so you can set them at design time)
float targetaspect = 16.0f / 9.0f;
// determine the game window's current aspect ratio
float windowaspect = (float)Screen.width / (float)Screen.height;
// current viewport height should be scaled by this amount
float scaleheight = windowaspect / targetaspect;
// obtain camera component so we can modify its viewport
Camera camera = GetComponent<Camera>();
// if scaled height is less than current height, add letterbox
if (scaleheight < 1.0f)
{
Rect rect = camera.rect;
rect.width = 1.0f;
rect.height = scaleheight;
rect.x = 0;
rect.y = (1.0f - scaleheight) / 2.0f;
camera.rect = rect;
}
else // add pillarbox
{
float scalewidth = 1.0f / scaleheight;
Rect rect = camera.rect;
rect.width = scalewidth;
rect.height = 1.0f;
rect.x = (1.0f - scalewidth) / 2.0f;
rect.y = 0;
camera.rect = rect;
}
}
}
You can them simply change the math for the targetaspect variable if you want to use another aspect ratio, or even if you want to switch from horizontal to vertical.
Thanks, but it still doesn’t work correctly. I’ve changed the targetaspect to 12:16, because I need the vertical view, and tried to run the game in my phone: