场景加载的方法
在进行场景的跳转脚本中一定要引用UnityEngine.SceneManagement
创见场景的索引
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class NO20_sceneManager : MonoBehaviour
{
AsyncOperation ao;
// Start is called before the first frame update
void Start()
{
//SceneManager.LoadScene(1);
//SceneManager.LoadScene("SceneExample");
//异步加载
//SceneManager.LoadSceneAsync();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine("LoadNextAsyncScene");
}
if (Input.anyKeyDown && ao.progress >= 0.9f)
{
ao.allowSceneActivation = true;
}
}
//运用协程使游戏场景的加载过程可控
IEnumerator LoadNextAsyncScene()
{
ao = SceneManager.LoadSceneAsync(1);
ao.allowSceneActivation = false;
while (ao.progress < 0.9f)
{
//当前加载进度小于90%
yield return null;
}
print("按下任意键继续");
}
}
注:在使用异步加载时,当前场景还是可以运行的