• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
40
Question by crasyboy42 · Jul 21, 2010 at 08:54 AM · camerascreenshotpicture

How to save a picture (take screenshot) from a camera in game?

it is possible for a camera to take a picture and save it as jpg? I could not find on google or unity.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cregox · Dec 12, 2012 at 01:53 PM 0
Share

interesting article about 3 ways of doing it: http://ralphbarbagallo.com/2012/04/09/3-ways-to-capture-a-screenshot-in-unity3d/

avatar image cregox · May 16, 2013 at 04:51 PM 0
Share

By the way, asset store has now a script to do it and store it in iOS and Android gallery. Great responsive developer behind it, I've used it and approved: http://u3d.as/content/ryansoft/gallery-screenshot/4pz

avatar image lazenska-wewerka · Oct 13, 2018 at 03:08 PM 0
Share

Hi. Is it possible to make a screenshot with a transparent background? Thank you. Wewerak

avatar image DeveshPandey · Jul 22, 2019 at 06:34 AM 0
Share

Yes, you can use the way as answered by $$anonymous$$ate-O, which is a good and easy solution to implement. But if you want to see your photo in device native gallery then you have to transfer that screenshot into native gallery, for that you can write native code (java for android & objective-c/swift for ios) to do that, which is little complicated.

If you don't have time to explore too much then you can choose use any plugin, Capture and Save - its good plugin that will also fulfil your need, it may help you.

11 Replies

· Add your reply
  • Sort: 
avatar image
80
Best Answer

Answer by jashan · Jul 21, 2010 at 11:12 AM

Yes, it is possible. And there's even a couple of different approaches you could use:

There's a script for taking screenshots on the Unifycommunity Wiki: TakeScreenshot

The method used to do this is: Application.CaptureScreenshot, [EDIT: Which also has a parameter superSize for high resolution screenshots using an integer multiple of the current screen resolution]

There's also an approach to do [-high resolution-] screenshots [EDIT: with any arbitrary resolution]. I have a script which is based on the script posted in Submitting featured content. This will take a screenshot and store it to disk when you press the key "k" (you could change the key by changing the code). You have to attach the script to a camera for it to work:

 using UnityEngine;
 using System.Collections;
 
 public class HiResScreenShots : MonoBehaviour {
     public int resWidth = 2550; 
     public int resHeight = 3300;
 
     private bool takeHiResShot = false;
 
     public static string ScreenShotName(int width, int height) {
         return string.Format("{0}/screenshots/screen_{1}x{2}_{3}.png", 
                              Application.dataPath, 
                              width, height, 
                              System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
     }
 
     public void TakeHiResShot() {
         takeHiResShot = true;
     }
 
     void LateUpdate() {
         takeHiResShot |= Input.GetKeyDown("k");
         if (takeHiResShot) {
             RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
             camera.targetTexture = rt;
             Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
             camera.Render();
             RenderTexture.active = rt;
             screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
             camera.targetTexture = null;
             RenderTexture.active = null; // JC: added to avoid errors
             Destroy(rt);
             byte[] bytes = screenShot.EncodeToPNG();
             string filename = ScreenShotName(resWidth, resHeight);
             System.IO.File.WriteAllBytes(filename, bytes);
             Debug.Log(string.Format("Took screenshot to: {0}", filename));
             takeHiResShot = false;
         }
     }
 }

It also shouldn't be too hard to post those screenshots to combine this with the example code in the WWWForm-documentation to post those screenshots to a server.

Comment
Add comment · Show 26 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Case23 · Jul 21, 2010 at 04:28 PM 1
Share

I dont now if this is a common issue, but i had to use the function in the LateUpdate call, some of my objects, which i create with Graphics.Draw$$anonymous$$esh() were not on the screenshot.

avatar image jashan · Jul 21, 2010 at 10:42 PM 2
Share

Good point - I've changed the code to use LateUpdate() ins$$anonymous$$d of Update() to be on the safe side.

avatar image jashan · Dec 11, 2012 at 01:58 PM 1
Share

@Cawas: Not sure if the parameter "superSize : int = 0" was added later to Application.CaptureScreenshot, or whether I missed it when I wrote the original answer. In most cases, using CaptureScreenshot is probably the best way to do it. An advantage of the other script may be that you can use any resolution, not just multiples of the current screen resolution. I'll edit the wording to reflect that change.

avatar image jastit · Oct 07, 2015 at 12:50 PM 1
Share

the supersize parameter in Application.CaptureScreenshot is not really useful because it only enlarges the pixels - it does not provide higher quality. So this provided script ist excellent for taking screenshots for resolutions that don't fit on my own monitor. Thanks you! One disadvantage: The UI Layer is not on the screenshot. Any idea how to also add the UI Layer this way?

avatar image Rares15 · Nov 20, 2016 at 08:51 AM 1
Share

How i can do to see and ui in the screenshoot?

avatar image Maxence_Marchand Rares15 · May 09, 2018 at 01:40 PM 0
Share

Put the UI on the same layer as the camera (search for something called "Layer" in the top of the Inspector of your UI, set it to 0, it should work. Sorry for the late answer

Show more comments
avatar image
4

Answer by Julian-Glenn · Jul 21, 2010 at 08:58 AM

You could try this:

http://unity3d.com/support/documentation/ScriptReference/WWWForm.html

Which from the link in this Answer: http://answers.unity3d.com/questions/292/is-is-possible-to-output-screenshots-from-unity

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
11

Answer by Mate-O · May 20, 2013 at 08:49 AM

  1. Create a render texture.

  2. Apply it on the camera.

  3. Reference the camera in the script

  4. Set the render texture as active RenderTexture.active

  5. Create a Texture2D to which you dump the current RenderTexture

  6. Put back the original RenderTexture.active back. Things to worry about: PNG files store alpha channels as well as any other color information. Sometimes some object might not appear because they simply have that information on it. Most of the time the Texture2D that you need has to be simply of type RGB24, not ARGB32.

Here is a code snippet that should help you:

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class ScreenCapture : MonoBehaviour
 {
     public RenderTexture overviewTexture;
     GameObject OVcamera;
     public string path = "";
 
     void Start()
     {
         OVcamera = GameObject.FindGameObjectWithTag("OverviewCamera");
     }
 
     void LateUpdate()
     {           
         if (Input.GetKeyDown("f9"))
         {
             StartCoroutine(TakeScreenShot());
         }    
     }
 
     // return file name
     string fileName(int width, int height)
     {
        return string.Format("screen_{0}x{1}_{2}.png",
                              width, height,
                              System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
     }
 
     public IEnumerator TakeScreenShot()
     {
         yield return new WaitForEndOfFrame();
 
         Camera camOV = OVcamera.camera;  
         RenderTexture currentRT = RenderTexture.active;    
         RenderTexture.active = camOV.targetTexture;
         camOV.Render();
         Texture2D imageOverview = new Texture2D(camOV.targetTexture.width, camOV.targetTexture.height, TextureFormat.RGB24, false);
         imageOverview.ReadPixels(new Rect(0, 0, camOV.targetTexture.width, camOV.targetTexture.height), 0, 0);
         imageOverview.Apply();
         RenderTexture.active = currentRT;    
         
         // Encode texture into PNG
         byte[] bytes = imageOverview.EncodeToPNG();
 
         // save in memory
         string filename = fileName(Convert.ToInt32(imageOverview.width), Convert.ToInt32(imageOverview.height));
         path = Application.persistentDataPath + "/Snapshots/" + filename;       
         System.IO.File.WriteAllBytes(path, bytes);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Prasanna · Dec 04, 2014 at 04:21 AM

May be this will help someone. Here it is, follow this steps;

  1. Go to Edit -> Project setting -> Player -> Other Setting -> Bundle Identifier. In that you can type the name company name and the product name like this "com.your_company_name.your_product_name".

  2. Go to Edit -> Project setting -> Player -> Other Setting -> Write Access -> External(SD Card). That will create a new folder in your android device.

  3. Now the path in your device is Android -> Data -> com.your_company_name.your_product_name -> files.

  4. Create a class for take a screenshot and drag that into your maincamera.

    using UnityEngine; using System.Collections;

    public class Capture : MonoBehaviour { // Store more screenshots... private int Screen_Shot_Count = 0; // Screenshot taking by touch the button. public GUITexture Capture_Model;

      void Update()
         {
             if (Input.touches.Length > 0)
             // Finger hit the button position.
             if(Capture_Model.HitTest (Input.GetTouch(0).position))
             {
                 if (Input.GetTouch(0).phase == TouchPhase.Began)
                 {
                     // Screenshot name.
                     string Screen_Shot_File_Name;
                     do
                     {
                         // Increament the screenshot count.
                         Screen_Shot_Count++;
                         // Save the screenshot name as Screenshot_1.png, Screenshot_2.png, ...
                         Screen_Shot_File_Name = "Screenshot_" + Screen_Shot_Count + ".png";    
                     } 
                     while (System.IO.File.Exists(Screen_Shot_File_Name));
                     // Taking Screenshot.
                     Application.CaptureScreenshot(Screen_Shot_File_Name);
                 }
             }
         }
     }
    
    

This script will help you to take a screenshot by using button.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by OmerEizenberg · May 07, 2015 at 09:42 AM

if you want to save to a android device try this :

public static void SaveTextureToFile( Texture2D texture, string fileName)

         {

                     byte[] bytes=texture.EncodeToPNG();

                     if (Application.platform != RuntimePlatform.Android) 

                          File.WriteAllBytes (fileName + ".png", bytes);

                     else {

                          Debug.Log ("Android save file. path = " + Application.persistentDataPath + "/" + fileName + ".png");

                          File.WriteAllBytes (Application.persistentDataPath + "/" + fileName + ".png", bytes);

                     }

         }

a little present which i have worked on for about a day :P

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • 3
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

60 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need to take (save) a screenshot from a camera other than the main one. How would I do this? 0 Answers

How to make camera position relative to a specific target. 1 Answer

How to take a Screenshot that includes GUI 1 Answer

Take screenshots in game? 1 Answer

Capture a (screen shot) scene shot with alpha 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges