64 lines
1.5 KiB
C#
64 lines
1.5 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
using XericLibrary.Runtime.CustomEditor;
|
|||
|
|
|
|||
|
|
namespace Deconstruction.Runtime.UI
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD><C8A1>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public class AutoCanvasScale : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
#region <EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|
|||
|
|
public CanvasScaler canvasScaler;
|
|||
|
|
|
|||
|
|
[Rename("<22><><EFBFBD>Ƴߴ<C6B3>")]
|
|||
|
|
public Vector2 StandardSize = new Vector2(1920,1080);
|
|||
|
|
|
|||
|
|
public Vector2 ScaleMinMax = new Vector2(0.5f, 2);
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|
|||
|
|
private void OnValidate()
|
|||
|
|
{
|
|||
|
|
FindCanvasScaler();
|
|||
|
|
GetScreenScale();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void Awake()
|
|||
|
|
{
|
|||
|
|
FindCanvasScaler();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void LateUpdate()
|
|||
|
|
{
|
|||
|
|
canvasScaler.scaleFactor = GetScreenScale();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|
|||
|
|
private void FindCanvasScaler()
|
|||
|
|
{
|
|||
|
|
if (canvasScaler == null)
|
|||
|
|
canvasScaler = transform.GetComponent<CanvasScaler>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private float GetScreenScale()
|
|||
|
|
{
|
|||
|
|
Vector2 screenSize = new Vector2(Screen.width, Screen.height);
|
|||
|
|
// Debug.Log(canvasScaler.referenceResolution);
|
|||
|
|
var sf = screenSize / StandardSize;
|
|||
|
|
return Mathf.Clamp(Mathf.Min(sf.x, sf.y), ScaleMinMax.x, ScaleMinMax.y);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
}
|