0

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.

Community
  • 1
  • 1
Mick
  • 427
  • 4
  • 11
  • I'm not sure what you're asking... Why do you want a screenshot? What are you going to do with that?(You should be able to comment on your own questions) – Oscar Lundberg Mar 16 '17 at 05:30
  • I already said why, because I need the image to use elsewhere. – Mick Mar 16 '17 at 05:31
  • Well sure you said that but what on earth are you going to use a screenshot for? Are you making a 2D game and simply need a duplicate of a 2D element or do you need an actual screen dump? – Oscar Lundberg Mar 16 '17 at 05:33
  • the user places the prefab where they want it so I need a visual representation of the prefab ie the image of the unit so they actually know what theyre selecting. – Mick Mar 16 '17 at 05:34

2 Answers2

2

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.csyou 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.

bobbyanne
  • 86
  • 4
  • hmm, its not working as i expected. I changed the name to GameObject and its taking the image, but all im getting is what i could have already done by taking a screenshot myself.. I was hoping for a transparent background but its just taking a screenshot of the image with the default brown/grey background – Mick Mar 16 '17 at 06:26
  • Okay so in `CanvasScreenShot.cs` scroll down to line 110, this is where he sets up the size of the image to take. See where it says `Texture2d screenImage = new Texture2D(Screen.width, Screen.height);` this is grabbing the whole screen, change this to the size of the object on the canvas (the object you want to take a picture of). Then on line 114 change `Screen.width, Screen.height` to where the object is located on the canvas. This will take some trial and error. Check out [ReadPixels](https://docs.unity3d.com/ScriptReference/Texture2D.ReadPixels.html) – bobbyanne Mar 16 '17 at 06:43
  • I may have found something to make the background transparent. I'll check up on it tomorrow. – bobbyanne Mar 16 '17 at 07:11
0

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.

Oscar Lundberg
  • 433
  • 4
  • 14