I would like to ensure that whatever the screen resolution my game is set to the best size even if there are empty places: In fact on this site their “problem” (at the beginning) is in fact what i want to do https://awesometuts.com/blog/support-mobile-screen-sizes-unity/?utm_medium=video&utm_source=youtube&utm_campaign=how_to_make_your_game_look_the_same_on_all_mobile_screen_sizes
I want there to be gaps on the left and right sides of the phone if the background is not long enough to fill those gaps
I try to do it but it’s not working… maybe because i use à canevas :
using UnityEngine;
public class FitWorkgroundToCamera : MonoBehaviour
{
private Camera mainCamera;
void Start()
{
mainCamera = Camera.main;
transform.position = new Vector3(mainCamera.transform.position.x, mainCamera.transform.position.y, 0);
Vector3 bottomLeft = mainCamera.ViewportToWorldPoint(Vector3.zero) * 100;
Vector3 topRight = mainCamera.ViewportToWorldPoint(new Vector3(mainCamera.rect.width, mainCamera.rect.height)) * 100;
Vector3 screenSize = topRight - bottomLeft;
float screenRatio = screenSize.x / screenSize.y;
float desiredRatio = transform.localScale.x / transform.localScale.y;
if (screenRatio > desiredRatio)
{
float height = screenSize.y;
transform.localScale = new Vector3(height * desiredRatio, height);
} else
{
float width = screenSize.x;
transform.localScale = new Vector3(width, width / desiredRatio);
}
}
}