Vincent
Vincent

Reputation: 11

Unity 4.6 image scaled disappeared

I need to resize an image to be sure it is a square.

So I do

var rectTransform = GetComponent<RectTransform>();
var width = rectTransform.rect.width;
var height = rectTransform.rect.height;
if (Math.Abs(width - height) > 0.1f)
{
    float size = width <  height? width : height;
    transform.localScale = new Vector3(size/width,size/height);
}

When I play it I see that the rectTransform has the good size but the sprite as disappear an the image is now transparent, does somebody know about it?

Using unity V4.6b20

Upvotes: 0

Views: 899

Answers (1)

Vincent
Vincent

Reputation: 11

Problem solved by doing

var rectTransform = GetComponent<RectTransform>();
var width = rectTransform.sizeDelta.x;
var height = rectTransform.sizeDelta.y;
if (Math.Abs(width - height) > 0.1f)
{
    float size = width < height ? width : height;
    rectTransform.sizeDelta=new Vector2(size,size);
}

Upvotes: 1

Related Questions