0
我有一堆精靈在不同的高度和寬度。我想讓他們擴大規模,以便他們不被拉伸。如何在圖像中縮放各種尺寸的Sprites大小?
現在每個圖像都會填滿空間。
public GameObject AnimationArea;
private float animationHeightBase;
private float animationWidthBase;
Image positionImage;
我首先獲取想要圖像出現的區域的基本大小。
void Start()
{
RectTransform rt = (RectTransform)AnimationArea.transform;
animationHeightBase = rt.rect.height;
animationWidthBase = rt.rect.width;
}
這部分選擇將顯示
void WhichSideUp()
{
switch (sideUp)
{
case 6:
height = imageSide5.bounds.size.y;
width = imageSide5.bounds.size.x;
positionImage.sprite = imageSide5; // Sprite to Image
Resize(); //Set the size of the image
break;
case 5:
height = imageSide4.bounds.size.y;
width = imageSide4.bounds.size.x;
positionImage.sprite = imageSide4; // Sprite to Image
Resize(); //Set the size of the image
break;
case 4:
SAME
case 3:
SAME
case 2:
SAME
case 1:
SAME
default:
break;
}
我試圖調整基於圖像如果高度大於寬度或寬度>高度大的圖像。
void Resize()
{
float imgScalex;
float imgScaley;
if(height > width)
{
float ratio = width/height;
imgScalex = (animationHeightBase * ratio);
imgScaley = animationHeightBase;
} else
{
float ratio = height/width;
imgScalex = animationWidthBase;
imgScaley = (animationWidthBase * ratio);
}
positionImage = new Vector2(imgScalex, imgScaley); //this is where I am failing
}
如果更改比例尺,則不應考慮寬高比,因此只需放置(0.5,0.5),寬高比都爲一半,縱橫比相同即可。 * .75f和* 1.5f沒有意義。 – Vancete