获取一个UI的大小
public static Vector2 GetSize(RectTransform rect, Space space = Space.Self)
{
var size = rect.rect.size;
if(space == Space.World)
{
var canvasList = rect.GetComponentsInParent<Canvas>();
var canvas = canvasList[canvasList.Length - 1];
return MathTools.Division(MathTools.Multiple(size, rect.lossyScale), canvas.transform.lossyScale);
}
return size;
}
2. 设置一个UI的大小
public static void SetSize(RectTransform rect, Vector2 targetSize, Space space = Space.Self)
{
if(space == Space.World)
{
var canvasList = rect.GetComponentsInParent<Canvas>();
var canvas = canvasList[canvasList.Length - 1];
targetSize = MathTools.Multiple(MathTools.Division(targetSize, rect.lossyScale), (canvas.transform).lossyScale);
}
var originSize = rect.rect.size - rect.sizeDelta;
var makeSizeDelta = targetSize - originSize;
rect.sizeDelta = makeSizeDelta;
}
联系客服