Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Ereptor · Jul 14, 2017 at 03:45 PM · guicoroutinefade

Fade GUI from Coroutine?

Hi Everyone,

I'm having an issue with my Fade In and Fade Out coroutines:

 private IEnumerator DoFadeIn(FadeState e)
     {
         float elapsedTime = 0.0f;
         while (elapsedTime < fadeTime)
         {
             yield return new WaitForEndOfFrame();
             elapsedTime += Time.deltaTime;
             Color newColor = GUI.color;
             newColor.a = 1.0f - Mathf.Clamp01(elapsedTime / fadeTime);
             GUI.color = newColor;
 
             GUI.depth = drawDepth;
 
             GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), fadeTexture);
         }
         FadeManager.OnFadesDone(e);
     }
 
     private IEnumerator DoFadeOut(FadeState e)
     {
         float elapsedTime = 0.0f;
         while (elapsedTime < fadeTime)
         {
             yield return new WaitForEndOfFrame();
             elapsedTime += Time.deltaTime;
             Color newColor = GUI.color;
             newColor.a = Mathf.Clamp01(elapsedTime / fadeTime);
             GUI.color = newColor;
 
             GUI.depth = drawDepth;
 
             GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), fadeTexture);
         }
         FadeManager.OnFadesDone(e);
     }

Before, the issue I was having was that for some reason this script required an empty OnGUI function at the end to function properly. It took a long time to figure out that this was the problem, but after realizing I only needed the empty OnGUI function everything worked fine.

Today I built my project, and some things began to make sense. Nothing was working because the integrated Fades were halting any process with the error "You can only call GUI functions from inside OnGUI". So, what was happening was the empty OnGUI function was sufficient to trick the editor that the GUI functions were happening properly, but this trick won't work for my build.

So, this begs the question... How can I get these coroutines to do what they need to do without calling GUI? Or, alternatively, if I do have to incorporate OnGUI into my coroutines, how would I go about doing it? (Note: These coroutines are triggered by events, and also trigger events when complete.)

Comment
Add comment · Show 1
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 Ereptor · Jul 14, 2017 at 03:58 PM 0
Share

I should also mention that this is an Oculus project, and so I've been very hesitant about using GUI for anything since previous experiences with Oculus lead me to believe that any Overlay GUI elements or functions will be disregarded entirely, and I'm not sure whether the GUI.DrawTexture call would fall into that category. (I think it would.)

So, the ideal solution would be to recreate these coroutines with the $$anonymous$$imal required change to avoid using anything that starts with "GUI".

But what does that leave me with to use as a fade?

1 Reply

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

Answer by Ereptor · Jul 14, 2017 at 06:51 PM

I've decided to simply use an inverted sphere FadeObject to surround the camera. My fade routines now look like this:

 private IEnumerator DoFadeIn(FadeState e)
     {
         float elapsedTime = 0.0f;
         while (elapsedTime < fadeTime)
         {
             yield return new WaitForEndOfFrame();
             elapsedTime += Time.deltaTime;
             FadeMaterial.color = new Color(1.0f, 1.0f, 1.0f, 1.0f - Mathf.Clamp01(elapsedTime / fadeTime));
         }
         FadeObject.SetActive(false);
         FadeManager.OnFadesDone(e);
     }
 
     private IEnumerator DoFadeOut(FadeState e)
     {
         FadeObject.SetActive(true);
         float elapsedTime = 0.0f;
         while (elapsedTime < fadeTime)
         {
             yield return new WaitForEndOfFrame();
             elapsedTime += Time.deltaTime;
             FadeMaterial.color = new Color(1.0f, 1.0f, 1.0f, Mathf.Clamp01(elapsedTime / fadeTime));
         }
         FadeManager.OnFadesDone(e);
     }
Comment
Add comment · 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

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

149 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Coroutine [sometimes] stops working 1 Answer

Transform search function only sporadically works?! 1 Answer

Coroutine not working when another Scene is first opened. 0 Answers

Is there a way to move any UGUI element in editor with a keyboard? 1 Answer

Multiplayer Hiding Layer just for Local Player 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