165 lines
6.2 KiB
C#
165 lines
6.2 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
namespace HK.FUJIFILM
|
||
{
|
||
public static class RawImageScaler
|
||
{
|
||
/// <summary>
|
||
/// 将Texture2D赋值给RawImage并确保等比缩放,保持在RawImage最大范围内
|
||
/// </summary>
|
||
/// <param name="rawImage">目标RawImage组件</param>
|
||
/// <param name="texture">要显示的Texture2D</param>
|
||
/// <param name="maxWidth">最大宽度限制(若为0则使用RawImage的当前宽度)</param>
|
||
/// <param name="maxHeight">最大高度限制(若为0则使用RawImage的当前高度)</param>
|
||
public static void SetTextureWithAspectRatio(RawImage rawImage, Texture2D texture, float maxWidth = 0,
|
||
float maxHeight = 0)
|
||
{
|
||
if (rawImage == null || texture == null)
|
||
{
|
||
Debug.LogError("RawImage或Texture2D不能为空");
|
||
return;
|
||
}
|
||
|
||
// 赋值纹理
|
||
rawImage.texture = texture;
|
||
|
||
// 获取纹理原始宽高
|
||
float textureWidth = texture.width;
|
||
float textureHeight = texture.height;
|
||
|
||
// 如果未指定最大宽高,使用RawImage当前的尺寸作为限制
|
||
float limitWidth = maxWidth > 0 ? maxWidth : rawImage.rectTransform.rect.width;
|
||
float limitHeight = maxHeight > 0 ? maxHeight : rawImage.rectTransform.rect.height;
|
||
|
||
// 计算宽高比例
|
||
float textureAspect = textureWidth / textureHeight;
|
||
float limitAspect = limitWidth / limitHeight;
|
||
|
||
// 计算缩放后的尺寸(保持比例并适应限制范围)
|
||
float scaledWidth, scaledHeight;
|
||
|
||
if (textureAspect > limitAspect)
|
||
{
|
||
// 纹理更宽,按宽度限制缩放
|
||
scaledWidth = limitWidth;
|
||
scaledHeight = scaledWidth / textureAspect;
|
||
}
|
||
else
|
||
{
|
||
// 纹理更高或比例相同,按高度限制缩放
|
||
scaledHeight = limitHeight;
|
||
scaledWidth = scaledHeight * textureAspect;
|
||
}
|
||
|
||
// 应用缩放后的尺寸
|
||
rawImage.rectTransform.sizeDelta = new Vector2(scaledWidth, scaledHeight);
|
||
|
||
// 可选:居中显示
|
||
rawImage.rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
|
||
rawImage.rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
|
||
rawImage.rectTransform.pivot = new Vector2(0.5f, 0.5f);
|
||
rawImage.rectTransform.anchoredPosition = Vector2.zero;
|
||
}
|
||
}
|
||
|
||
public static class ImageScaler
|
||
{
|
||
/// <summary>
|
||
/// 将Image等比缩放到不超过指定的最大尺寸(宽和高中较大的值不超过maxSize)
|
||
/// </summary>
|
||
/// <param name="image">需要缩放的Image组件</param>
|
||
/// <param name="maxSize">宽和高中较大值的最大限制</param>
|
||
/// <param name="keepOriginalIfSmaller">如果原图比限制小,是否保持原图大小</param>
|
||
public static void ScaleImageWithSize(Image image, float maxSize, bool keepOriginalIfSmaller = false)
|
||
{
|
||
if (image == null)
|
||
{
|
||
Debug.LogError("Image组件不能为空!");
|
||
return;
|
||
}
|
||
|
||
// 获取原始尺寸
|
||
float originalWidth, originalHeight;
|
||
|
||
if (image.sprite != null)
|
||
{
|
||
// 使用Sprite的原始尺寸
|
||
originalWidth = image.sprite.rect.width;
|
||
originalHeight = image.sprite.rect.height;
|
||
}
|
||
else
|
||
{
|
||
// 没有Sprite时使用当前RectTransform的尺寸
|
||
originalWidth = image.rectTransform.rect.width;
|
||
originalHeight = image.rectTransform.rect.height;
|
||
}
|
||
|
||
// 计算缩放比例
|
||
float scaleRatio = CalculateScaleRatio(originalWidth, originalHeight, maxSize, keepOriginalIfSmaller);
|
||
|
||
// 应用缩放后的尺寸
|
||
image.rectTransform.sizeDelta = new Vector2(
|
||
originalWidth,
|
||
originalHeight
|
||
);
|
||
// image.rectTransform.sizeDelta = new Vector2(
|
||
// originalWidth * scaleRatio,
|
||
// originalHeight * scaleRatio
|
||
// );
|
||
}
|
||
|
||
public static void ScaleImageWithMaxSize(Image image, float maxSize, bool keepOriginalIfSmaller = false)
|
||
{
|
||
if (image == null)
|
||
{
|
||
Debug.LogError("Image组件不能为空!");
|
||
return;
|
||
}
|
||
|
||
// 获取原始尺寸
|
||
float originalWidth, originalHeight;
|
||
|
||
if (image.sprite != null)
|
||
{
|
||
// 使用Sprite的原始尺寸
|
||
originalWidth = image.sprite.rect.width;
|
||
originalHeight = image.sprite.rect.height;
|
||
}
|
||
else
|
||
{
|
||
// 没有Sprite时使用当前RectTransform的尺寸
|
||
originalWidth = image.rectTransform.rect.width;
|
||
originalHeight = image.rectTransform.rect.height;
|
||
}
|
||
|
||
// 计算缩放比例
|
||
float scaleRatio = CalculateScaleRatio(originalWidth, originalHeight, maxSize, keepOriginalIfSmaller);
|
||
|
||
// 应用缩放后的尺寸
|
||
image.rectTransform.sizeDelta = new Vector2(
|
||
originalWidth * scaleRatio,
|
||
originalHeight * scaleRatio
|
||
);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 计算缩放比例
|
||
/// </summary>
|
||
private static float CalculateScaleRatio(float originalWidth, float originalHeight, float maxSize,
|
||
bool keepOriginalIfSmaller)
|
||
{
|
||
// 找到原始宽高中的较大值
|
||
float maxOriginal = Mathf.Max(originalWidth, originalHeight);
|
||
|
||
// 如果原图的最大边小于等于限制值,且需要保持原图大小,则不缩放
|
||
if (maxOriginal <= maxSize && keepOriginalIfSmaller)
|
||
{
|
||
return 1f; // 不缩放
|
||
}
|
||
|
||
// 计算缩放比例(让最大边刚好等于maxSize)
|
||
return maxSize / maxOriginal;
|
||
}
|
||
}
|
||
} |