Предположим, у нас есть такой код:
Код: Выделить всё
private async UniTask LoadGameAsync()
{
//this should be the right way to await the scene loading
await LoadSceneAsync("Level_01");
Debug.Log("Level 01 finished loading.");
//does this work?
//I'm uncertain if this is correct because I await a non async method. But then again it returns a task ... No compiler error.
await LoadScene("Level_02");
Debug.Log("Level 02 finished loading.");
//why all this?
//because I want to load multiple scenes simultaneously and only when all are finished loading, hide a loading screen.
//Does this work the way I intend to or are there pitfalls to avoid? If so, what's the correct way of implementing something like this?
UniTask level03Task = LoadScene("Level_03");
UniTask level03UITask = LoadScene("Level_03_UI");
await UniTask.WhenAll(level03Task, level03UITask);
Debug.Log("Level 03 and its UI finished loading, hide loading screen now.");
}
private async UniTask LoadSceneAsync(string sceneName)
{
await SceneManager.LoadSceneAsync(sceneName).ToUniTask();
}
private UniTask LoadScene(string sceneName)
{
return SceneManager.LoadSceneAsync(sceneName).ToUniTask();
}
Если это не работает должным образом, каков был бы правильный способ реализации? что я себе представляю?
Обратите внимание, что UniTask по сравнению с Task — это структура, а не класс. Не уверен, что это повлияет на создание задач для дальнейшего использования.
Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/785 ... ask-return
Мобильная версия