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 Orion_Reed · Jul 19, 2013 at 01:19 PM · timesmoothfadeoptimisation

FadeIn/Out code optimisation

Hey All, i was wondering if theres anything i could do to optimise this code i wrote for a smooth fade in, not that its behaving slowly, i just want to get the hang of code optimisation, feel free to use it btw, it gives a lovely AAA feeling.

 public var fadeInTexture : Texture2D;
 public var fadeOutTexture : Texture2D;
 public var fadeSpeed = 0.3;
 public var fadeInMultiplier = -5;
 public var fadeOutMultiplier = 1;
 public var reloadWait = 2.0f;
 public var newTimeScale = 0.1;
 public var rampTimeIn = 0.05;
  
 var drawDepth = -1000;
 
 private var alpha = 1.0; 
  
 private var fadeDir = 0;
 
 private var fadingIn = true;
  
 function OnGUI()
 {
  
     alpha += fadeDir * fadeSpeed * Time.deltaTime;    
     alpha = Mathf.Clamp01(alpha);    
  
     GUI.color.a = alpha;
  
     GUI.depth = drawDepth;
     
     if (fadingIn)
     {
     GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeInTexture);
     }
     if (!fadingIn)
     {
     GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
     }
 }
  
 function fadeIn()
 {
     Time.timeScale = 0.1;
     yield WaitForSeconds(0.01);
     fadeDir = -fadeInMultiplier;
     fadingIn = true;    
 }
  
 function fadeOut()
 {
     fadeDir = fadeOutMultiplier;
     fadingIn = false;
     yield WaitForSeconds(reloadWait);
     Application.LoadLevel(Application.loadedLevel);
     
 }
  
 function Start()
 {
     alpha=1;
     Time.timeScale = newTimeScale;
     fadeIn();
 }
 
 
 function FixedUpdate() 
 {
     if (fadingIn) 
         {
             newTimeScale+=rampTimeIn;
             Time.timeScale = newTimeScale;
             Time.fixedDeltaTime = 0.02 * Time.timeScale;
         }
         if (Time.timeScale >=1) 
         {
             Time.timeScale = 1;
             Time.fixedDeltaTime = 0.02 * Time.timeScale;
         }
     }
 
 
 
 function OnTriggerEnter (other: Collider)
 {
     if(other.gameObject.tag != "Player")
     {
         var go:GameObject = other.gameObject;
         Destroy(go);
     }
     else
     {
         fadeOut();
    }
 }
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 Owen-Reynolds · Jul 19, 2013 at 01:54 PM 0
Share

I haven't tested this, but the 2nd piece of optimization advice is not to use OnGUI() (of course, you can't use GUI.DrawTexture, then. Use a GUITexture.)

The first piece of optimization advice is that if it's for a menu, or anything where you aren't pushing the framerate, keep it easy to read and don't optimize.

1 Reply

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

Answer by Eric5h5 · Jul 19, 2013 at 02:26 PM

This is sub-optimal:

 if (fadingIn) ...
 if (!fadingIn) ...

Change it to

 if (fadingIn) ...
 else ...

A far bigger problem is that you always have a texture covering the screen. Even if it's 100% transparent, you're always drawing it, which burns fill rate for no reason (which can be a major issue on some platforms like the iPhone 4 where the GPU is inadequate for the native screen resolution).

Also, ideally you would not have code running at all if it's not necessary. OnGUI runs every frame, so for simple things (like a texture covering the screen), it's better to use a GUITexture object. You can disable it when it's not actually needed, and use a coroutine (like Fade) to only do the fading when necessary, so no code is running when no fading is occurring.

(Not that it has anything to do with performance, but your function names should be uppercase, so they are consistent with the Unity API, which makes code easier to read.)

Comment
Add comment · Show 4 · 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 Orion_Reed · Jul 19, 2013 at 03:10 PM 0
Share

Thank you, could you briefly explain why (!X) is more expensive than Else? feel free to talk techno...

avatar image Eric5h5 · Jul 19, 2013 at 03:28 PM 0
Share

Because you're executing two if statements ins$$anonymous$$d of one.

avatar image BlueRaja_2014 · Jul 19, 2013 at 03:32 PM 0
Share

@Novaous: It's not. Or rather, it is, but the difference is so incredibly, incredibly $$anonymous$$or that, unless you are calling this function literally millions of times a second, it makes absolutely zero difference whatsoever in speed.

The real reason to write it the second way is that it's cleaner code.

avatar image Orion_Reed · Jul 19, 2013 at 03:55 PM 0
Share

Thanks all, I will stop bumping up this topic now

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

18 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

Related Questions

Circular Timer 1 Answer

Quick change over time math 2 Answers

Time Freezer 1 Answer

Change position (hover) over time with easing? 1 Answer

How do I get a value to increment smoothly? 2 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