- Home /
Generator function visibility?
I've been using a jpeg encoder to take a screenshot of my game. The function which encodes the screenshot, aptly named EncodeScreenshot(filepath: String) is a generator function that is it contains a yield statement. The same javascript contains another function, getFilepath() which returns the filepath where the screenshot is stored.
From my gui script (written in C sharp), I can call getFilepath, no problem. But when I try to call EncodeScreenshot(filepath), it doesn't appear to call the function. I have Debug.Log statements inside the function, and before and after the function call. The before and after statements print, but not the one inside the function.
On the other hand, if I write a test OnGui into the Screenshot script, the function saves the screenshot to the correct location.
What is happening? Why does one function call work and not the other? My wild guess is that it has something to do with the way generator functions work.
GUI Code abridged
void OnGui(){
if(submenuVisible = 1){
getScreenshot();
}
}
void getScreenshot(){
string filename = this.GetComponent<Screenshot>().ScreenShotName();
string message = "Your screenshot will be saved to: " + filename;
this.GetComponent<Screenshot>().ScreenshotEncode(filename);
GUI.BeginGroup(centerRect);
GUI.Box(new Rect(0, 0, centerRect.width, centerRect.height), message);
if(GUI.Button(new Rect((int)(centerRect.width/2 - 50), centerRect.height - 40, 100, 30), "OK")){
Debug.Log("Button true");
this.GetComponent<Screenshot>().ScreenshotEncode(filename);
Debug.Log("Picture stored?");
submenuVisible = -1;
}
GUI.EndGroup();
}
Screenshot Code in full
import System.IO;
private var filename : String;
/**
* Test key
*/
function OnGUI()
{
if(GUI.Button(new Rect(50, 50, 100, 100), "click")){
filename = ScreenShotName();
ScreenshotEncode(filename);
}
}
function Start(){
filename = "";
}
/**
* Take the screen buffer and spit out a JPG
*/
function ScreenshotEncode(filenameParameter: String)
{
Debug.Log("Start Encode");
filename = filenameParameter;
// wait for graphics to render
yield WaitForEndOfFrame();
// create a texture to pass to encoding
var texture:Texture2D = new Texture2D (Screen.width, Screen.height, TextureFormat.RGB24, false);
// put buffer into texture
texture.ReadPixels(Rect(0.0, 0.0, Screen.width, Screen.height), 0.0, 0.0);
texture.Apply();
// split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
yield;
// create our encoder for this texture
var encoder:JPGEncoder = new JPGEncoder(texture, 75.0);
// encoder is threaded; wait for it to finish
while(!encoder.isDone) {
yield;
}
// save our test image (could also upload to WWW)
File.WriteAllBytes(filename, encoder.GetBytes());
Debug.Log(String.Format("Took screenshot to: {0}", filename));
//return filename;
}
function ScreenShotName() {
return (Application.persistentDataPath + "/screen_" +
System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") +".jpg");
}
That line:
if(submenuVisible = 1){
getScreenshot;
}
is that exactly how it is in your program? If so, I don't think it does what you expect. You are trying to call it as a member, not as a function. You should use
getScreenshot();
ins$$anonymous$$d. Of course, if that was just pseudocode and not actually how it goes in your script, disregard this comment.
It wasn't supposed to be pseudocode, but I was a bit sloppy with copy and paste. Will edit.
The GUI code is not all the code I'm using, but if you think the problem might be hiding in another part of the code, I can post it complete.
Answer by syclamoth · Jan 23, 2012 at 03:23 PM
In your getScreenshot C# method, you need to wrap
this.GetComponent<Screenshot>().ScreenshotEncode(filename);
in a StartCoroutine method.
Otherwise, it won't execute properly, because it doesn't know how to handle the yield statements correctly. So-
StartCoroutine(this.GetComponent<Screenshot>().ScreenshotEncode(filename));
Should fix at least some of the problems.
The confusion arises because in JS you don't need to explicity state that your function is a Coroutine- all that stuff gets magically inserted at compile time.
Yikes, I just looked at the coroutine page on wikipedia. Is there a reference that might better explain what coroutines are? Your solution solves the problem, but now I'm curious.
Your answer
Follow this Question
Related Questions
How to add a jpg as a background on a GUI skin 1 Answer
Is there a FileChooser available for GUI? 1 Answer
Amnesia/Skyrim Style Notes 0 Answers
User choose audio from HardDisk/Cd 2 Answers
Rotating a Rect by the GUI.matrix? 0 Answers