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