LoadSceneAsync方法使用
时间: 2023-09-15 13:20:55 浏览: 145
LoadSceneAsync方法是Unity中用于异步加载场景的方法。它允许在不阻塞主线程的情况下,异步地加载一个场景。
语法:
```
public static AsyncOperation LoadSceneAsync(string sceneName, LoadSceneMode mode = LoadSceneMode.Single);
public static AsyncOperation LoadSceneAsync(int sceneBuildIndex, LoadSceneMode mode = LoadSceneMode.Single);
```
参数:
- sceneName:场景名称。
- sceneBuildIndex:场景构建索引。
- mode:加载场景的模式。
返回值:
- AsyncOperation:异步操作对象,可以用于监视加载进度和处理加载完成事件。
示例:
```csharp
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour
{
public void LoadScene(string sceneName)
{
StartCoroutine(LoadSceneAsync(sceneName));
}
IEnumerator LoadSceneAsync(string sceneName)
{
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneName);
while (!operation.isDone)
{
float progress = Mathf.Clamp01(operation.progress / 0.9f);
Debug.Log("Loading progress: " + (progress * 100f) + "%");
yield return null;
}
Debug.Log("Loading completed!");
}
}
```
上述代码将异步加载场景,并在加载过程中输出加载进度。加载完成后,输出加载完成信息。
阅读全文