Search Unity

Automatic Screenshot Taker for Publishing on Stores

Discussion in 'Scripting' started by KeiseEntertainment, Jul 3, 2016.

  1. KeiseEntertainment

    KeiseEntertainment

    Joined:
    May 5, 2015
    Posts:
    23
    Hi guys,

    So, what i'm trying to do is creating something similar to Multi Screenshots Suite, meaning that i want only to click a button and have a perfect screenshot of my game in as many resolution i want. The objective doing that is to automatically get prints for Apple, Google and Windows Phone stores, wasting less time (a lot less) doing that manually.

    I already did the script below, but the UI, Camera and etc, do not follow the changes that my screenshot is set. How can i manage to do that?

    Thanks for the help. =]

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ScreenshotTaker : MonoBehaviour
    5. {
    6.     public Screenshots[] screenshots;
    7.  
    8.     private bool takeHiResShot = false;
    9.  
    10.     public static string ScreenShotName(string name, int width, int height)
    11.     {
    12.         string path = string.Format("{0}/Screenshots/{1}",
    13.                              Application.dataPath, name);
    14.  
    15.         if (!System.IO.Directory.Exists(path))
    16.             System.IO.Directory.CreateDirectory(path);
    17.  
    18.         return string.Format("{0}/Screenshots/{1}/screen_{2}x{3}_{4}.png",
    19.                              Application.dataPath,
    20.                              name,
    21.                              width, height,
    22.                              System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
    23.     }
    24.  
    25.     public void TakeHiResShot()
    26.     {
    27.         takeHiResShot = true;
    28.     }
    29.  
    30.     void LateUpdate()
    31.     {
    32.         takeHiResShot |= Input.GetKeyDown("k");
    33.         if (takeHiResShot)
    34.         {
    35.             for (int i = 0, length = screenshots.Length; i < length; i++)
    36.             {
    37.                 RenderTexture rt = new RenderTexture((int)screenshots[i].resolution.x, (int)screenshots[i].resolution.y, 24);
    38.                 GetComponent<Camera>().targetTexture = rt;
    39.                 Texture2D screenShot = new Texture2D((int)screenshots[i].resolution.x, (int)screenshots[i].resolution.y, TextureFormat.RGB24, false);
    40.                 GetComponent<Camera>().Render();
    41.                 RenderTexture.active = rt;
    42.                 screenShot.ReadPixels(new Rect(0, 0, (int)screenshots[i].resolution.x, (int)screenshots[i].resolution.y), 0, 0);
    43.                 GetComponent<Camera>().targetTexture = null;
    44.                 RenderTexture.active = null; // JC: added to avoid errors
    45.                 Destroy(rt);
    46.                 byte[] bytes = screenShot.EncodeToPNG();
    47.  
    48.                 string filename = ScreenShotName(screenshots[i].name, (int)screenshots[i].resolution.x, (int)screenshots[i].resolution.y);
    49.                 System.IO.File.WriteAllBytes(filename, bytes);
    50.                 Debug.Log(string.Format("Took screenshot to: {0}", filename));
    51.             }
    52.          
    53.             takeHiResShot = false;
    54.         }
    55.     }
    56.  
    57.     [System.Serializable]
    58.     public class Screenshots
    59.     {
    60.         public string name;
    61.         public Vector2 resolution;
    62.     }
    63. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    What do you mean by not following your changes?

    I know when implementing my own screenshot system, I had to use a coroutine with a yield return new waitForEndOfFrame before the screenshot code could take place.
     
    Last edited: Jul 3, 2016
  3. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    I believe you have to apply the pixels after reading them. Try this?

    Code (CSharp):
    1. screenShot.ReadPixels(new Rect(0, 0, (int)screenshots[i].resolution.x, (int)screenshots[i].resolution.y), 0, 0);
    2. screenShot.Apply();
     
  4. KeiseEntertainment

    KeiseEntertainment

    Joined:
    May 5, 2015
    Posts:
    23
    What i wanna do is something like the asset i said (Multi Screenshots Suite)...

    I want get all resolutions from iPhones (for example) with just a press of a button...

    Here is my script editor configuration:

     
  5. KeiseEntertainment

    KeiseEntertainment

    Joined:
    May 5, 2015
    Posts:
    23
    Here is the asset video of what i need (jump to 35s):