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 Jaks_ · Nov 18, 2016 at 11:31 PM · fading

fade in scene when level is loaded, fade out when button pressed

hi all! i'm new. i want to fade in the scene when the level loads in and fade out when the player presses the space bar to get to the next level and do the same thing in the next level. i have no idea how to do this, looking up tutorials but didn't get it to work. Thanks

 void Update()
 {
     if (Application.loadedLevel == 2)//if loaded level is 1
     {
         {
         if (score >= 0)//if score is 0
             DesTime -= Time.deltaTime;//level description kill time countdown
         {
             if (DesTime <= 0)
                 Destroy(LevelDes);

                 if (Input.GetButtonDown("Restart"))
                     Application.LoadLevel(Application.loadedLevel);
             }
     }
         if (score >= 8)//if score is 8
         {
             if (Input.GetButtonDown("Jump"))   
                 Application.LoadLevel("Level2");//load level 2
         }
     }
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 TBruce · Nov 19, 2016 at 01:38 AM 0
Share

Try this out.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using UnityEngine.Scene$$anonymous$$anagement;
 
 public class ScreenFader : $$anonymous$$onoBehaviour
 {
     public Image FadeImg;
     public float fadeSpeed = 1.5f;
 
     void Awake()
     {
         if (FadeImg != null)
         {
             StartCoroutine(DoFade(true, fadeSpeed, -1));
         }
     }
 
     IEnumerator DoFade(bool fadeIn, float fadeTime, int sceneIndex)
     {
         bool done = false;
         float time = 0;
         Color startColor = FadeImg.color;
         startColor.a = (!fadeIn) ? 0 : 1;
         Color endColor = new Color(FadeImg.color.r, FadeImg.color.g, FadeImg.color.b, fadeIn ? 0 : 1);
         FadeImg.enabled = true;
         while (!done)
         {
             time += Time.deltaTime;
             float lerpValue = time / fadeTime;
             FadeImg.color = Color.Lerp (startColor, endColor, lerpValue);
             yield return null;
 
             if (fadeIn)
                 done = FadeImg.color.a <= 0.0f;
             else    
                 done = FadeImg.color.a >= 1.0f;
 
         }
         yield return new WaitForSeconds(1);
         if (sceneIndex >= 0)
         {
             Scene$$anonymous$$anager.LoadScene(sceneIndex);
             yield return new WaitForSeconds(0.5f);
         }
         FadeImg.enabled = false;
     }
 
     public void LoadScene(int SceneNumber)
     {
         StartCoroutine(DoFade(false, fadeSpeed, SceneNumber));
     }
 
     public void RestartScene()
     {
         LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex);
     }
 
     public void LoadNextScene()
     {
         if (Scene$$anonymous$$anager.GetActiveScene().buildIndex < (Scene$$anonymous$$anager.sceneCountInBuildSettings - 1))
             LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1);
         else
             RestartScene();
     }
 
     public void LoadPreviousScene()
     {
         if (Scene$$anonymous$$anager.GetActiveScene().buildIndex > 0)
             LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex - 1);
         else
             RestartScene();
     }
 }

The public functions for loading scenes are

 LoadScene(int SceneNumber)
 RestartScene()
 LoadNextScene()
 LoadPreviousScene()

0 Replies

· Add your reply
  • Sort: 

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

77 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

Related Questions

how to fade away scene for few seconds 2 Answers

How do I slowly fade text? 0 Answers

GUYS! Need to make fade in when level ending 0 Answers

Fade child object with a coroutine 0 Answers

Why Scene does not open after scene fading? C# 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