- Home /
Fadeout Issue
I am almost done with my scenefader. The fadein works properly, but the fadeout lags terribly. What could be casuing this? I have a video showing the problem and the animator in action - https://youtu.be/q9_cDFnENi4.
Change Scene Manager Code:
using UnityEngine;
using System.Collections;
using UnityEngine .SceneManagement ;
using UnityEngine .UI;
public class ChangeSceneManager : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
//Button Functions
public void MainMenu(){
SceneFader.instance.FadeIn("Title Menu");
}
public void HowToPlay(){
SceneFader.instance.FadeIn ("How To Play Scene");
}
public void Play(){
SceneFader.instance.FadeIn ("Gameplay");
}
}
Scene Fader Code:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class SceneFader : MonoBehaviour {
public static SceneFader instance;
[SerializeField]
private GameObject fadeCanvas;
[SerializeField]
private Animator fadeAnim;
void Awake(){
MakeASingleInstance ();
}
void MakeASingleInstance(){
if (instance != null) {
Destroy (gameObject);
} else {
instance = this;
DontDestroyOnLoad (gameObject);
}
}
IEnumerator FadeInAnimate (string levelName){
fadeCanvas.SetActive (true);
fadeAnim.Play ("FadeIn");
yield return StartCoroutine(MyCoroutine.WaitForRealSecond(0.5f));
SceneManager.LoadScene (levelName);
FadeOut ();
}
IEnumerator FadeOutAnimate(){
fadeAnim.Play ("FadeOut");
yield return new WaitForSeconds (1f);
fadeCanvas.SetActive (false);
}
public void FadeIn(string levelName){
StartCoroutine (FadeInAnimate(levelName));
}
public void FadeOut(){
StartCoroutine (FadeOutAnimate());
}
}
My Coroutine Code:
using UnityEngine;
using System.Collections;
public static class MyCoroutine {
public static IEnumerator WaitForRealSecond(float time){
float start = Time.realtimeSinceStartup;
while (Time.realtimeSinceStartup < (start + time)) {
yield return null;
}
}
}
Did you try calling your "WaitForRealSeconds" code in the fadeout as well?
Right now I see you're calling it for the fade in but calling WaitForSeconds in fade out. Also, WaitForRealSeconds seems to be fading faster (since you're calling the code via 0.5f) while WaitForSeconds will fade slower (since you're telling it to run every 1 second).
Hope this helps solve the issue.
Thanks for the input, but this unfortunately did not work.
I assume you are animating the canvas group's alpha value. Ins$$anonymous$$d of animating it, you can just use a tweener(ex: Lean Tween ) and tween the alpha in certain time.
Would I need to make an entirely different code? I tried messing around with the crossfade alpha option in a seperate code, but didn't get it to work. Would it be possible to implement your solution into my current code?
Assu$$anonymous$$g you use "LeanTween" you can do this on your canvas group's (cg) alpha. Just one line of code.
LeanTween.value(gameObject, (val)=> { cg.alpha = val; }, cg.alpha, 0, 1);
Above you are tweening the value from current cg.alpha to 0 in time of 1 second and for every value update you are assigning the alpha value (which you can see in the update delegate)
I now have leantween and updated my code with your suggestions, but have not been able to get it to work. I also attached some pictures of my inspector and hierarchy.
Here's my updated code:
SceneFader:
using UnityEngine;
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
public class SceneFader1 : $$anonymous$$onoBehaviour {
public static SceneFader1 instance;
[SerializeField]
private CanvasGroup cg;
void Awake(){
Time.timeScale = 1f;
$$anonymous$$akeASingleInstance ();
}
void $$anonymous$$akeASingleInstance(){
if (instance != null) {
Destroy (gameObject);
} else {
instance = this;
DontDestroyOnLoad (gameObject);
}
}
public void FadeIn(string levelName){
LeanTween.value(gameObject, (val)=> { cg.alpha = val; }, cg.alpha, 1, 0);
FadeOut ();
}
public void FadeOut(){
LeanTween.value(gameObject, (val)=> { cg.alpha = val; }, cg.alpha, 0, 1);
}
}
Scene$$anonymous$$anager:
using System.Collections;
using UnityEngine .Scene$$anonymous$$anagement ;
using UnityEngine .UI;
public class ChangeScene$$anonymous$$anager : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void $$anonymous$$ain$$anonymous$$enu(){
SceneFader1.instance.FadeIn("Title $$anonymous$$enu");
Scene$$anonymous$$anager.LoadScene ("Title $$anonymous$$enu");
StartCoroutine (Wait ());
SceneFader1.instance.FadeOut();
//Scene$$anonymous$$anager.LoadScene ("Title $$anonymous$$enu");
//ScreenFader.instance.LoadLevel("Title $$anonymous$$enu");
//SceneFader.instance.FadeIn("Title $$anonymous$$enu");
//StartCoroutine(ChangeLevel("Title $$anonymous$$enu"));
}
public void HowToPlay(){
SceneFader1.instance.FadeIn("How To Play Scene");
Scene$$anonymous$$anager.LoadScene ("How To Play Scene");
StartCoroutine (Wait ());
SceneFader1.instance.FadeOut();
//SceneFader.instance.FadeIn ("How To Play Scene");
//Scene$$anonymous$$anager.LoadScene ("How To Play Scene");
//ScreenFader.instance.LoadLevel("How To Play Scene");
//StartCoroutine(ChangeLevel("How To Play Scene"));
}
public void Play(){
SceneFader1.instance.FadeIn("Gameplay");
Scene$$anonymous$$anager.LoadScene ("Gameplay");
StartCoroutine (Wait ());
SceneFader1.instance.FadeOut();
//SceneFader.instance.FadeIn ("Gameplay");
//ScreenFader.instance.LoadLevel("Gameplay");
//StartCoroutine(ChangeLevel("Gameplay"));
}
IEnumerator Wait (){
yield return new WaitForSeconds (1f);
}
}
[1]: /storage/temp/73697-screen-shot-2016-07-08-at-82059-pm.png
Hey you are calling fadeOut in fade in method. This will cause both to happen together?
Also last parameter of the "LeanTween.value" is time.. u have made it 0 in fade in.
What exactly is happening? Try calling fade in separately and fade out separately Also you can chain methods to the tweener
Ex: If you want to do something after fade in is complete you can do this: LeanTween.value(gameObject, (val)=> { cg.alpha = val; }, cg.alpha, 1, 1).SetOnComplete(yourfunction/delegate);