简体   繁体   中英

How can I make a new scene fade in when a button is pressed?

I have followed a tutorial from Brackeys (you can watch it here ) on how to fade between scenes. I followed the tutorial the best I could, but, when I run my scene, the scene fades in (which is not supposed to happen) and when I press the button, nothing happens (but the scene is supposed to change).

What is wrong with my code? How do I fix it so a new scene fades in when the button is pressed? Here is my code:

changeScene.cs

using UnityEngine;
using System.Collections;

public class changeScene : MonoBehaviour {

public IEnumerator changeToGameScene () {

    float fadeTime =     GameObject.Find("managerObject").GetComponent<fadeScript>().BeginFade(1);
    yield return new WaitForSeconds(fadeTime);
    Application.LoadLevel("gameScene");

}

}

fadeScript.cs

using UnityEngine;
using System.Collections;

public class fadeScript : MonoBehaviour {


// All Variables
public Texture2D fadeOutTexture;
public float fadeSpeed = 0.8f;

private int drawDepth = -1000;
private float alpha = 1.0f;
private int fadeDirection = -1;

void OnGUI () {

    alpha += fadeDirection * fadeSpeed * Time.deltaTime;
    alpha = Mathf.Clamp01(alpha);

    GUI.color = new Color (GUI.color.r, GUI.color.g, GUI.color.b, alpha);
    GUI.depth = drawDepth;
    GUI.DrawTexture ( new Rect (0, 0, Screen.width, Screen.height),     fadeOutTexture );
}

public float BeginFade (int direction) {

    fadeDirection = direction;
    return (fadeSpeed);

}

void OnLevelWasLoaded () {

    BeginFade (-1);

}

}

You could try placing a panel on top of the scene. Then, using an animator component, create a new animation where the opacity decreases. In fact, you can make the button call this animation. At the end of the animation you could add an event to call a function that will destroy the panel. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM