Unity как загрузить сцену
Перейти к содержимому

Unity как загрузить сцену

SceneManager.LoadScene

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Declaration

Declaration

Parameters

sceneName Name or path of the Scene to load.
sceneBuildIndex Index of the Scene in the Build Settings to load.
mode Allows you to specify whether or not to load the Scene additively. See LoadSceneMode for more information about the options.

Description

Loads the Scene by its name or index in Build Settings.

Note: In most cases, to avoid pauses or performance hiccups while loading, you should use the asynchronous version of this command which is: LoadSceneAsync.

When using SceneManager.LoadScene, the scene loads in the next frame, that is it does not load immediately. This semi-asynchronous behavior can cause frame stuttering and can be confusing because load does not complete immediately.

Because loading is set to complete in the next rendered frame, calling SceneManager.LoadScene forces all previous AsyncOperations to complete, even if AsyncOperation.allowSceneActivation is set to false. To avoid this, use LoadSceneAsync instead.

The given sceneName can either be the Scene name only, without the .unity extension, or the path as shown in the BuildSettings window still without the .unity extension. If only the Scene name is given this will load the first Scene in the list that matches. If you have multiple Scenes with the same name but different paths, you should use the full path.

Note that sceneName is case insensitive, except when you load the Scene from an AssetBundle.

For opening Scenes in the Editor see EditorSceneManager.OpenScene. SceneA can additively load SceneB multiple times. The regular name is used for each loaded scene. If SceneA loads SceneB ten times each SceneB will have the same name. Finding a particular added scene is not possible.

If a single mode scene is loaded, Unity calls Resources.UnloadUnusedAssets automatically.

The following two script examples show how LoadScene can load Scenes from Build Settings. LoadSceneA uses the name of the Scene to load. LoadSceneB uses the number of the Scene to load. The scripts work together.

How to load a scene in Unity

Loading in Unity

Loading a new Scene in Unity can be very straightforward, requiring only a single line of code.

Despite being simple to do, the best technique for loading new Levels will vary depending on how you want to do it.

For example, you may want to display a loading screen, a progress bar, or show tips or other information while the player waits for a Level to load.

Likewise, how you structure your game may affect how you load and move between Scenes.

If your game is small, you might only need a few different Scenes.

However, if you’re making a larger game, you might need to split your game, and even each level, into many different Scenes.

There is no one size fits all, but understanding the different options that are available to you, and seeing examples of how to actually use them, can make it much easier to pick the right method of moving between Scenes for your game.

Which is exactly what you’ll learn in this article.

What you’ll find on this page:

First, let’s start with the basic method of loading a new Scene in Unity

How to load a new Scene in Unity

To load any Scene from a script in Unity, first, you’ll need to make sure that the Scene you want to load is included in the build.

Otherwise, if the Scene hasn’t been added to the Build Settings, or the Asset Bundle hasn’t been loaded, none of this will work and the Scene won’t load.

To check, select File > Build Settings and, if you can’t see the Scene listed, simply drag it to the list of Scenes from the Project View.

Unity Build Settings Window

Remember to add the Scene you want to load to the build, otherwise, none of this will work.

Next, you’ll need to add the Scene Management namespace to any script that you want to load Scenes from.

Simply add “using UnityEngine.SceneManagement;” with the other using directives.

Like this:

This will allow you to use functions from the SceneManager class.

Finally, to load a new Scene, call the Load Scene function, passing in the name of the Scene you’d like to load.

Like this:

Alternatively, you can use the Scene’s index instead.

Like this:

If you don’t know the index of the Scene you want to load, you can look it up by opening the Build Settings. You can also reorder Scenes to change their index number.

Loading by a Scene’s index can be especially useful if you want to load Scenes in order; For example, if you want the player to proceed to the next Scene in the list at the end of each level.

If it suits the structure of your game, loading the next Scene instead of a specific Scene can be easier to manage, as you won’t need to explicitly name the Scene to be loaded.

So how do you load the next Scene on the list?

How to load the next Scene in Unity

To load Scenes in order, i.e. load whatever Scene is next on the list after the current Scene, all you need to do is get the current Scene index and add 1.

Here’s what it looks like in scripting:

All this does is get the index of the active Scene and increment it by one, which is ideal for advancing to the next Scene at the end of every level.

Keep in mind, however, that this only really works when your game structure allows for it (i.e. players are meant to experience every Level in order) and only when each Scene is in the correct order in the build list.

Because of this, you’ll need to be careful to avoid accidentally reordering Scenes in the Build Settings.

This can also be a benefit.

While you will need to be careful to keep the Scenes in the order you want them, it also means you can move Scenes to a different position very easily.

When using this method, changing the order of Scenes in your game is as easy as reordering the build list.

Load Scene vs Load Scene Async

There are two different methods available for Loading a scene.

Load Scene and Load Scene Async.

So what’s the difference?

Load Scene, as used in the earlier example, loads the Scene directly with loading taking place during the next frame:

While the Scene loads, the game will freeze.

Music and audio will continue to play but the game will be unresponsive until the load has finished.

Depending on the amount of time that’s required to load the next Scene, this pause can give the impression that the game has crashed, unless you hide the pause with a Loading Screen (more on that later).

However, it’s also possible to load the next Scene in the background, asynchronously, while still allowing the game to run, using Load Scene Async.

Load Scene Async works in a similar way to Load Scene, except the loading takes place as a background operation and is spread over multiple frames:

There are a few reasons that you might want to do this.

For example, you might want to keep the current Scene running until the new Scene is ready.

Or, if your loading screen includes animated elements, such as tooltips or a loading icon, these can only work when loading the Scene in the background.

Also, as an Asynchronous Operation, you have the option to check when the loading has finished using AsyncOperation.isDone. You can even get the current progress of the load, which is useful when making a loading progress bar.

How to get the Load Scene Async progress

To access the progress of the load, cache a reference to the Asynchronous Operation when you call it.

Like this:

You can then access the operation’s properties.

For example the progress:

Which returns the progress of the load as a float value between 0 and 1.

Or to check if loading is finished.

Like this:

These additional properties are only available when using Load Scene Async.

Why does Unity still freeze and stutter when using Load Scene Async?

Load Scene Async, while described as a background operation, is still a heavy task to perform.

You may find that it will still cause the game to stutter or even freeze while loading takes place.

If this is happening in your project, you might not actually have an issue.

Loading a new Scene in the editor will bring most Scenes, even very basic ones, to a complete halt.

However, if you build the project first you’ll find that the loading process, and speed, is significantly better than when previewing it in the editor.

So, if you’re having issues with how smoothly your Scenes load, and you haven’t tried building your project yet, try running a finished build first.

This will help you to get an idea of what the loading process is actually like in your game.

How to make a Loading Screen in Unity

Regardless of which loading method you use, it’s often a good idea to use a Loading Screen to disguise the loading process.

And while you may prefer to avoid Loading Screens altogether, every game needs to load something at some point and using one can make the experience of transitioning between menus and levels much smoother.

So what should you add to your Loading Screen?

In its most basic form, you’ll need something to cover the screen with that can also be used as a background for a loading message or other information.

For example, a UI image, set to black and stretched to fill the screen, works well for this:

Unity Loading Screen UI Image

Add a UI Image to act as a background for your Loading Screen.

Better yet, why not add a custom image that sets up the Level that’s being loaded.

Here I’ve used an in-game image of the Sun Temple asset that I’ve been using to test with, with a faded area at the bottom to leave some space for messages.

Custom Loading Screen Image

Using a custom background image can help set the scene for the Level that’s being loaded.

Speaking of messages, there should be something to indicate that loading is taking place.

The point of using a Loading Screen at all is to communicate that the game has not, in fact, crashed. So it’s a good idea to include some sort of indicator that there is something going on behind the scenes.

This could be a simple “Loading” message or some kind of animated icon.

I added a Text object and a rotating Image to make this loading message.

While an animated icon tells you that the game is loading, a progress bar or a percentage display is a great way to show how much of the Level is loaded.

So how do you do that?

How to make a loading Progress Bar in Unity

Unity Loading Bar Example

Here’s how to make a loading Progress Bar in Unity.

1. Set up a Slider to act as a Progress Bar:
  • Create a UI Slider

  • Remove the handle

  • Set the left and right values in the Rect transform to zero on both the Fill Area object and the Fill object
2. Load the Scene Asynchronously
  • When loading the Scene use Load Scene Async, otherwise this won’t work
  • Cache a reference to the Asynchronous Operation when you call it, like this:
3. Update the Slider during the load
  • Get a reference to the Slider (remember to add using UnityEngine.UI; to the script)
  • Get the loading progress, which is a float value between 0 and 1 (using AsyncOperation.progress)
  • Using Mathf.Clamp01, convert the progress to a value between 0 and 0.9, like this:

Why do you need to convert the progress to value between 0 and 0.9f?

This is because the loading operation’s progress is only measured up to 0.9, with the last 0.1 being used for activation (which won’t be visible on the Slider).

Doing it this way means the bar will fill up completely.

For a really good explanation on how this works, try Brackeys’ video (Youtube).

Keeping the final value between 0 and 1 makes it a little easier to also convert the loading progress to a percentage, which is useful if you’d like to display a percentage value as well.

Display the load progress as a percentage

To display the load progress as a percentage, just like before, when creating the Progress Bar, take the Async Operation Progress value and convert the value to a float between 0 and 0.9:

Then, to display it as a percentage, simply round the value, multiplied by 100, to the nearest integer.

Like this:

How to fade the Loading Screen

Fading in the Loading Screen is a great way to smooth out the transitions between screens.

But, while single UI elements can be faded by setting the Alpha of their Canvas Renderer, chances are your Loading Screen is made up of multiple elements.

Luckily, there’s a simple solution.

A Canvas Group component makes it easy to apply settings to a number of UI elements all at once.

Canvas Group Component Unity

Control multiple UI elements with a Canvas Group

For example, to create a Loading Screen you might use multiple UI objects, such as a background image, some text and a slider.

Using a Canvas Group makes it easy to apply the same settings to all of those objects, so long as they’re Child objects of the Parent Loading Screen.

To fade the screen, Lerp the Canvas Group Alpha value between 0 and 1 in a Coroutine.

Like this:

When fading during loading, just as with other animation, you’ll need to use Load Scene Async.

That is, if you want loading to start at the same time as the fade.

However, if you’re using the basic Load Scene method, and you still want to fade the screen, you can.

Just make sure to perform the fade before starting the load.

Make it interesting

Once you know how to build the different elements, it becomes easier to create a Loading Screen that’s unique to your game.

And, personally, I think that it’s worth taking extra time and effort to create a Loading Screen that is engaging, nice to look at or that smoothes out the transitions between menus and levels in your game.

Because loading is waiting and waiting, even in small bursts, is rarely fun.

And, while it’s inevitable, in many cases at least, to have to wait for some part of your game to load, that doesn’t mean it can’t be a pleasant experience, or informative or, especially in tricky games, a chance to familiarise the player with some of the controls or concepts of the game.

So why not make the most of it.

Adding the Loading Screen to your game (3 examples)

Once you’ve designed a Loading Screen, it’s time to implement it.

And while there’s no one way to do this, there are a few options that may be more or less suitable, depending on the how you organise your game’s structure.

Below are three different options for adding the Loading Screen to your Unity project.

Example 1: The Game Object method (using a Loading Screen Prefab)

This is, potentially, the simplest of the three methods in that it requires very little set up.

You simply create your Loading Screen as you want it, store it as a Prefab, and then add to any Scene that requires a Loading Screen.

Like this:

Using a Loading Screen Prefab makes it easier to manage the object from one place.

The benefit of this method is that it’s very simple and easy to manage, and any changes to the Loading Screen can be made to the Prefab object.

However, you’ll still need to add the Loading Screen object, and get a reference to it, in every Scene that uses it, which could become difficult to manage.

And while this isn’t necessarily an exclusive problem (after all there will be other objects that are common to every Scene too, such as the player) you may find it easier to manage only a single Loading Scene object in one location.

Such as in the Persistent Method.

Example 2: The Persistent Method (using Don’t Destroy on Load)

This method works in almost the same way as the Game Object method, with one difference.

Instead of using multiple Loading Screen objects (one for each Scene) that all derive from a Prefab, this method uses a single object that is reused for every Scene.

This is made possible by using Don’t Destroy on Load, a function in Unity that prevents an object from being Destroyed when a new Scene is loaded, as is the default.

To use it, call the Don’t Destroy on Load function, passing in the Game Object you want to carry over to the next Scene.

In this case, pass in the Loading Screen Object

Like this:

This is useful for creating more complex Loading Scene transitions that continue until after a Scene is loaded.

For example, this jigsaw loading transition from Banjo Kazooie: Nuts and Bolts overlaps both Scenes, starting before and ending after the switch. In Unity, using Don’t Destroy on Load allows you to create similar animations across two Scenes with just one Loading Screen object.

This loading sequence from Banjo Kazooie: Nuts and Bolts overlaps two Scenes. (Source)

In the example below I use the Persistent method to fade in a Loading Screen, before fading the same Loading Screen out again once the new Scene is loaded.

Create a separate Canvas just for your Loading Screen

When using the Persistent method, you may wish to create your Loading Screen on its own Canvas.

The Canvas object stores and displays UI elements for your game. It’s automatically created when you add a UI element and is the parent object for everything that you add to your menu, hud and, in this case, Loading Screen.

However, it’s also possible to create Canvasses manually, that are separate from others in the Scene.

And while creating your Loading Screen on its own Canvas isn’t required for this method, it can make managing it easier.

This is because Don’t Destroy on Load only works at the root level of an object, so it makes sense to separate the Loading Screen Canvas from other UI elements that you don’t want to take through to other Scenes.

Separating the Loading Screen Canvas in this way means you may end up using multiple Canvas objects in your Scene.

To make sure that your Loading Screen Canvas renders above other UI elements, set its Sort Order higher than other Canvas objects.

Managing Multiple Canvases in Unity

When using multiple canvases in Unity, the Canvas sort order decides which will be displayed on top.

Example 3: The Loading Scene method (using static data)

The Loading Scene Method involves using a dedicated Scene to display the Loading Screen instead of an in-game object.

This method is great for building really complex Loading Screens, with game tips, rotating 3D models, animated visuals etc.

And while you can do all of that with an in-game object, it’s much neater and easier to manage in a separate Scene.

So how does it work?

  1. Create a Scene, called “Loading” or something similar, and build it just like you would using any other method.
  2. Then, when you want to load a new Scene, instead of directly Loading the target Scene, transition to the dedicated Loading Scene.
  3. Inside the Loading Scene, add a script to load the next level of the game as soon as the Loading Scene starts.

Using a Loading Scene in Unity

Creating one special Loading Scene can be an easy way to manage Loading Screen objects in Unity.

Except, there’s a problem.

Assuming that your game is made up of many different Levels, how will the Loading Scene script know which Scene to load next?

Luckily there’s a simple method for passing data to a new Scene when you load it.

How to pass data and variables between Scenes in Unity

There are several options for saving data that can be reused at a later time. For example, Player Prefs or by saving information to a file.

However, for the purpose of transferring data from one active Scene to the next, a Static Class, that’s created solely for passing data when loading, is one of the more appropriate options available.

This involves creating a public static class with public static variables that can be used to store the data that you want to transfer between Scenes.

In this case a static string variable can be used to store the name of the Scene that needs to be loaded next.

Then, when the loading script in the Loading Scene launches, it can read the same value and load the required Scene.

This is helpful because the loading trigger doesn’t need to get a reference to a Loading Screen object or the Loading Data class.

Because it’s a publicly accessible static class, all you need to do is type the class name. Any object can set the value directly for the Loading Screen script to read.

Here’s how to do it:

  1. Create a new C Sharp script inside your Project View (remember there will not be an instance of this script in any Scene). Call it LoadingData or something similar
  2. Remove the Start and Update functions, as you won’t need them
  3. Remove “: Monobehaviour” as static classes can’t derive from Monobehaviour
  4. You can also remove the “using” directives from the top of the script, as you won’t need them
  5. Add the “static” keyword between public and class
  6. Finally, add a public static string variable called sceneToLoad or similar

Your script should now look something like this:

In the Loading Screen Scene, create a script on a Game Object to read the Loading Data and load the target Scene.

Like this:

Finally, when you want to trigger a Scene load from the game or from a menu, add the following script, setting the string value to the name of the new Scene you want to load.

Like this:

This sets the scene name in the static Loading Data class and then immediately loads the Loading Scene which, in turn, reads the scene name and loads the final target Scene.

Like this:

How to pass data between Scenes Unity

Using a Static Class makes it easy to pass data from one Scene to another, such as the name of the next Scene.

What’s more, this method can be used to display additional information about the loading Scene.

For example, you could configure the Loading Screen to display a custom image or message based on which Scene was being loaded.

Now it’s your turn

How are you loading Scenes in your game?

Do you have many Scenes to deal with, or only a few? And how are you managing them?

Or maybe you’ve got a great tip for loading Scenes in Unity that other people would love to know.

Whatever it is, let me know by leaving a comment below.

by John Leonard French

Game audio professional and a keen amateur developer.

Get Game Development Tips, Straight to Your inbox

Get helpful tips & tricks and master game development basics the easy way, with deep-dive tutorials and guides.

My favourite time-saving Unity assets

Rewired (the best input management system)

Rewired is an input management asset that extends Unity’s default input system, the Input Manager, adding much needed improvements and support for modern devices. Put simply, it’s much more advanced than the default Input Manager and more reliable than Unity’s new Input System. When I tested both systems, I found Rewired to be surprisingly easy to use and fully featured, so I can understand why everyone loves it.

DOTween Pro (should be built into Unity)

An asset so useful, it should already be built into Unity. Except it’s not. DOTween Pro is an animation and timing tool that allows you to animate anything in Unity. You can move, fade, scale, rotate without writing Coroutines or Lerp functions.

Easy Save (there’s no reason not to use it)

Easy Save makes managing game saves and file serialization extremely easy in Unity. So much so that, for the time it would take to build a save system, vs the cost of buying Easy Save, I don’t recommend making your own save system since Easy Save already exists.

Comments

Simple, good.
Did you ever manage to get LoadAsync smooth?
These fancy loading screens are inspiring but back in Unity 4.7 stutter fest, how is it nowadays in 2017LTS?

I used Unity 2018 to test with and, when building the project first, the async loading was pretty smooth. I imagine 2017 LTS would be similar. However, what’s being loaded, CPU and drive speed will probably affect loading performance massively so your mileage, and everyone who plays the game, may vary massively regardless.

So I loaded scenes async but my application still freezes on the built version, I read somewhere my awake method might be the problem, is there a way I can run the awake method smoothly without having the entire program freeze. I had this issue with my vr project and also my AR mobile project

Are you doing anything in awake that could be stalling the main thread? As you say, it may not be related to scene loading at all.

Thanks a lot for these amazing examples. I was looking for a way to pass parameters from menu screen to loading screen.

got Fanned(if that’s a word) by a single post, been making games for over 2 years no but there were some things here which were very useful to me, this is the first time I’ve seen a blog/tutorial site where I’ve subscribed (and I’ve seen a lot).

Thanks so much! Glad it helped.

I loved your website tutorial, I am a new game developer in this field, Thanks.

Please whatever you know about unity put it on your website.

Your article is so much clean and easy to follow it

I absolutely love these articles. Easy to follow, extremely important/useful information, and very well put-together. Are you going to continue to write these types of articles on this site? I definitely hope so!

hey John, your articles are exceptional! Thank you so much for providing this amazing free resource, your writing has transformed my understanding of designing for and developing with Unity 3D. You’ve made it so much easier to create my first ever Unity VR game this year. Seriously appreciate it!

Thanks so much! I’m really glad that it’s helped you. Best of luck with your game!

I prefer to do it with the “base” scene that contains all the common elements throughout the game (settings, loading screen, etc.). All other scenes are loading asynchronously in additive mode, and the base scene is responsible for loading screen.

Great tip! Thanks.

Oh that’s actually an awesome idea!
do you control where to exactly spawn the next scene?

“Load Scene Async, while described as a background operation, is still a heavy task to perform.

You may find that it will still cause the game to stutter or even freeze while loading takes place.”

Actually, it’s because Unity’s implementation is horrible and quite amateur in its implementation. It’s barely Asynchronous at all.
Synchronous holds the thread up until it’s completely loaded, Async breaks that down into a 5 or a dozen chunks where it frees up the thread just a few time. This is of no use where anything smooth is needed during loading.

AAA titles have far heavier loading and can manage silky smooth animations during loading of assets.

Thanks, this was a really well written, informative piece for both novices and advanced users. You saved me a lot of time!

Thank you! Glad it helped.

From Unity’s documentation:

public static SceneManagement.Scene LoadScene(string sceneName, SceneManagement.LoadSceneParameters parameters);

Please explain “parameters”. Can we use them to pass information to the scene that is being loaded?

Your article nicely explains the use of a static variable, but it would be so much cleaner if we could just pass information through the LoadScene statement itself.

I believe that the parameter that’s referring to is the Load Scene mode. Whether to load it additively or not (i.e. combine multiple scenes). https://docs.unity3d.com/ScriptReference/SceneManagement.LoadSceneParameters-ctor.html

Great article – full of useful tips and instructions. Good job!

Hi. Just wanted to say that I’m a total beginner and figuring out how to link scenes has been killing me for days. This blog post was so informative and demystified scripting for me a lot. I still don’t know anything, but I’m a lot less intimidated than I was. Do you have a “virtual tip jar” of some kind?

Really glad to hear it helped you!

Great article & site. Very helpful to have the material in a written format. I would purchase if there was an option to. Thanks

Leave a Comment Cancel reply

Welcome to my blog

John Leonard French - Photo

I’m John, a professional game composer and audio designer. I’m also a keen amateur developer and love learning how to make games. More about me

Latest Posts
  • Godot vs Unity (for making your first game)
  • How to capture the screen in Unity (3 methods)
  • How to write a game design document (with examples)
  • How to start making a game (a guide to planning your first project)
  • How to use script composition in Unity
Thanks for Your Support

Some of my posts include affiliate links, meaning I may earn a commission on purchases you make, at no cost to you, which supports my blog.

Loading Scenes In Unity

Even though loading a different Scene in Unity only requires a single line of code, depending on varying situations there’s quite some to say about how we can utilise Scene Management functionality.

Depending on the size or structure of your game you might want to split the game into different Scenes, each representing a level or a part of a level on its own.

There might be situations where we need to access a specific Scene, like the previous or next Scene. Or maybe we just want to load the Scene with a particular ‘SceneName’.

We may want to display additional loading information on screen like a loading progress bar or some game tips, during the ‘loading time’.

We have quite a few methods provided by UnityEngine.SceneManagement allowing us to manage scene loading behaviour, in this article you’ll be introduced to a few important concepts. In addition we’ll start implementing scene management behaviour to our own space shooter project.

Preface

Amongst a few other classes provides by UnityEngine.SceneManagement there are two classes that I swiftly want to highlight; the Scene and SceneManager Class. They help us derive information from our Game Scenes and how we can efficiently manage storing or switching between them.

Scene Class

Run-time data structure for *.unity file.

A few important properties:

  • name : Returns the name of the Scene that is currently active.
  • path : Returns the relative path of the Scene. Like: “Assets/MyScenes/MyScene.unity”.
  • buildIndex : Returns the index of the Scene in the Build Settings.

For more information about the Scene Class, click here.

SceneManager Class

Scene management at run-time.

This Class has two Static Properties:

  • sceneCount : The total number of currently loaded Scenes.
  • sceneCountInBuildSettings : Number of Scenes in Build Settings.

The SceneManager provides us with a range of Static Methods allowing us to handle Scene Management. A few examples:

  • GetActiveScene : Gets the currently active Scene.
  • GetSceneByBuildIndex : Get a Scene struct from a build index.
  • GetSceneByName : Searches through the Scenes loaded for a Scene with the given name.
  • GetSceneByPath : Searches all Scenes loaded for a Scene that has the given asset path.
  • LoadScene : Loads the Scene by its name or index in Build Settings.
  • LoadSceneAsync : Loads the Scene asynchronously in the background.
  • SetActiveScene : Set the Scene to be active.

For more information about the SceneManager Class, click here.

Loading Scenes

First of all we need to make sure that the Scene we want to load is included in the ‘Scenes In Build’ List, otherwise it won’t load.

Select File > Build Settings or Ctrl + Shift + B, and drag in the Scene in to the Build Settings List. Alternatively you can add all active scenes to the Build Settings by clicking Add Open Scenes.

Then we need to make a new Script where we want to load our Scenes from. You can call this as you like but I think it’s handy to keep logic that handles scene management separated from other types of behaviour.

In this Script we need to add the UnityEngine.SceneManagement; namespace giving us access to the Scene and SceneManager Class related functions.

To Load a Scene we can call the Static Method Load Scene() and either pass in a Scene Name (string), a Scene Index (int) or a Scene Path (string).

A Scene can be loaded in Single Mode, which unloads the previous Scene immediately upon loading the next Scene. Or, it can be loaded in Additive Mode, keeping both Scenes active at the same time in the Hierarchy while overlapping each other in the Game View. Just declaring a single parameter will execute loading a Scene in Single mode.

When to use which parameter

This depends on the structure of your game and what kind of functionality is needed. A few examples might demonstrate this;

  • If the Scene Index number is not the last one, add one to the index and load that scene.

  • If the Scene Index number is not the first one, remove one from the index and load that scene.

Just keep in mind that if you load a Scene by its Scene Index, the index number must match the respective Scene in the Builds Settings. However, you can easily rearrange them.

Load Scene vs Load Scene Async

Important to know is that there are two methods available for Loading a scene: Load Scene and Load Scene Async.

Load Scene

Load Scene loads the Scene directly with loading taking place during the next frame. This semi-asynchronous behaviour can cause frame stuttering and can be confusing because load does not complete immediately.

For more information about LoadScene(), click here.

Load Scene Async

Load Scene Async loads the Scene in the background and is spread over multiple frames.

In general, it is recommended to use the Async method since it is much more efficient spreading the loading over several frames instead of one, it works perfectly in a Player Build, however in the Editor itself it might stutter and freeze because the Editor does not support background operations very well.

On the plus it leaves us the option to access the loading process which is useful for when I’ll introduce Loading Screen Behaviour to the game in a second part of this article.

Because Load Scene Async is an Asynchronous Operation, we have the ability to yield information from the loading process (similar to how a coroutine works).

For more information about LoadSceneAsync(), click here.

This being an introduction to SceneManagement in Unity we’ll cover other classes and class properties of interest as we get further into the development process. Yet if you already want more in-depth information you can visit the official Unity Documentation. What’s left for us to do now is to apply some basic scene management functionality to our own game.

Implementation

To start using the Scene and SceneManager Classes in the Space Shooter Project, I’ll implement a Restart() method which then can be triggered by the Player through a button in case a level restart is wished for.

First I’ll add a new GameManager.cs Script to a new Empty GameObject. This script will contain any information that is related to the Game Logic, for example Game Over Logic, Pause and Play and so fourth.

I can now also remove the game over logic from the Player.cs and add it to the GameManager.cs since we have a separate script for this type of behaviour.

Then, to another new Empty Game Object in the Scene Hierarchy, I attach a script called SceneLoadingManager.cs. This script will handle anything related to Scene Management in our Game.

To the Game Over Window I add two new buttons and updated their text to display as ‘Quit’ and ‘Restart’.

In the SceneLoadingManager.cs I create a public method which can be accessed by the GameManager.cs, responsible for Loading a Scene by Index number.

I also create a method to return the currently active scene which again can be accessed by the GameManager.cs;

Then in the GameManager.cs, we should grab a reference to the SceneLoadingManager.cs, together with the Game Over logic it should look like this;

In the Restart() method we can now request the active scene through the SceneLoadingManager.cs and reactivate it if it’s Game Over.

In the Editor, the GameManager Restart() method should be assigned to the ‘Restart’ Button On Click Event, like so;

In the Player.cs, all what is left to do is to notify the GameManager.cs that the game is over when the Player dies.

Now when the Game is Over, we have the ability to restart the active scene. We did that by fetching the current active scene and reloading it through the GameManager when the restart button is pressed.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *