简体   繁体   中英

Unity3D - taking a screenshot of prefab

I've only just started to learn C# and Unity but I'm trying to get an image of a prefab so i can use it elsewhere in the game.

I came across the code here but truthfully I don't understand how to use it.

I've got my prefab, I added a canvas to it and then applied the code to the prefab but then how do i actually make it run?

I'd have commented on the original post but I'm not able to comment yet.

It looks the code on the link you provided, is setting up everything in void Start(). So when you press play in Unity it'll run through the code in the start method effectively taking the picture. You need to make sure that that the name of the object you added the CanvasScreenShot.cs is "GameObject". In line 10 of test.cs he attempts to find a GameObject with the name of "GameObject". Or in line 10 you can change that to the name of your GameObject. Also in

void receivePNGScreenShot(byte[] pngArray)
{
    Debug.Log("Picture taken");

    //Do Something With the Image (Save)
    string path = Application.persistentDataPath + "/CanvasScreenShot.png";
    System.IO.File.WriteAllBytes(path, pngArray);
    Debug.Log(path);
}

of test.cs you might find it easier to change the path value, to like your desktop or something or in your unity folder. Are you getting any errors in the console? Feel free to comment I'll try to help you best I can.

One method could be to use a Camera to achieve this. You can move the camera around, and change the culling mask on the camera in order to decide what it renders, and in that way you can tweak it so it only renders the objects in your scene that you want.

To display that image, you can use render texture to get a continuously updating texture. If you need to get a single image though, you will have to write some code, and I'm not sure what the best approach is for that.

As for getting a PNG or any image I don't know how to do that, but the code you linked looked like it had a good explanation written on how to use it. I'm still not quite sure why you would want a PNG unless you want the user to have a saved image after runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM