init push 0.2.4
This commit is contained in:
64
Runtime/UI/AutoCanvasScale.cs
Normal file
64
Runtime/UI/AutoCanvasScale.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user