From bdf1344b2601913b3e6290f77c8b91e23ea95418 Mon Sep 17 00:00:00 2001 From: GL <2365963573@qq.com> Date: Fri, 18 Oct 2024 23:06:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=8A=E5=A4=A7=E5=A4=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- meng_yao/Assets/communal/ugui.meta | 8 ++++ meng_yao/Assets/communal/ugui/adaptive_ui.cs | 38 +++++++++++++++++++ .../Assets/communal/ugui/adaptive_ui.cs.meta | 11 ++++++ 3 files changed, 57 insertions(+) create mode 100644 meng_yao/Assets/communal/ugui.meta create mode 100644 meng_yao/Assets/communal/ugui/adaptive_ui.cs create mode 100644 meng_yao/Assets/communal/ugui/adaptive_ui.cs.meta diff --git a/meng_yao/Assets/communal/ugui.meta b/meng_yao/Assets/communal/ugui.meta new file mode 100644 index 00000000..ad1530ad --- /dev/null +++ b/meng_yao/Assets/communal/ugui.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9b10b14aef60efe4097684860488530f +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/meng_yao/Assets/communal/ugui/adaptive_ui.cs b/meng_yao/Assets/communal/ugui/adaptive_ui.cs new file mode 100644 index 00000000..8e2bf427 --- /dev/null +++ b/meng_yao/Assets/communal/ugui/adaptive_ui.cs @@ -0,0 +1,38 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + +public class adaptive_ui : MonoBehaviour +{ + public RawImage rawImage; // 使用RawImage组件展示纹理 + public RectTransform boundaryRect; // 代表边界矩形的RectTransform + + void Start() + { + ScaleAndCropTexture(); + } + + void ScaleAndCropTexture() + { + Texture texture = rawImage.texture; + float textureWidth = texture.width; + float textureHeight = texture.height; + + float boundaryWidth = boundaryRect.rect.width; + float boundaryHeight = boundaryRect.rect.height; + + // 计算缩放比例,确保较短的一边适应边界矩形 + float scaleFactor = Mathf.Max(boundaryWidth / textureWidth, boundaryHeight / textureHeight); + + // 设置缩放后的尺寸 + float newWidth = textureWidth * scaleFactor; + float newHeight = textureHeight * scaleFactor; + + // 设置RawImage的RectTransform尺寸,使其居中并覆盖边界矩形 + rawImage.rectTransform.sizeDelta = new Vector2(newWidth, newHeight); + + // 裁剪超出的部分 + rawImage.rectTransform.anchoredPosition = Vector2.zero; // 确保居中 + } +} diff --git a/meng_yao/Assets/communal/ugui/adaptive_ui.cs.meta b/meng_yao/Assets/communal/ugui/adaptive_ui.cs.meta new file mode 100644 index 00000000..d071c8e3 --- /dev/null +++ b/meng_yao/Assets/communal/ugui/adaptive_ui.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8014c4cd1f84d6f4c9ed8324201ffcf7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: