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
2
Question by smirlianos · Nov 01, 2012 at 03:54 PM · cameraeditorscreeneffect

How to "fade out" a scene

How I can make a scene go to another one, but by making the screen darker and darker... I want it to be smooth

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

6 Replies

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

Answer by Griffo · Nov 01, 2012 at 03:59 PM

Drop a black texture in the inspector and place script on the camera.

 #pragma strict
 
 // FadeInOut
 
 var fadeTexture : Texture2D;
 var fadeSpeed = 0.2;
 var drawDepth = -1000;
 
 private var alpha = 1.0; 
 private var fadeDir = -1;
 
 function OnGUI(){
     
     alpha += fadeDir * fadeSpeed * Time.deltaTime;  
     alpha = Mathf.Clamp01(alpha);   
     
     GUI.color.a = alpha;
     
     GUI.depth = drawDepth;
     
     GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
 }
Comment
Add comment · Show 6 · 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 smirlianos · Nov 01, 2012 at 04:36 PM 0
Share

thanks, it worked!

avatar image fafase · Nov 01, 2012 at 04:51 PM 2
Share

The idea is definitely there. $$anonymous$$y concern is about OnGUI(). This function is called once or more each frame. Here you are calling it every frame for nothing. You might use a custom function with a black GUITexture that is called on Start and when leaving only.

avatar image Griffo · Nov 01, 2012 at 05:07 PM 0
Share

What about something like ..

 private var fade : boolean;
 
 function OnGUI(){
   if (fade){
     // GUI code goes here
   }
 }
avatar image fafase · Nov 01, 2012 at 05:22 PM 0
Share

Well, what about:

 void Start(){
    FadeIn();
 }
 void Update(){
     if(levelDone){
        //all kind of stuff
        FadeOut(); 
        Application.LoadLevel(next);
     }
 }

Calling a function means a jump from the program to a special location in memory where the function is stored. It also involves some data movements such as storage of the program counter, of the this pointer onto the stack and any other parameters. OnGui is called at least once per frame so this would happen at least once per frame. A boolean check is about a couple of cycles. Nonetheless, your way works perfectly as well. I just mentioned there are also other ways.

avatar image Griffo · Nov 01, 2012 at 05:31 PM 0
Share

$$anonymous$$y way was the only way that came to $$anonymous$$d and I really appreciate your input, now I've read your comments I agree it's a waste of time to call it if not needed, optimisation .. every little helps I suppose.

Show more comments
avatar image
6

Answer by Griffo · Nov 01, 2012 at 04:17 PM

That script will fade in .. to fade out use this one

 #pragma strict
 
 // FadeInOut
 
 var fadeTexture : Texture2D;
 var fadeSpeed = 0.2;
 var drawDepth = -1000;
 
 private var alpha = 0.0; 
 private var fadeDir = -1;
 
 function OnGUI(){
 
     alpha -= fadeDir * fadeSpeed * Time.deltaTime;  
     alpha = Mathf.Clamp01(alpha);   
 
     GUI.color.a = alpha;
 
     GUI.depth = drawDepth;
 
     GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
 }
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 Ian094 · Dec 14, 2013 at 08:38 PM 0
Share

Wow, thanks so much for this. It's by far the best Fade In/Out script! All the others I've found are way too complex! Thanks again.

avatar image Pedro11 · Mar 22, 2014 at 02:10 PM 0
Share

Hy $$anonymous$$r Griffo,this work perfectly in javascript but i tried several times to change this code to C# and doesnt work ,you know why ? Thanks.

avatar image whiteant54 · Mar 18, 2015 at 03:43 AM 0
Share

@Griffo, how can I make the fade speed longer? I tried putting it to very high numbers but it seams to be that only 0.9 is the highest. Is that true?

avatar image Griffo · Mar 18, 2015 at 06:14 AM 0
Share

You could just change line 14 to

 alpha += fadeDir * fadeSpeed * (Time.deltaTime*0.2);

And change the last value to what you want.

avatar image
4

Answer by shohan4556 · Oct 03, 2016 at 05:59 AM

I would like to post here the updated script that will work in unity-5.x

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class FadeScreen : MonoBehaviour {
 
     public Texture2D fadeTexture;
 
     [Range(0.1f,1f)]
     public float fadespeed;
     public int drawDepth = -1000;
 
     private float alpha = 1f;
     private float fadeDir = -1f;
 
     // Use this for initialization
     void Start () {
     
     }
     
 
     void OnGUI() {
 
         alpha += fadeDir * fadespeed * Time.deltaTime;
         alpha = Mathf.Clamp01(alpha);
 
         Color newColor = GUI.color; 
         newColor.a = alpha;
 
         GUI.color = newColor;
 
         GUI.depth = drawDepth;
 
         GUI.DrawTexture( new Rect(0,0, Screen.width, Screen.height), fadeTexture);
 
     }
 
   
 
 }
 

 
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
avatar image
1

Answer by n_soufiani · May 26, 2014 at 12:40 AM

I managed to convert it into c# so if anyone is interested, here it is:

public bool fade; public Texture2D fadeTexture; public float fadeSpeed = 0.2f; public int drawDepth = -1000; private float alpha = 1.0f; private int fadeDir = -1; if (fade) { alpha += fadeDir * fadeSpeed * Time.deltaTime; alpha = Mathf.Clamp01 (alpha); // GUI.color = new Color() { a = 0.5f }; GUI.color = new Color() {a=0.8f}; GUI.depth = drawDepth; GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), fadeTexture); }

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 VirusPunk · Dec 06, 2015 at 05:01 AM 1
Share

That doesn't really work, just stays stuck on black but it doesn't actually fade. Try this ins$$anonymous$$d for C#...

         if (isFadingOut)
         {
             alpha -= fadeDir * fadeSpeed * Time.deltaTime;
             alpha = $$anonymous$$athf.Clamp01(alpha);
 
             Color thisAlpha = GUI.color;
             thisAlpha.a = alpha;
             GUI.color = thisAlpha;
 
             GUI.depth = drawDepth;
 
             GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), fadeImage);
         }
avatar image
0

Answer by BDX777 · May 26, 2014 at 12:56 AM

You could try lerping the alpha of a blank black GUITexture from invisible to visible, but I'm not sure how you would go about doing that.

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
  • 1
  • 2
  • ›

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

20 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

Related Questions

Why doesn't my players camera work and why does it spaz out randomly 1 Answer

Picture-in-Picture Effect 1 Answer

How do I take a high quality screen shot from my scene view camera? 5 Answers

How to render all opaque meshes with the same effect? 1 Answer

Why does Viking Village Water move with camera in Editor? 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