- Home /
Saving UI Image to file with transparent background
Hello, I need help in understand what i need to do to save an image properly.
Right now i've manged to save my UI image, and then load it from file and into a sprite. but the problem is, the image that i take, also contains the background of the element i wanted to save.
So what should i do in order to only show my character in the saved picture, without anything else?
Right now this is how i save:
var tex = new Texture2D((int)width, (int)height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(corners[0].x, corners[0].y, width, height), 0, 0);
Answer by JedBeryll · Dec 09, 2015 at 05:41 PM
If you use a camera to take the picture than set the camera's "clear flags" to don't clear.
The element I'm trying to take a picture of is a UI Image, and it's not in a camera (i think)
Do i have to change this Image to a sprite in the camera view? or is there another way?
As it's an ui element it has to be in the camera. (It doesn't have to be a child object or anything, based on the setting of it's canvas it can be anywhere). If it's an UI Image then your picture is aready a sprite, otherwise it's an UI RawImage.
Yes, i've realized this today.. $$anonymous$$y question was rather dumb :/
thanks for the help though!
Answer by tomhog · Dec 11, 2015 at 02:48 PM
Pretty sure your Texture format its wrong. Assuming where ever you're reading the pixels from has an alpha channel you need to use
Texture2D tex = new Texture2D((int)width, (int)height, TextureFormat.ARGB32, false);
Also ensure you save via
tex.EncodeToPNG();
As jpegs can't have alpha channels
Answer by KnightRiderGuy · Dec 11, 2015 at 03:07 PM
Not sure if its the same thing but in Photoshop you can save out an image with transparent areas as PNG and import that into Unity as a UI Sprite.