使用淡出效果在 Unity 中加载场景,但它根本不起作用



现在我正在制作c#脚本加载淡出效果后的场景。
第一个场景有一个面板,面板的标签是image。
第二个场景的名字是EndingCredit

但是当我将这个脚本添加到面板(面板在画布下面)并点击播放按钮时,它根本不起作用

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
/*
* script explanation
* shows Earth image and fade out  
* after fade out effect, load EndingCredit Scene 
*/
public class LoadToCredit : MonoBehaviour
{
public Image image;
// Start is called before the first frame update
void Start()
{
//SceneManager.LoadScene("EndingCredit");
FadeEffect();
}
private IEnumerator FadeEffect()
{
float fadeCount = 0; //initial alpha value
while (fadeCount < 1.0f)
{
fadeCount += 0.01f; //lower alpha value 0.01 per 0.01 second 
yield return new WaitForSeconds(0.01f); //per 0.01 second
image.color = new Color(0, 0, 0, fadeCount); //makes image look transparent  
}
//after while loop ends, load EndingCredit scene
SceneManager.LoadScene("EndingCredit");
}
// Update is called once per frame
void Update()
{
//FadeEffect();
//SceneManager.LoadScene("EndingCredit");
}
}

我测试了使用LoadScene()的另一个代码。
这个脚本运行正常。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
/*
* script explanation
* shows Earth image and fade out  
* after fade out effect, load EndingCredit Scene 
*/
public class LoadToCredit : MonoBehaviour
{
public Image image;
// Start is called before the first frame update
void Start()
{

}
// Update is called once per frame
void Update()
{
SceneManager.LoadScene("EndingCredit");
}
}

这里有一些问题。

  1. 如果脚本要修复,我应该如何正确修复它?
  2. 可以在FadeEffect中找到SceneManager.LoadScene("EndingCredit");吗?
  3. 我想要的是在第一个场景的面板淡出后,而不是在2秒后结束信用出现。

感谢您阅读这个问题。

调用FadeEffect作为协程:

void Start() => StartCoroutine(FadeEffect());

最新更新