可设定canvas跟随屏幕缩放而缩放的组件,用于等比调节与标准UI的渲染缩放

This commit is contained in:
2023-12-08 17:05:45 +08:00
parent 4750ddc910
commit 04cdf14bb5
5 changed files with 112 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: cd531b4b89e02624cb1c5e28e82c927d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,67 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace XericLibrary.Runtime.Type
{
/// <summary>
/// 调整缩放以适配分辨率
/// </summary>
public class CanvasZoomAdjustmentBase : WeaklyMonoBase
{
#region 字段属性
protected CanvasScaler _canvasScaler;
/// <summary>
/// 当前目标屏幕的分辨率
/// </summary>
public static Vector2Int CurrentScreenResolution
=> new Vector2Int(Screen.currentResolution.width, Screen.currentResolution.height);
/// <summary>
/// 常规标准屏幕 16:9
/// </summary>
public static Vector2Int FullHDScreenResolution
=> new Vector2Int(1920, 1080);
/// <summary>
/// 常规标准屏幕 16:9
/// </summary>
public static Vector2Int QHDScreenResolution
=> new Vector2Int(2560, 1440);
#endregion
#region 生命周期
private void Awake()
{
_canvasScaler = transform.GetComponent<CanvasScaler>();
}
#endregion
#region MyRegion
/// <summary>
/// 获取与给定的标准宽度的缩放比例
/// </summary>
/// <param name="standardWidth"></param>
/// <returns></returns>
public static float ScreenScaleFactorWithWidth(int standardWidth = 1920)
=> (float)Screen.currentResolution.width / standardWidth;
/// <summary>
/// 获取与给定的标准高度的缩放比例
/// </summary>
/// <param name="standardHeight"></param>
/// <returns></returns>
public static float ScreenScaleFactorWithHeight(int standardHeight = 1080)
=> (float)Screen.currentResolution.height / standardHeight;
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a9e4512923ba0d64cb475d93f7c9f055
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+15
View File
@@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace XericLibrary.Runtime.Type
{
public class CanvasZoomStart : CanvasZoomAdjustmentBase
{
// Start is called before the first frame update
void Start()
{
_canvasScaler.scaleFactor = ScreenScaleFactorWithWidth();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5e5e480bc8648214891d46b029219a27
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: