按下按钮时,如何使新场景淡入?

时间:2016-07-09 01:03:53

标签: c# unity3d fade

我已经按照Brackeys的教程(你可以看here)了解如何在场景之间淡入淡出。我尽可能地遵循教程,但是,当我运行我的场景时,场景淡入(这不应该发生),当我按下按钮时,没有任何反应(但场景应该改变)。

我的代码出了什么问题?如何修复它,以便在按下按钮时新场景消失?这是我的代码:

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);

}

}

1 个答案:

答案 0 :(得分:1)

您可以尝试在场景顶部放置一个面板。 然后,使用动画组件创建不透明度降低的新动画。实际上,您可以使按钮调用此动画。在动画结束时,您可以添加一个事件来调用将破坏面板的函数。 希望它有所帮助。

相关问题