如何使用UniTask来加载场景,请举两个例子
时间: 2023-03-27 17:00:24 浏览: 153
可以使用UniTask的SceneLoader来加载场景,以下是两个例子:
1. 使用UniTask的SceneLoader加载场景:
```csharp
using Cysharp.Threading.Tasks;
using UnityEngine.SceneManagement;
async void LoadSceneAsync(string sceneName)
{
await SceneLoader.LoadSceneAsync(sceneName, LoadSceneMode.Single);
}
```
2. 使用UniTask的SceneLoader加载场景并传递参数:
```csharp
using Cysharp.Threading.Tasks;
using UnityEngine.SceneManagement;
async void LoadSceneAsync(string sceneName, object parameter)
{
var scene = await SceneLoader.LoadSceneAsync(sceneName, LoadSceneMode.Single);
var rootObjects = scene.GetRootGameObjects();
foreach (var rootObject in rootObjects)
{
var component = rootObject.GetComponent<ISceneParameterReceiver>();
if (component != null)
{
component.ReceiveParameter(parameter);
}
}
}
```
以上是两个使用UniTask的SceneLoader加载场景的例子。
阅读全文