Search Unity

Save Image

Discussion in 'Scripting' started by jerex, Aug 30, 2014.

  1. jerex

    jerex

    Joined:
    Jun 21, 2014
    Posts:
    28
    Hi guys. I want to make the scene save in the gallery of the android phone when the button is click? can you help me doing that? if there is another way please tell me and ill try it. I badly need it now. :(
     
  2. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    http://answers.unity3d.com/questions/204372/saving-screenshots-to-android-gallery.html

    The last answer had some messy code I tryed to clean a bit but perhaps you can use it.
    Code (CSharp):
    1. IEnumerator ScreenshotEncode()
    2.     {
    3.         yield return new WaitForEndOfFrame();
    4.         Texture2D texture1 = new Texture2D (Screen.width, Screen.height, TextureFormat.RGB24, false);
    5.         texture1.ReadPixels (new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    6.         texture1.Apply();
    7.         yield return 0;
    8.  
    9.         byte [] bytes = texture1.EncodeToJPG();
    10.         File.WriteAllBytes( "/mnt/sdcard/DCIM/Images/" + "InsertName" +".jpg",bytes );
    11.     }
     
  3. jerex

    jerex

    Joined:
    Jun 21, 2014
    Posts:
    28
    this is the code that i will try?
     
  4. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    This code take a screenshot into a Texture2d and saves it. If you have a Texture2d already, just use the last 2 lines of code.