[英]How can I make a new scene fade in when a button is pressed?
我遵循了Brackeys的教程(您可以在此處觀看)中有關如何在場景之間淡入淡出的教程。 我盡力按照本教程進行操作,但是,當我運行場景時,場景會淡入(這不應該發生),而當我按下按鈕時,什么也不會發生(但場景應該會發生變化)。
我的代碼有什么問題? 如何解決此問題,以便在按下按鈕時淡入一個新場景? 這是我的代碼:
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);
}
}
您可以嘗試將面板放置在場景頂部。 然后,使用動畫器組件創建一個新動畫,其中不透明度會降低。 實際上,您可以使按鈕調用此動畫。 在動畫的最后,您可以添加事件以調用將破壞面板的函數。 希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.