- Home /
 
Take photo in unity
Hey, I'm trying to build a program that takes your photo and places it with a different background, like a monument or so. So far, I was able to turn the camera on when I start the project with this code
     webcamTexture = new WebCamTexture();
     rawImage.texture = webcamTexture;
     rawImage.material.mainTexture = webcamTexture;
     webcamTexture.Play();
     Texture2D PhotoTaken = new Texture2D (webcamTexture.width, webcamTexture.height);
     PhotoTaken.SetPixels (webcamTexture.GetPixels ());
     PhotoTaken.Apply ();
 
               However, I can't take a screenshot or photo because it always ends up all black. I've tried different codes but nothing is working. Can someone please help? Thanks
Answer by BodhiPurplePill · Apr 07, 2017 at 03:34 PM
Try
RenderTexture PhotoTaken = new RenderTexture(webcamTexture.width, webcamTexture.height,0); Graphics.Blit(webcamTexture,PhotoTaken);
After that, convert the RenderTexture to Texture2D to save it.
I have this now, and it takes the screenshot, but it's all white:
 void Start () {
     webcamTexture = new WebCamTexture();
     rawImage.texture = webcamTexture;
     rawImage.material.mainTexture = webcamTexture;
     webcamTexture.Play();
     RenderTexture PhotoTaken = new RenderTexture(webcamTexture.width, webcamTexture.height,0); 
     Graphics.Blit(webcamTexture, PhotoTaken);
 }
     
 void Update(RenderTexture source) {
     var width = 400;
     var height = 300;
     RenderTexture.active = source;
     Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
     texture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
     texture.Apply();
     Texture2D PhotoTaken = new Texture2D (width, height);
     // Encode texture into PNG
     var bytes = PhotoTaken.EncodeToPNG();
     Destroy(PhotoTaken);
     File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
 }
                 Your answer
 
             Follow this Question
Related Questions
Display Game Center photo in iOS game 0 Answers
Save Image to Android Photo gallery 1 Answer
How to save screenshots on Android with Unity and C#? 0 Answers
webcamtexture makes unity 5 crash 1 Answer
How to make a WebCamTexture 'run'? 0 Answers