public TextMeshProUGUI loadText;
public Button quitButton;
private string targetSceneName;
public Image image;//黑幕图片
void Start()
{
GameObject.DontDestroyOnLoad(this.gameObject);//Canvas不能有父物体
quitButton.onClick.AddListener(() => LoadButton("StartScene"));
}
private void LoadButton(string sceneName)
{
targetSceneName = sceneName;
StartCoroutine(LoadSceneMode());
}
IEnumerator LoadSceneMode()
{
image.DOFade(1, 1f).SetEase(Ease.InOutQuad);//变黑
loadText.gameObject.SetActive(true);
yield return null;
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(targetSceneName );
asyncOperation.allowSceneActivation=false;
while (!asyncOperation.isDone)
{
loadText.text = "等待加载:" + (asyncOperation.progress * 100).ToString("F1") + "%";
if (asyncOperation.progress>=0.9f)
{
loadText.text = "按下任意键继续";
if (Input.anyKey)
{
asyncOperation.allowSceneActivation = true;
image.DOFade(0, 1f).SetEase(Ease.InOutQuad);//变亮
loadText.text = "";
}
}
yield return null;
}
}