- Home /
WaitForEndOfFrame not working ?
i just too this script:
using System.IO;
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
void Start()
{
Debug.Log("start");
UploadPNG();
}
IEnumerator UploadPNG()
{
Debug.Log("pre");
yield return new WaitForEndOfFrame();
Debug.Log("post");
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:/localhostcgi-bin/env.cgi?post", form);
yield return w;
if (w.error != null)
print(w.error);
else
print("Finished Uploading Screenshot");
}
}
from http://docs.unity3d.com/Documentation/ScriptReference/WaitForEndOfFrame.html modified it a bit added tracing and it's not working and i don't get it
HELP!
Comment
Best Answer
Answer by frarees · Mar 13, 2014 at 05:15 PM
Coroutines need to be started using StartCoroutine
. So, do:
void Start()
{
Debug.Log("start");
StartCoroutine (UploadPNG());
}
Or even:
IEnumerator Start()
{
Debug.Log("start");
yield return StartCoroutine (UploadPNG());
}
i am looking for WaitForEndOfFrame not every frame coroutine, it needs to be done postrender
@komodor yield doesn't work unless you start the coroutine. Coroutines do not run every frame unless they are specifically told to.
Yes, the reference does seem to be wrong. The correct way is the first snippet in this answer.
I think there has been a copy-paste error in the documentation. In UnityScript, coroutines do not need to be explicitly started.