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 /
avatar image
0
Question by Ereptor · Jun 14, 2017 at 07:12 PM · guifade

My fade coroutine won't work without an empty OnGUI method.

Hi everyone,

I noticed something interesting today. I couldn't figure out why my fade coroutine would sometimes work and sometimes not.

My FadeControl.cs is subscribed to a state change event, and fires off a coroutine when the state changes. Interestingly, the fade is aligned to the center of the screen instead of the corner of the screen if I do not include an empty "void OnGUI(){}" at the end of my cs file.

Subscribe to Event on Enable:

     private void OnEnable()
     {
         StateManager.StateChanged += DoFade;
     }

Execute Coroutine:

     private void DoFade(SceneState e)
     {
         StartCoroutine(FullFade());
     }

Do Fade:

     private IEnumerator FullFade()
     {
         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);
         }
         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);
         }
     }


Again, this does not cover the whole screen unless I include the following at the end of my script. (Completely empty function:)

 private void OnGUI(){}
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:26 PM 0
Share

As it turns out, this is related to a larger problem. This has since been refactored, but the empty OnGUI function is necessary because it tricks the editor into thinking all those GUI calls are actually happening in OnGUI. They are not.

This becomes problematic when making a build (as I just found out) since all those lines that start with "GUI" must be executed in OnGUI in the build. I'm about to post a question related to it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lili-Shi · Jun 14, 2017 at 07:17 PM

Do I see it right, that you try to fade a color? Why don't you use UI.Graphic.CrossFadeColor(Color targetColor, float duration, bool IgnoreTimeScale, bool useAlpha)?

CrossFadeColor Documentation

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

105 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

Related Questions

Fade logo before main menu 2 Answers

Fading an image drawn with GUI.DrawTexture 1 Answer

Disable component via script 1 Answer

How to fade in onGUI on trigger enter/exit? Gui shows on trigger, but no fade. 1 Answer

Fade in from black 4 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