2017-01-02 139 views
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

我不得不做一些工作,並稍微改變代碼。我最終改變了圖像的大小,改變了大小。它似乎工作得很好。我也必須玩這個比例,以便看起來很好。

void Resize() 
{ 
    float imgScalex; 
    float imgScaley; 
    if(height > width) 
    { 
     float ratio = width/height;   
     imgScaley = 1; 
     imgScalex = ratio * .75f; /added the .75 because it wasn't scaling properly 
    } else 
    { 
     float ratio = height/width; 
     imgScaley = ratio * 1.5f; /added the 1.5 because it wasn't scaling properly 
     imgScalex = 1; 
    } 
    positionImage.transform.localScale = new Vector2(imgScalex, imgScaley); 
+0

如果更改比例尺,則不應考慮寬高比,因此只需放置(0.5,0.5),寬高比都爲一半,縱橫比相同即可。 * .75f和* 1.5f沒有意義。 – Vancete

相關問題