Designing a Loading Screen in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
loading screens we all have our favorites some are short some are long some are beautiful and summer well you tried I guess the point is whether you like it or not loading screens are an essential part of almost every video game so it's important to know what they're for and how you might want to go about making one let's imagine for a moment that our player is a member of the audience about to watch a stage show the show starts in less than 5 minutes and none of the sets are on stage yet so we need to get the set for the first act on stage before the show starts you may choose to just start building the sets and however the audience watch the crew build the set and then watch all of the actors get into that starting position an awkwardly assemble before the lights go down and the show actually starts or you draw the curtain with the curtain drawn the crew can run around hectically before the show starts and the audience can experience a smooth start of show with no idea what was going on just seconds before that curtain was drawn if you haven't gathered already the curtain in this analogy represents a loading screen so let's take a look at how we can create our curtain hi there I'm Matt and welcome to game dev guide in this video we're going to take a look at how we can create a loading screen in unity we're going to look at how we might approach managing our scenes and also a few different ways we can use a loading screen to our advantage I've got two scenes here a super lightweight menu with just a few pieces of UI and then an extremely detailed environment that I'd like to load into the first thing we're actually going to do is start with a persistent scene this is just an empty scene with a game object in it and a mostly empty game manager script this is going to act as a director for the states of the game and will as the name suggests persist between all of the different scene States and manage the logic for moving between them so let's jump into the script and start our workflow below the singleton assignment let's load straight into our title screen as that's the first thing we want to see when the game starts we want to load the scene additively meaning that we want it to load in addition to our persistent scene so we'll call scene manager dot load scene async and load our title screen with the additive load scene mode something I like to do when dealing with scenes is to Korea an enum with all my scene names in and build indexes assigned this way I can easily reference their index and don't need to constantly check back and remember their names and much less likely to change their build index then I am the scene name and if I do change the build index it's super easy to update in the load game method or write some pretty standard code for loading in our main scene we'll want to unload the title screen and then additively load in our gameplay scenes if we just hit play and try to load into it you can see that we're sort of stuck in a weird state where we can see the old scene and there's no real feedback that we're loading into our environment we also get this weird and frustrating no camera message from unity overall it's just a bit messy this is the first case for why it's a good idea to have a loading screen in place we can close the curtain and signpost to the player that their input did something and now we're setting the stage for the next thing so let's look at how to manage this I've prepared a loading screen as a prefab here it's pretty basic just a nice background image a little loading text and a progress bar in our game manager let's create a reference to it and assign it in the inspector then when we're loading our game we can enable the game object as you can tell that already feels much better we get almost instant feedback from clicking play and our scenes start to load without us being able to see them loading in the background the only problem is when our scene is loaded and ready our curtain doesn't draw a loading screen doesn't go away in our manager script let's create a list of async operations called scenes loading and let's add each of our load scene async methods into this list will then create a new ko routine called get scene load progress and iterate through the list while the scene hasn't finished loading will yield null then at the end of our loop will clear the scenes loading and set a loading screen to false now our loading screen will show and when both scenes are fully loaded by unity our loading screen will be removed this is a great place to start but our progress bar isn't exactly very helpful in letting us know how much we've got left to go so let's jump back into our manager script and start tracking progress let's create a reference to our progress bar in our game-manager script for each operation in our list we'll total its current progress then or divide our total by the number of scenes in our list and multiply the result by a hundred to get a percentage for our progress bar now when we load our scene a progress bar gives us an indication of the total progress of all of our scenes loading and that's the basics of handling a loading screen as I'm sure you can tell it feels a lot cleaner and a lot nicer putting up a screen like this as our scenes are managed in the background so that's how we can manage progress from unity scene loading system but usually most of us need to initialize some things and put things in place after our scene is loaded for instance I need my city to populate with pedestrians when the scene loads if we just go to the scene as soon as it's ready we end up seeing this it takes a good number of seconds for the spawn script to start and finish and for all of the agents to get moving I'd like to avoid showing this initialization to the player so we need some way of hooking the initialization of our scene into a loading screen I'll create a singleton reference to the script I'm using to initialize my pedestrians and add a float called progress I'll also add a boolean to check if the progress is finished then we'll simply track the progress by dividing our count by the total number we're spawning and after our spawn loop we'll just wait half a second before flagging the process as done this allows the agents enough time to start up we could go through each agent and check individually but it's cheaper and easier in this instance just to designate a small amount of time to wait back in our loading script let's create a new Co routine could get total progress will use this Co routine to calculate the progress of both our scene loading and our initialization progress will move our progress bar code into here too and create a new float called total spawn progress will create a while loop that repeats if our initialization script hasn't loaded yet or if it isn't done in here we'll either set the spawn progress to zero if our script is null or get the progress directly from the script and we'll multiply it by 100 to get the percent then we'll calculate the total progress by adding our scene progress and our spawn progress and dividing them by two then by assigning the result to the progress bar we get the total result once the loop is complete we can remove the loading screen now our loading screen shows feedback for both all the scene loads and initialization by hooking these into co-routines like this we can create feedback points for different stages it can be traditional in loading screens to tell the player just exactly what's going on and what they're waiting for so with the extra information we now have let's take this one step further and hook that into our loading text let's create a reference to our text field and assign it in the inspector then in our script let's format a string for when our scene is loading and then when we've moved on to loading our spawn progress we'll show the progress in our text field if we were to add more things to initialize in our scene we could create an enum for each stage and assign the current stage in our initialization script a loading screen would then check what stage were on and return a string and value depending on which stage we're at so that's more or less the basics of dealing with scene management and analyzation progress I could wrap it up here but as you probably know I'm never one to do things by half-measures if the payers going to be staring at a curtain regularly probably want to make that curtain as interesting or as helpful as possible and there's a few simple things we can do to make it just that bit more interesting the first thing we could do that's a core trope of a loading screen is changing up the point of focus let's create an array of sprites and a reference to our background image then when the screen is enabled we'll simply pick a random image from our array to display now as much as we'd hate to admit it and keep loading time short some hardware may end up taking quite a long time to load these scenes so we might as well give people something to read while they're waiting this is the perfect time to give tips to the player about your game let's create a pool of tips that we can slowly iterate through we'll create a new text message on our loading screen and set it up to hold our tip text will also add a canvas group to it as this will allow us to fade the text in and out then in our manager script let's create a reference to our tips text field and also an array of strings for our tips in unity let's assign the tips text and write out our tips as soon as we enable the loading screen we start a new curve routine called generate to tip here we'll assign a random tip from our list and then while the loading screen is still active we'll iterate through the tips with just a little bit of extra work we've created a much more interesting and more dynamic loading screen you can maybe even go a bit further and have the screen transition in and out or perhaps have it more interactive all but I think this is a great place for anyone to start now I've mostly just covered loading and unloading of a scene in this video but there may be more things you're doing in your game that could use a loading screen for instance if you're making a platformer or adventure game and the player dies you might not need to completely reload the scene but you might want to reset some things that would be the perfect opportunity to throw up a loading screen or you clean up the scene and again if you know what you need cleaning up you can give feedback to the player in much the same way but that's about it for me for now if you've enjoyed this video be sure to give it a thumbs up and let me know what you think below hopefully there's some information in here that's helped you out or helped you think a bit more about the kind of loading screens you could create also if you haven't already please consider subscribing to the channel and if you're interested in sticking around and seeing more videos like this maybe check out some of the videos on screen now as always thank you very much for watching and I'll see you again next time you
Info
Channel: Game Dev Guide
Views: 106,671
Rating: undefined out of 5
Keywords: unity, learn, games, gamedev, how to, making games in unity, tutorial, unity tutorial, learning unity, unity3d tutorial, programming, making games, loading, scene, scene management, loading screen, the sims, no mans sky
Id: iXWFTgFNRdM
Channel Id: undefined
Length: 11min 56sec (716 seconds)
Published: Mon Dec 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.