Unity中的场景加载

1、同步场景

2、异步加载

public class TestScenesLoad : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(Load());
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    private IEnumerator Load() 
    {
//异步加载,常用模式,asyncOperation.isDone不会调用
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("scenes/SampleScene");

//一般很少使用复场景加载模式,asyncOperation.isDone此时会被调用
// AsyncOperation asyncOperation = //SceneManager.LoadSceneAsync("scenes/SampleScene",LoadSceneMode.Additive);

        asyncOperation.allowSceneActivation = false;
        while (asyncOperation.progress < 0.9f) 
        {
            Debug.Log("当前加载的进度为:"+ asyncOperation.progress);
            yield return null;  //协成中可以等待一帧
        }
        Debug.Log("当前加载的进度为:" + asyncOperation.progress);
        asyncOperation.allowSceneActivation = true;
        yield return null;  //协成中可以等待一帧
        if (asyncOperation.isDone)
        {
            Debug.Log("加载并跳转完毕");
        }
        else 
        {
            Debug.Log("还没有加载并跳转完毕");
        }
    }
}

 3、保留物体到下个场景中

猜你喜欢

转载自blog.csdn.net/baidu_24743861/article/details/132755685