/// <summary>
/// 设置通用Item大小
/// </summary>
/// <param name="imgIcon"></param>
/// <param name="imgQua"></param>
public static void SetCommonItemSize(Image imgIcon, Image imgQua,float scale = 1)
{
if (imgIcon == null || imgQua == null) return;
float iconSize = 90f * scale;
float quaSize = 105f * scale;
var iconrect = imgIcon.GetComponent<RectTransform>();
if (iconrect.anchorMin.x == iconrect.anchorMax.x && iconrect.anchorMin.y == iconrect.anchorMax.y)
{
iconrect.sizeDelta = Vector2.one * iconSize;
}
else
{
//适配满铺的处理
Vector2 size = iconrect.rect.size;
size.x -= iconrect.offsetMax.y;
size.x += iconrect.offsetMin.y;
size.y -= iconrect.offsetMax.x;
size.y += iconrect.offsetMin.x;
//算出实际长宽
//LogTool.EditorError($"iconSize = {iconSize},size = {size},iconrect.rect.size = {iconrect.rect.size}");
iconrect.offsetMax = new Vector2((iconSize - size.y)/2f, (iconSize - size.x) / 2f);
iconrect.offsetMin = new Vector2((size.x - iconSize)/2f, (size.y - iconSize) / 2f);
}
var quarect = imgQua.GetComponent<RectTransform>();
if (quarect.anchorMax == Vector2.one / 2f && quarect.anchorMin == Vector2.one / 2f && quarect.pivot == Vector2.one / 2f)
{
quarect.sizeDelta = Vector2.one * quaSize;
}
else
{
Vector2 size = quarect.rect.size;
size.x -= quarect.offsetMax.y;
size.x += quarect.offsetMin.y;
size.y -= quarect.offsetMax.x;
size.y += quarect.offsetMin.x;
quarect.offsetMax = new Vector2((quaSize - size.y) / 2f, (quaSize - size.x)/ 2f);
quarect.offsetMin = new Vector2((size.x - quaSize) / 2f, (size.y - quaSize)/ 2f);
}
}
UGUI适配不同通用物品大小
于 2024-08-22 14:26:46 首次发布