Search Unity

Resolved Get rect reflecting the non-transparent portion of a UI image?

Discussion in 'Scripting' started by ADNCG, Mar 18, 2022.

  1. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    Basically I'm trying to dynamically determine the size(in canvas space preferably) of the non transparent portion of a given UI image.

    I think unity knows this since filled images will fill according to the visible portion of an image. For instance create a 128x128 transparent sprite and center in a 64x64 opaque square in the program of your choice. Then make the image filled, change the fill amount and you'll notice it starts and ends where the center square starts and ends, rather than at the image bounds.

    Edit: For clarity, by "visible portion of an image", I mean imagine what's left of the image after every pixels where the alpha is 0 has been clipped.

    Edit2 [SOLVED]: Was able to get the rect I needed from sprite.textureRect and then remap it to canvas space
     
    Last edited: Mar 18, 2022