Legacy Documentation: Version 4.6.2
Language: English
  • C#
  • JS
  • Boo

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

WaitForEndOfFrame

Namespace: UnityEngine

/

Inherits from: YieldInstruction

Description

Waits until the end of the frame after all cameras and GUI is rendered, just before displaying the frame on screen.

You can use it to read the display into a texture, encode it as an image file (see Texture2D.ReadPixels and Texture2D.EncodeToPNG) and send it somewhere.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    IEnumerator Example() {
        yield return new WaitForEndOfFrame();
    }
}

Another example:

using System.IO;
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Start() {
        UploadPNG();
    }
    IEnumerator UploadPNG() {
        yield return new WaitForEndOfFrame();
        int width = Screen.width;
        int height = Screen.height;
        Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
        tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        tex.Apply();
        byte[] bytes = tex.EncodeToPNG();
        Destroy(tex);
        WWWForm form = new WWWForm();
        form.AddField("frameCount", Time.frameCount.ToString());
        form.AddBinaryData("fileUpload", bytes);
        WWW w = new WWW("http://localhost/cgi-bin/env.cgi?post", form);
        yield return w;
        if (w.error != null)
            print(w.error);
        else
            print("Finished Uploading Screenshot");
    }
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    private Material mat;
    IEnumerator Start() {
        while (true) {
            yield return new WaitForEndOfFrame();
            if (!mat)
                mat = new Material("Shader \"Hidden/Alpha\" {" + "SubShader {" + "    Pass {" + "        ZTest Always Cull Off ZWrite Off" + "        Blend DstAlpha Zero" + "        Color (1,1,1,1)" + "    }" + "}" + "}");
            
            GL.PushMatrix();
            GL.LoadOrtho();
            int i = 0;
            while (i < mat.passCount) {
                mat.SetPass(i);
                GL.Begin(GL.QUADS);
                GL.Vertex3(0, 0, 0.1F);
                GL.Vertex3(1, 0, 0.1F);
                GL.Vertex3(1, 1, 0.1F);
                GL.Vertex3(0, 1, 0.1F);
                GL.End();
                ++i;
            }
            GL.PopMatrix();
        }
    }
}

Inherited members