Search Unity

Getting stretched image UI object bounding rect?

Discussion in 'UGUI & TextMesh Pro' started by sysameca, Nov 6, 2016.

  1. sysameca

    sysameca

    Joined:
    Mar 2, 2013
    Posts:
    99
    I have a UI Image object which is stretched with layout element component. However I can't get the real rectangle bounds/size of the sprite.Neither sizeDelta nor rect.width/hight give me the correct result of the UI element. Does anybody have an idea how the sizes be retrieved?

    Untitled-1.png
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi there,

    to be honest, I just wonder, do you really need that info :) ?

    I'm not sure if you can directly get UI Image sprite actual canvas sizes...

    But you can work it out;

    You can get the actual sprite size (like 32x64), you know the RectTransform size.

    When RT is size 100x400, if you have preserve Aspect on, sprite width must be that same 100, and height is proportional to width that is now known, so you can get the value from here then and you get the canvas height for sprite.

    Same for the case where RT is wider than it is tall but using different axis.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Images are made up of a sprite. So you'll probably need to get the sprite itself.
    https://docs.unity3d.com/ScriptReference/Sprite.html

    Try targetting the sprite on the image. GetComponent<Image>().sprite
    Looks like the sprite has a rect on it. So probably look at that.
     
  4. sysameca

    sysameca

    Joined:
    Mar 2, 2013
    Posts:
    99
    @Brathnann That will give me the sprite dimensions overall not when stretched in the UI canvas. As eses wrote It came in mind I can get the proper result by multiplying the real sprite aspect (width/height) by the rect sizes according to the axis size. So for example if the rect is overstretched on it's width the height proportion will be always the same as the real sprite height. So in this case I just multiply the width by the real sprite aspect. Just thought there will be an easier way to get the canvas image dimensions. But how can I figure when the image is stretched by it's width or by it's height? That seems too complicated for just a simple demand as in my case.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Ah, my mistake. I thought you were trying to get the original dimensions. But you want the dimensions based on the sprite when it doesn't match the recttransform size due to preserve aspect. My mistake, I misunderstood.
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @sysameca I think you could try something like this:

    Code (csharp):
    1.  
    2.   if (image.rectTransform.sizeDelta.x > image.rectTransform.sizeDelta.y)
    3.   {
    4.   var mul = (image.rectTransform.sizeDelta.y / image.sprite.rect.height);
    5.   spriteWidth = image.sprite.rect.width * mul;
    6.   spriteHeight = image.rectTransform.sizeDelta.y;
    7.   }
    8.   else
    9.   {
    10.   var mul = (image.rectTransform.sizeDelta.x / image.sprite.rect.width);
    11.   spriteWidth = image.rectTransform.sizeDelta.x;
    12.   spriteHeight = image.sprite.rect.height * mul;
    13.   }
    14.