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 JustFamous · Sep 01, 2017 at 08:33 AM · scripting problemfadeinfade out

FadeInOut problem

Hello, currently i have this code for a FadeInOut animation between my levels:

using UnityEngine;

public class CameraFade : MonoBehaviour { // ---------------------------------------- // PUBLIC FIELDS // ----------------------------------------

 // Alpha start value
 public float startAlpha = 1;

 // Texture used for fading
 public Texture2D fadeTexture;

 // Default time a fade takes in seconds
 public float fadeDuration = 2;

 // Depth of the gui element
 public int guiDepth = -1000;

 // Fade into scene at start
 public bool fadeIntoScene = true;

 // ---------------------------------------- 
 //     PRIVATE FIELDS
 // ----------------------------------------

 // Current alpha of the texture
 private float currentAlpha = 1;

 // Current duration of the fade
 private float currentDuration;

 // Direction of the fade
 private int fadeDirection = -1;

 // Fade alpha to
 private float targetAlpha = 0;

 // Alpha difference
 private float alphaDifference = 0;

 // Style for background tiling
 private GUIStyle backgroundStyle = new GUIStyle();
 private Texture2D dummyTex;

 // Color object for alpha setting
 Color alphaColor = new Color();

 // ---------------------------------------- 
 //     FADE METHODS
 // ----------------------------------------

 public void FadeIn(float duration, float to)
 {
     // Set fade duration
     currentDuration = duration;
     // Set target alpha
     targetAlpha = to;
     // Difference
     alphaDifference = Mathf.Clamp01(currentAlpha - targetAlpha);
     // Set direction to Fade in
     fadeDirection = -1;
     //Check to see if currentAlpha is set to 1.  It will need to be 1 to fade properly
     }

 public void FadeIn()
 {
     FadeIn(fadeDuration, 0);
 }

 public void FadeIn(float duration)
 {
     FadeIn(duration, 0);
 }

 public void FadeOut(float duration, float to)
 {
     // Set fade duration
     currentDuration = duration;
     // Set target alpha
     targetAlpha = to;
     // Difference
     alphaDifference = Mathf.Clamp01(targetAlpha - currentAlpha);
     // Set direction to fade out
     fadeDirection = 1;
     //Check to see if currentAlpha is set to 1.  It will need to be 1 to fade properly
     }

 public void FadeOut()
 {
     FadeOut(fadeDuration, 1);
 }

 public void FadeOut(float duration)
 {
     FadeOut(duration, 1);
 }

 // ---------------------------------------- 
 //     STATIC FADING FOR MAIN CAMERA
 // ----------------------------------------

 public static void FadeInMain(float duration, float to)
 {
     GetInstance().FadeIn(duration, to);
 }

 public static void FadeInMain()
 {
     GetInstance().FadeIn();
 }

 public static void FadeInMain(float duration)
 {
     GetInstance().FadeIn(duration);
 }

 public static void FadeOutMain(float duration, float to)
 {
     GetInstance().FadeOut(duration, to);
 }

 public static void FadeOutMain()
 {
     GetInstance().FadeOut();
 }

 public static void FadeOutMain(float duration)
 {
     GetInstance().FadeOut(duration);
 }

 // Get script fom Camera
 public static CameraFade GetInstance()
 {
     // Get Script
     CameraFade fader = (CameraFade)Camera.main.GetComponent("FadeInOut");
     // Check if script exists
     if (fader == null)
     {
         Debug.LogError("No FadeInOut attached to the main camera.");
     }
     return fader;
 }

 // ---------------------------------------- 
 //     SCENE FADEIN
 // ----------------------------------------

 public void Start()
 {
     Debug.Log("Starting FadeInOut");

     dummyTex = new Texture2D(1, 1);
     dummyTex.SetPixel(0, 0, Color.clear);
     backgroundStyle.normal.background = fadeTexture;
     currentAlpha = startAlpha;
     if (fadeIntoScene)
     {
         FadeIn();
     }
 }

 // ---------------------------------------- 
 //     FADING METHOD
 // ----------------------------------------

 public void OnGUI()
 {
     // Fade alpha if active
     if ((fadeDirection == -1 && currentAlpha > targetAlpha) ||
         (fadeDirection == 1 && currentAlpha < targetAlpha))
     {
         // Advance fade by fraction of full fade time
         currentAlpha += (fadeDirection * alphaDifference) * (Time.deltaTime / currentDuration);
         // Clamp to 0-1
         currentAlpha = Mathf.Clamp01(currentAlpha);
     }

     // Draw only if not transculent
     if (currentAlpha > 0)
     {
         // Draw texture at depth
         alphaColor.a = currentAlpha;
         GUI.color = alphaColor;
         GUI.depth = guiDepth;
         GUI.Label(new Rect(-10, -10, Screen.width + 10, Screen.height + 10), dummyTex, backgroundStyle);
     }
 }

} The problem is that when i go back to my main menu, the screen remains black but the music is playing in the background. I assume this is caused by the Alpha but i don't know what to do. I have this script from : http://wiki.unity3d.com/index.php?title=FadeInOut#Introduction_5 The solution is this :
//Check to see if currentAlpha is set to 1. It will need to be 1 to fade properly if (currentAlpha != 1){ currentAlpha = 1;
} But i don't know where to put it to work well, i tried in the Fade in and Fade out but still the same problem. Thanks!

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

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

158 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 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

...Smashing myself endlessly in the face with a brick.. 0 Answers

ScreenFade Help 1 Answer

[SOLVED]Fade in Fade out problem 1 Answer

Problem with fade out cancel before scene change. 0 Answers

What's the easiest way to fade a sprite in? - Looking for equiv of jQuery's fadeIn() 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