Learning Async Unity Scene Loading
I've been working on re-learning how to work with scenes in Unity and am planning a larger project around the feature, but wanted to bring all of you on my journey. In this video we'll be learning the basics of how to work with Unity's Asynchronous Scene Loading functions. They're extremely powerful and offer some really cool opportunities for different ways to compose your scenes that we'll be looking into in the future, but for now we'll just be focusing on the basics of how to additively load a scene using synchronous (blocking) and asynchronous (non-blocking) methods. We'll also get a chance to explore Unity Coroutines - another area I am less familiar with.
Unity references Scenes in one of two ways:
* **The Build ID of the Scene**. This can be configured in your projects Build Settings.
* **The Scene Name**. This is either the file name of the scene or the full path of the scene if multiple scenes in your project have the same name.
There are two optional `LoadSceneMode`'s you can provide to Unity when loading a scene that will change how Unity handles the scene you load.
| Option | Result |
| ----------- | ---------- |
| `LoadSceneMode.Single` | All other loaded scenes are unloaded prior and a new scene is loaded. |
| `LoadSceneMode.Additive` | The new scene is loaded alongside the already loaded scenes. |
There are different ways to load scenes asynchronously and track their progress by using an event callback on the `AsyncOperation.completed` event:
```csharp
var sceneStatus = SceneManager.LoadSceneAsync("SceneName", LoadSceneMode.Additive);
sceneStatus.completed += (e) =˃ Debug.Log("Scene Loaded");
```
or by using a Unity Coroutine:
```csharp
public void BeginLoadLevel() {
StartCoroutine(LoadLevelAsync());
}
private IEnumerator LoadLevelAsync()
{
var progress = SceneManager.LoadSceneAsync("SceneName", LoadSceneMode.Additive);
while (!progress.isDone)
{
// Check each frame if the scene has completed.
// For more information about yield in C# see: https://youtu.be/bsZjfuTrPSA
yield return null;
}
// Code after this point is executed after the new scene has loaded
Debug.Log("Scene Loaded");
}
```
You can also load scenes synchronously (your game will freeze until the scene loads) by avoiding the Async versions:
```csharp
SceneManager.LoadScene("SceneName", LoadSceneMode.Additive);
```
***
Unity's documentation on using `LoadSceneAsync` is located here: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html
The `AsyncOperation.completed` event is handled in a unique way (it will always be invoked on the proceeding frame even if the scene is loaded synchronously). You may learn more about the behavior here: https://docs.unity3d.com/ScriptReference/AsyncOperation-completed.html
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u
Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «Learning Async Unity Scene Loading», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.
Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.
Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!
Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.