Unity3d screenshot function Summary, and save in the specified folder

Source: Internet
Author: User

Fast New Year, has been working overtime to drive the project, no time to write a blog, today to work the last day, on the rest of the Spring Festival, will I strong days in a function to share out, online check a lot, but are in Unity editor so that can, and show out, but, in Android, success, And can show out, but is not saved to the mobile phone album, find a lot of reasons do not know how to go, look up all kinds of information finally solved. I summed up a bit, I used the method, I hope you can use the on.


The first method:

Use the Capturescreenshot method under the application class. But I think it's not easy, not casual. cannot be directed to a camera's screen. for the local screen, it is inconvenient to realize, and the efficiency is low.

   using system.windows.forms;      //saved in the computer C drive, I didn't figure out how to use this method to save my Android phone    if  (input  getmousebuttondown  (0))  {     savefiledialog save = new  savefiledialog ();       save. initialdirectory =  "c:\\";       save. filter =  "Image files (*. jpg;*. bmp;*. PNG) |*. jpg;*. bmp;*. Png| all files  (* *) |*.* ";            dialogresult  result = save. ShowDialog ();           if  (result ==  DialogResult.OK)  {             string  path = save. Filename;      //editorutility.savefilepanel can also be saved, but can only be run under Unity editor.               application.capturescreenshot (Path);            }       }

Android, Application.capturescreenshot (); Save in Shahe, you can take a look at, speak very clearly: http://jingpin.jikexueyuan.com/article/30470.html


The second method: using the new texture2d to intercept the image is divided into the interception part and full screen;

Intercept an area by clicking on the point of the mouse to capture the area to be intercepted; using unityengine;  using system.collections;  using  system.io;  public class shotscreenpicture : monobehaviour  {         Texture2D image;       texture2d cutimage;      www www;       Rect rect;      float time;      vector2  pos1;      Vector2 pos2;         // update is called once per frame      void  update ()       {           //Click the left mouse button to record the first position           if  ( Input.getmousebuttondown (0))           {               pos1 = Input.mousePosition;               time = Time.time;               if  (time > 1f)                {                   debug.log (POS1);               }          }           //left-click Record second position            if  (input.getmousebuttonup (0))           {               pos2 = input.mouseposition;               debug.log (POS2);               startcoroutine (CutImage ());               time = 0;           }      }         void ongui ()       {           //Download Complete           if  (www.isDone )           {               gui. Drawtexture (New rect (0, 0, 600, 904),  image);           }             Gui. Button (New rect (0, 0, 100, 50),  "W"  + Screen.width +  "H"  +  screen.height);            if  (pos1  != null)           {               gui. Button (New rect (0, 50, 150, 50),  pos1. ToString ());          }           if  (pos2 != null)            {              gui. Button (New rect (0, 100, 150, 50),  pos2. ToString ());           }          if  (cutImage != null)            {               gui. Button (New rect (0, 150, 150, 50),  "Image w"  + cutimage.width +   "H"  + cutimage.height);          }             if  (rect != null)            {               gui. Button (New rect (0, 200, 250, 50),  rect. ToString ());           }      }       //      ienumerator cutimage ()        {          //Picture Size            cutimage = new texture2d ((int) (pos2.x -  pos1.x),  (int) (POS1.Y - POS2.Y),  textureformat.rgb24, true);               //coordinates the lower left corner is 0           rect = new rect ((int) pos1.x, screen.height -  (int) ( SCREEN.HEIGHT - POS2.Y),  (int) (pos2.x - pos1.x),  (int) (POS1.Y - POS2.Y ));            yield return new  Waitforendofframe ();           cutimage.readpixels (Rect,  0, 0, true);                    &Nbsp; cutimage.apply ();           yield return  cutImage;          byte[] byt =  Cutimage.encodetopng ();           //Save            //if it is a andriod platform, you can change the Application.streamingassetspath to destination  =  "/sdcard/dcim/camera";         file.writeallbytes ( application.streamingassetspath +  "/cutimage.png",  byt);       }   }
 //Full-screen  //storage path     private string path_save;    // Read Path     private string path_read;    private string  filepath;    private string destination;    void  Start ()     {        filepath =  application.persistentdatapath +  "/test.txt";     }    public  void onclickshot ()     {         Startcoroutine (Gettexture2d ());     }    ienumerator gettexture2d ( )     {        //Hidden ui        .................................        //Operation           Yield return new waitforseconds (0.1f);         Texture2d t = new texture2d (Screen.width, screen.height, textureformat.rgb24,  false);         //display ui         .......................             t. Readpixels (New rect (0, 0, screen.width, screen.height),  0, 0, true);         byte[] bytes = t.encodetopng ();         t.compress (True);         t.apply ();         img.texture = t;         //t is the picture we can upload here to the server         //here is the start save          //wonTake system time         System.DateTime now = new  System.DateTime ();        now = system.datetime.now;         string filename = string. Format ("Image{0}{1}{2}{3}.png",  now. Day, now. Hour, now. Minute, now. Second);         //record each name          streamwriter sw;        fileinfo ft = new  fileinfo (filepath);        la[1].text = filename;         if  (!ft. Exists)         {             sw = ft. CreateText ();        }        else        {             sw = ft. AppendText ();        }         Sw. WriteLine (filename);         sw. Close ();         sw. Dispose ();         //application platform judgment, path selection          if  (application.platform == runtimeplatform.android)          {            string origin  = path_save;            //saved in Android album , if the PC is changed to application .datapath  path              destination =  "/sdcard/dcim/camera";             if  (! Directory.Exists (destination))             {                  Directory.CreateDirectory (destination);             }            destination =  destination +  "/"  + filename;             Path_save = destination;        }         //Save File         file.writeallbytes ( path_save, bytes);     }


The third method: there is a plug-in, the attachment can be downloaded;

Snapshot.captureandsavetoalbum ();

Can be successful, including the selection area and full screen.


"Note" The second and third type of mobile phone albums stored in Android will be changed to cheer the default path when it is released.


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7A/AF/wKiom1awPL-hyCJsAAHRNF_rJ78504.png "title=" 1.png " alt= "Wkiom1awpl-hycjsaahrnf_rj78504.png" style= "float:right;"/>

This article is from the "Unity_3d Technology discussion" blog, please be sure to keep this source http://caoliyong.blog.51cto.com/9944020/1740642

Unity3d function Summary, and save in the specified folder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.