Search Unity

Here is a script to take the same screenshot at multiple resolutions in editor

Discussion in 'Scripting' started by Mello42, Aug 21, 2014.

  1. Mello42

    Mello42

    Joined:
    May 30, 2010
    Posts:
    34
    Dear All,

    I wrote this little JS script to make automated splash screens from the game running for my iOS game.

    As you know, Unity does not allow resizing the game window from code while in editor.

    To do this, I used some RenderTexture to Texture2d conversion tricks.

    I am a terrible programmer so there might be something wring with it but here it is. Note: it can be easily modified to take multiple screenshots of the same frame in various resolutions and aspect ratios.

    Code (javascript):
    1.  
    2. private var names = ["Default", "Default@2x", "Default-568h@2x", "Default-Landscape", "Default-Landscape@2x", "Default-Portrait", "Default-Portrait@2x"];
    3. private var resolutions : Vector2[] = [Vector2(320,480), Vector2(640,960), Vector2(640,1136),    Vector2(1024,768), Vector2(2048,1536), Vector2(768,1024), Vector2(1536,2048)];
    4.  
    5. var hide : GameObject[];
    6. var mainCamera : Camera;
    7. private var rTexture : RenderTexture;
    8.  
    9.  
    10. function Start () {
    11.     if(Application.isEditor){
    12.         for (var obj : GameObject in hide){
    13.             obj.SetActive(false);
    14.         }
    15.         CreateSplash();
    16.     }
    17. }
    18.  
    19. function CreateSplash(){
    20.     var originalTimeScale = Time.timeScale;
    21.     Time.timeScale = 0.0;      
    22.     var path = System.IO.Directory.GetCurrentDirectory()+ "/Assets/SplashImages";
    23.     System.IO.Directory.CreateDirectory(path);  
    24.     yield;
    25.     for(var i = 0; i < names.Length; i++){
    26.         rTexture = new RenderTexture(resolutions[i].x, resolutions[i].y, 24 );
    27.         mainCamera.targetTexture = rTexture;
    28.         mainCamera.aspect = resolutions[i].x/resolutions[i].y;
    29.         yield;
    30.         RenderTexture.active = rTexture;
    31.         var tex = new Texture2D(resolutions[i].x, resolutions[i].y);
    32.         tex.ReadPixels(new Rect(0, 0, rTexture.width, rTexture.height), 0, 0);
    33.         tex.Apply();
    34.         var bytes = tex.EncodeToPNG();
    35.         var fullpath = String.Format("{0}/"+names[i]+".png", path);
    36.         System.IO.File.WriteAllBytes(fullpath, bytes);
    37.         yield;
    38.     }
    39.     Time.timeScale = originalTimeScale;  
    40.     mainCamera.ResetAspect();
    41.     mainCamera.targetTexture = null;
    42.  
    43. }
    44.