Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cmauceri · Jan 23, 2012 at 02:26 PM · guifile-iogeneratorjpg

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");
 }

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Jan 23, 2012 at 03:01 PM 0
Share

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.

avatar image cmauceri · Jan 23, 2012 at 03:14 PM 0
Share

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.

avatar image syclamoth · Jan 23, 2012 at 03:22 PM 0
Share

Waait, is ScreenshotEncode meant to be a coroutine?

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cmauceri · Jan 23, 2012 at 03:39 PM 0
Share

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.

avatar image cmauceri · Jan 23, 2012 at 03:40 PM 1
Share

This is what comes from borrowing code! :P

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges