62 lines
1.2 KiB
C#
62 lines
1.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class CanvasScaleFactor : MonoBehaviour
|
|
{
|
|
private Canvas canvas;
|
|
|
|
private CanvasScaler canvasScaler;
|
|
|
|
private float currentFactor;
|
|
|
|
private Vector2 lastScreenRes = Vector2.zero;
|
|
|
|
public bool useHiddenCanvas;
|
|
|
|
private void Start()
|
|
{
|
|
canvas = GetComponent<Canvas>();
|
|
canvasScaler = GetComponent<CanvasScaler>();
|
|
SetScaleFactor();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
SetScaleFactor();
|
|
if (useHiddenCanvas)
|
|
{
|
|
if (GameManager.Instance.addectiveSceneLoaded != "none")
|
|
{
|
|
canvas.enabled = false;
|
|
}
|
|
else if (Singleton<QuickMenu>.Instance.isOn)
|
|
{
|
|
canvas.enabled = true;
|
|
}
|
|
else
|
|
{
|
|
canvas.enabled = InputManager.showUI;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SetScaleFactor()
|
|
{
|
|
if (!(canvasScaler == null) && !(lastScreenRes == new Vector2(Screen.width, Screen.height)))
|
|
{
|
|
float num = (float)Screen.width / (float)Screen.height;
|
|
if (num <= 1.77f)
|
|
{
|
|
currentFactor = (float)Screen.width / 1920f;
|
|
}
|
|
if (num > 1.77f)
|
|
{
|
|
currentFactor = (float)Screen.height / 1080f;
|
|
}
|
|
canvasScaler.scaleFactor = currentFactor;
|
|
lastScreenRes = new Vector2(Screen.width, Screen.height);
|
|
Debug.Log("Set canvas scale factor");
|
|
}
|
|
}
|
|
}
|