简体   繁体   English

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

[英]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. 我遵循了Brackeys的教程(您可以在此处观看)中有关如何在场景之间淡入淡出的教程。 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 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 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. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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