Disc Golf Scene Play It Again Open Lancaster
Loading Scenes In Unity
Even though loading a different Scene in Unity only requires a unmarried line of code, depending on varying situations at that place'southward quite some to say about how we can employ Scene Direction functionality.
Depending on the size or structure of your game yous might want to carve up the game into different Scenes, each representing a level or a part of a level on its own.
In that location might exist situations where we need to access a specific Scene, like the previous or next Scene. Or maybe we just desire to load the Scene with a item 'SceneName'.
We may want to display additional loading data on screen like a loading progress bar or some game tips, during the 'loading fourth dimension'.
We have quite a few methods provided past UnityEngine.SceneManagement assuasive united states to manage scene loading behaviour, in this commodity yous'll be introduced to a few important concepts. In addition we'll showtime implementing scene management behaviour to our own space shooter projection.
Preface
Amongst a few other classes provides past UnityEngine.SceneManagement there are two classes that I swiftly want to highlight; the Scene and SceneManager Class. They aid us derive information from our Game Scenes and how nosotros tin can efficiently manage storing or switching between them.
Scene Course
Run-fourth dimension information 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. Similar: "Assets/MyScenes/MyScene.unity".
- buildIndex : Returns the index of the Scene in the Build Settings.
For more information near the Scene Class, click hither.
SceneManager Class
Scene direction 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 u.s. with a range of Static Methods assuasive u.s.a. to handle Scene Management. A few examples:
- GetActiveScene : Gets the currently agile Scene.
- GetSceneByBuildIndex : Get a Scene struct from a build alphabetize.
- 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 nugget path.
- LoadScene : Loads the Scene by its name or alphabetize in Build Settings.
- LoadSceneAsync : Loads the Scene asynchronously in the background.
- SetActiveScene : Set the Scene to be active.
For more information nearly the SceneManager Class, click here.
Loading Scenes
First of all we demand to make sure that the Scene we desire 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 Listing. Alternatively you tin add all agile scenes to the Build Settings by clicking Add together Open Scenes.
So we need to make a new Script where we desire to load our Scenes from. Y'all can call this as yous like but I retrieve it'southward handy to proceed logic that handles scene management separated from other types of behaviour.
In this Script we need to add the UnityEngine.SceneManagement; namespace giving us admission to the Scene and SceneManager Course 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 Unmarried Mode, which unloads the previous Scene immediately upon loading the next Scene. Or, information technology can be loaded in Additive Style, keeping both Scenes active at the same time in the Hierarchy while overlapping each other in the Game View. Just declaring a unmarried parameter will execute loading a Scene in Single manner.
When to utilise 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 Alphabetize number is not the final ane, add together i to the index and load that scene.
- If the Scene Index number is not the first ane, remove one from the index and load that scene.
Simply go on 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 lot can hands rearrange them.
Load Scene vs Load Scene Async
Important to know is that there are two methods bachelor for Loading a scene: Load Scene and Load Scene Async.
Load Scene
Load Scene loads the Scene directly with loading taking place during the adjacent frame. This semi-asynchronous behaviour tin cause frame stuttering and can be disruptive because load does non complete immediately.
For more information virtually LoadScene(), click here.
Load Scene Async
Load Scene Async loads the Scene in the background and is spread over multiple frames.
In full general, it is recommended to apply the Async method since information technology is much more efficient spreading the loading over several frames instead of one, information technology works perfectly in a Player Build, even so in the Editor itself information technology might stutter and freeze considering the Editor does not back up background operations very well.
On the plus it leaves the states the option to access the loading process which is useful for when I'll introduce Loading Screen Behaviour to the game in a second office of this commodity.
Because Load Scene Async is an Asynchronous Operation , we have the power to yield information from the loading process (like to how a coroutine works).
For more information about LoadSceneAsync(), click here.
This being an introduction to SceneManagement in Unity nosotros'll comprehend other classes and class properties of interest every bit we get further into the development procedure. Yet if you already desire more in-depth information y'all tin visit the official Unity Documentation. What's left for united states of america to do at present 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 and so can be triggered by the Role 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 whatsoever information that is related to the Game Logic, for example Game Over Logic, Pause and Play and so quaternary.
I tin can now likewise remove the game over logic from the Player.cs and add it to the GameManager.cs since we have a split script for this blazon of behaviour.
Then, to another new Empty Game Object in the Scene Hierarchy, I attach a script chosen SceneLoadingManager.cs . This script will handle anything related to Scene Direction 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 tin 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 once more tin can be accessed by the GameManager.cs;
And so in the GameManager.cs , nosotros should grab a reference to the SceneLoadingManager.cs, together with the Game Over logic it should expect like this;
In the Restart() method we can at present asking 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, similar and so;
In the Player.cs , all what is left to do is to notify the GameManager.cs that the game is over when the Role player dies.
Now when the Game is Over, we have the power to restart the active scene. We did that by fetching the electric current active scene and reloading it through the GameManager when the restart push is pressed.
This article should provide a business firm ground of agreement how UnityEngine.SceneManagement works and how nosotros tin use it in our projects to handle scene related functionality. We learnt about loading scenes and how to retrieve information from them, which is a disquisitional pace in social club for us to add more features to the User Interface Arrangement in the future, such as dynamic loading screens or other UI menu'south. I am definitely looking forward to continue, what about yous? Adept luck with it!
← Previous | Next →
Source: https://medium.com/geekculture/loading-scenes-in-unity-98e446756497
0 Response to "Disc Golf Scene Play It Again Open Lancaster"
Post a Comment