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 Quillicit · Jul 04, 2013 at 01:07 PM · guispeedspeed upfading

Keypress speeds up fade?

And another fading problem. I'm starting to find abrupt transitions very attractive. I'm using this code to fade out to a game over screen. The problem is that if the player is still holding down the forward button (or any keyboard button, even those not assigned to any function) when the fade begins, it completes much faster than it should. Any ideas on why?

Here is the active code:

 using UnityEngine;
 using System;
  
 public class CameraFade : MonoBehaviour
 {   
     private static CameraFade mInstance = null;
  
     private static CameraFade instance
     {
         get
         {
             if( mInstance == null )
             {
                 mInstance = GameObject.FindObjectOfType(typeof(CameraFade)) as CameraFade;
  
                 if( mInstance == null )
                 {
                     mInstance = new GameObject("CameraFade").AddComponent<CameraFade>();
                 }
             }
  
             return mInstance;
         }
     }
  
     void Awake()
     {
         if( mInstance == null )
         {
             mInstance = this as CameraFade;
             instance.init();
         }
     }
  
     public GUIStyle m_BackgroundStyle = new GUIStyle();                        // Style for background tiling
     public Texture2D m_FadeTexture;                                            // 1x1 pixel texture used for fading
     public Color m_CurrentScreenOverlayColor = new Color(0,0,0,0);            // default starting color: black and fully transparrent
     public Color m_TargetScreenOverlayColor = new Color(0,0,0,0);            // default target color: black and fully transparrent
     public Color m_DeltaColor = new Color(0,0,0,0);                            // the delta-color is basically the "speed / second" at which the current color should change
     public int m_FadeGUIDepth = -1000;                                        // make sure this texture is drawn on top of everything
  
     public float m_FadeDelay = 0;
     public Action m_OnFadeFinish = null;
  
     // Initialize the texture, background-style and initial color:
     public void init()
     {        
         instance.m_FadeTexture = new Texture2D(1, 1);        
         instance.m_BackgroundStyle.normal.background = instance.m_FadeTexture;
     }
  
     // Draw the texture and perform the fade:
     void OnGUI()
     {   
         // If delay is over...
         if( Time.time > instance.m_FadeDelay )
         {
             // If the current color of the screen is not equal to the desired color: keep fading!
             if (instance.m_CurrentScreenOverlayColor != instance.m_TargetScreenOverlayColor)
             {            
                 // If the difference between the current alpha and the desired alpha is smaller than delta-alpha * deltaTime, then we're pretty much done fading:
                 if (Mathf.Abs(instance.m_CurrentScreenOverlayColor.a - instance.m_TargetScreenOverlayColor.a) < Mathf.Abs(instance.m_DeltaColor.a) * Time.deltaTime)
                 {
                     instance.m_CurrentScreenOverlayColor = instance.m_TargetScreenOverlayColor;
                     SetScreenOverlayColor(instance.m_CurrentScreenOverlayColor);
                     instance.m_DeltaColor = new Color( 0,0,0,0 );
  
                     if( instance.m_OnFadeFinish != null )
                         instance.m_OnFadeFinish();
  
                     Die();
                 }
                 else
                 {
                     // Fade!
                     SetScreenOverlayColor(instance.m_CurrentScreenOverlayColor + instance.m_DeltaColor * Time.deltaTime);
                 }
             }
         }
         // Only draw the texture when the alpha value is greater than 0:
         if (m_CurrentScreenOverlayColor.a > 0)
         {            
             GUI.depth = instance.m_FadeGUIDepth;
             GUI.Label(new Rect(-10, -10, Screen.width + 10, Screen.height + 10), instance.m_FadeTexture, instance.m_BackgroundStyle);
         }
     }
  
  
     /// <summary>
     /// Sets the color of the screen overlay instantly.  Useful to start a fade.
     /// </summary>
     /// <param name='newScreenOverlayColor'>
     /// New screen overlay color.
     /// </param>
     private static void SetScreenOverlayColor(Color newScreenOverlayColor)
     {
         instance.m_CurrentScreenOverlayColor = newScreenOverlayColor;
         instance.m_FadeTexture.SetPixel(0, 0, instance.m_CurrentScreenOverlayColor);
         instance.m_FadeTexture.Apply();
     }
  
      /// <summary>
      /// Starts the fade from color newScreenOverlayColor. If isFadeIn, start fully opaque, else start transparent.
      /// </summary>
      /// <param name='newScreenOverlayColor'>
      /// Target screen overlay Color.
      /// </param>
      /// <param name='fadeDuration'>
      /// Fade duration.
      /// </param>
     public static void StartAlphaFade(Color newScreenOverlayColor, bool isFadeIn, float fadeDuration )
     {
         if (fadeDuration <= 0.0f)        
         {
             SetScreenOverlayColor(newScreenOverlayColor);
         }
         else                    
         {
             if( isFadeIn )
             {
                 instance.m_TargetScreenOverlayColor = new Color( newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0 );
                 SetScreenOverlayColor( newScreenOverlayColor );
             } else {
                 instance.m_TargetScreenOverlayColor = newScreenOverlayColor;
                 SetScreenOverlayColor( new Color( newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0 ) );
             }
  
             instance.m_DeltaColor = (instance.m_TargetScreenOverlayColor - instance.m_CurrentScreenOverlayColor) / fadeDuration;    
         }
     }
 
     void Die()
     {
         mInstance = null;
         Destroy(gameObject);
     }
  
     void OnApplicationQuit()
     {
         mInstance = null;
     }
 }

Comment
Add comment
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

1 Reply

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

Answer by robertbu · Jul 05, 2013 at 06:29 PM

OnGUI() gets called multiple times per frame. Pressing keys, increases the number of events/calls per frame, so you fade faster. To better understand the number of calls/events, add this inside your OnGUI() code:

 Debug.Log(Event.current.type);

To make your code consistent, I'd restrict it to running only when there is a Repaint event:

 if (Event.current.type == EventType.Repaint) {
     // Your OnGUI code goes here  
 }
Comment
Add comment · Show 1 · 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 Quillicit · Jul 08, 2013 at 03:27 AM 0
Share

robertbu, you represent all that is right and good in the world.

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

16 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

Related Questions

Marquee.cs speeds up scrolling with keydown 1 Answer

Changing Speed in Gameplay Affects GUI 2 Answers

I need Help With a Speed up script 1 Answer

Make String Update for Value (GUI) 1 Answer

Fading In and Out the GUI 1 Answer


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