Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help please (ReadPixels bug?)

Discussion in 'Scripting' started by jarden, Mar 30, 2014.

  1. jarden

    jarden

    Joined:
    Mar 29, 2011
    Posts:
    74
    Hi, can anyone say why my code doesn't work? It freezes the editor every time. Like it's trying to make to many screen-shots, or is it trying to name every pixel the way i set it up?

    Code (csharp):
    1.     void Update () {
    2.         timeE += Time.deltaTime;
    3.         if (Input.GetKeyDown("space")) {
    4.             //duration/frameRate will give increments for this sprite sheet animation
    5.             //instead of relying on how fast my computer can take screenshots
    6.             timeE = (duration/frameRate)+0.005f;
    7.             //(frameRate * duration) will give me the number of screenshots to take
    8.             //bool1 tells me if my coroutine is running
    9.             while (count1 < (frameRate * duration)+3) {
    10.                 if (bool1 = false  timeE >(duration/frameRate)*count1) {
    11.                     StartCoroutine("Example2");
    12.                 }
    13.             }
    14.         }
    15.     }
    Code (csharp):
    1.     IEnumerator Example2() {
    2.         bool1 = true;
    3.         Texture2D sshot = new Texture2D(width, height, TextureFormat.ARGB32, true);
    4.         yield return new WaitForEndOfFrame();
    5.         sshot.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    6.         yield return 0;
    7.         sshot.Apply();
    8.         byte[] pngShot = sshot.EncodeToPNG();
    9.         Destroy(sshot);
    10.         File.WriteAllBytes(Application.dataPath + "/AAASCap/" + name1 + count1.ToString() + ".png", pngShot);
    11.         bool1 = false;
    12.         count1++;
    13.     }
     
    Last edited: Apr 5, 2014
  2. jarden

    jarden

    Joined:
    Mar 29, 2011
    Posts:
    74
    Anyone?
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,999
    Does it write any files, or gets stuck before it?

    Or does it work if you remove the while loop, take just 1 image when press key?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
  5. jarden

    jarden

    Joined:
    Mar 29, 2011
    Posts:
    74
    It doesn't write any files and Application.CaptureScreenshot can't be set to a size that i know of (i don't want to crop 60 pics manually for a sprite animation).

    Without the if statement in the while loop is crashes and make no files.

    Without the while loop it doesn't crash, but it still doesn't write a file.

    I can get it to write files without the if or while conditions.
     
    Last edited: Mar 31, 2014
  6. jarden

    jarden

    Joined:
    Mar 29, 2011
    Posts:
    74
    Still no answer here. Should I bug report it?