Search Unity

Question What is the best method to grab a piece of the UI canvas?

Discussion in 'UGUI & TextMesh Pro' started by nickhe2003, Feb 10, 2022.

  1. nickhe2003

    nickhe2003

    Joined:
    Jun 20, 2020
    Posts:
    15
    Hello everyone! I've done 12 hours of research, and I've successfully created a UI page where the user can zoom and drag images to be turned into sprites (using a mask, planning to crop out the square part inside the frame (the mask) to be a new sprite, from texture->sprite). However, I'm stuck on the valid way of doing so:
    1. use some math formula and arrays to create a new texture from scratch, somehow record the zoomed into and offsetted pixel positions and fill up the rest of the texture with white pixels
    2. use ReadPixels to directly grab off of the box (I couldn't get it to work, keep getting "why drawing" errors... maybe world coordinates instead of canvas idk?)
    3. use Render to texture and read off of texture (how would I do that in a practical way with UI)?
    I just want to save the square region within the frame... Thank you do much for your time and help!
    upload_2022-2-10_5-38-20.png
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,463
    The UI forums are here. I'll move your post for you.
     
  3. nickhe2003

    nickhe2003

    Joined:
    Jun 20, 2020
    Posts:
    15
    I JUST FREAKING SOLVED IT LETS GOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!!!!

    Basically, if you encounter this type of stuff in the future, make a camera canvas somewhere and reference the overlay canvas image with ratio (same size scaling). Basically you want to copy and project exact same thing onto the camera canvas's raw image. Then you use that specifically sized camera viewport to "crop" the image by directing the output onto a rendering texture and turn that texture into sprite ;DDDDDDDDDDDDDDDDDDDD