Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Camera))]
public class Cam : MonoBehaviour
{
// Set this to your target aspect ratio, eg. (16, 9) or (4, 3).
public Vector2 targetAspect = new Vector2(16, 9);
Camera _camera;
private CanvasScaler lev;
public static int rat;
void Start()
{
_camera = GetComponent<Camera>();
lev = GameObject.FindGameObjectWithTag("lev").GetComponent<CanvasScaler>();
UpdateCrop();
}
public void UpdateCrop()
{
float screenRatio = Screen.width / (float)Screen.height;
float targetRatio = targetAspect.x / targetAspect.y;
if (Mathf.Approximately(screenRatio, targetRatio))
{
lev.matchWidthOrHeight = 1f;
rat = 1;
_camera.rect = new Rect(0, 0, 1, 1);
}
else if (screenRatio > targetRatio)
{
lev.matchWidthOrHeight = 1f;
rat = 1;
float normalizedWidth = targetRatio / screenRatio;
float barThickness = (1f - normalizedWidth) / 2f;
_camera.rect = new Rect(barThickness, 0, normalizedWidth, 1);
}
else
{
lev.matchWidthOrHeight = 0f;
rat = 2;
float normalizedHeight = screenRatio / targetRatio;
float barThickness = (1f - normalizedHeight) / 2f;
_camera.rect = new Rect(0, barThickness, 1, normalizedHeight);
}
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Camera))]
public class Cam : MonoBehaviour
{
// Set this to your target aspect ratio, eg. (16, 9) or (4, 3).
public Vector2 targetAspect = new Vector2(16, 9);
Camera _camera;
private CanvasScaler lev;
public static int rat;
void Start()
{
_camera = GetComponent<Camera>();
lev = GameObject.FindGameObjectWithTag("lev").GetComponent<CanvasScaler>();
UpdateCrop();
}
public void UpdateCrop()
{
float screenRatio = Screen.width / (float)Screen.height;
float targetRatio = targetAspect.x / targetAspect.y;
if (Mathf.Approximately(screenRatio, targetRatio))
{
lev.matchWidthOrHeight = 1f;
rat = 1;
_camera.rect = new Rect(0, 0, 1, 1);
}
else if (screenRatio > targetRatio)
{
lev.matchWidthOrHeight = 1f;
rat = 1;
float normalizedWidth = targetRatio / screenRatio;
float barThickness = (1f - normalizedWidth) / 2f;
_camera.rect = new Rect(barThickness, 0, normalizedWidth, 1);
}
else
{
lev.matchWidthOrHeight = 0f;
rat = 2;
float normalizedHeight = screenRatio / targetRatio;
float barThickness = (1f - normalizedHeight) / 2f;
_camera.rect = new Rect(0, barThickness, 1, normalizedHeight);
}
}
}
Почему такое может быть?
На остальных устройствах, в том числе и в юнити с этими же разрешениями экрана все норм.