Monster shadow

VIDEO

Scene Manager - Load between scenes and show a progress bar - [ Unity Tutorial ]

Description:
Show a progress bar to your players as a new scene loads. Loading your scenes asynchronously can help reduce the time users have to wait between levels.
ADD A COMMENT

Bobby Yudis Hendana
thank you sir. but, i wanna ask about, can we randomly load a different loading image screen to another random stage scene?
Indiyanathan
scenemanager not in visual studio
Tarodev
Add "using UnityEngine.SceneManagement" to the top of the file
Aydın Bekoğlu
thank u so much.
Renegen1
I think your videos show a high level of skill with programming, but you're a lousy teacher. You simply can't do 5 other things not related to the task you're teaching. Every time you create any kind of special variable, function, you need to reference that to other videos which describe in depth that task separately. This is how you can choose your next videos. Every single logic in your code needs its own unique video, and it needs to be explicitly acknowledged. Also, tying all your videos into a single large project helps immensely. Too many videos start already with a setup situation that the viewer may not relate to.
Tarodev
Valid points and something I'm slowly starting to improve (I've been told once or twice I need to explain certain lines a bit clearer). A major component of not doing it is because I'm trying to make my videos short and to the point... Explaining every line would make each video 30 minutes long, which is the opposite of what I wanted for the channel. For example I don't fill my videos begging for subs or shove any unessersary life story in your face as I don't want to waste your time. I'll keep this in mind as it's good advice. From now on if I write a special line I'll either link to another video of mine or somebody else's. Thanks for taking the time to give valuable feedback 👍
322ss
That 0.9 loading thing happens when you set allowSceneActivation to false. For some reason Unity has implemented this so that 0 to 0.9 is for loading the data (which can happen pretty much instantly on a desktop PC) and 0.9 to 1 is for setting up the scene/gameobjects etc, which might take more time...
322ss
Thanks for the video. Correct me if I'm wrong but 1:20 creating such singleton pattern where one calls Destroy on gameobject isn't actually going to always work how one would expect. If you call Destroy, it will destroy the object at the end of that frame, so Start and such will run on to be destroyed object (if your MB has such), unless you have some guard variable to check (wasDestroyed) to make sure that those don't run.
322ss
@Tarodev Maybe I've simply messed up something in my code earlier! Go figure. Anyway - great if it works!
Tarodev
I just tested this by placing breakpoints in start and it never hits (v2020.3.13f1). Maybe in an older version of Unity this happened?
GamesFromSriLanka
Does singleton approach work when we have several different level with a main menu...
Masky!
Yes, you would have to create a "loading screen caller" (a script that calls the singleton), and add it to all of your scenes. That way, you could have your main loading screen script on, let's say on your main menu, and once you load a different scene, you would be able to call that loading screen, since there's a script on that scene that calls it. Another approach could be using Game Events.
Snpr Skllr
super-useful you deserve way more subs
Not a Game Dev
Cool
SEE ALL COMMENTS


Transcript:

good evening friends today i'm going to show you how to use the
scene manager provided by unity and also hook up a loading screen
with a progress bar so first thing we're going to need to do is
create a new script and unity already has a class called
scene manager which i ideally would have liked to call
this class but instead i'm going to call it
level manager just to avoid conflicts open that up let's make a public
static level manager and we'll call that instance
and now in our awake function we will say if instance
equals null well we will make this instance equals this
and then we will say don't destroy onload and we'll
send in the game object so basically what that means is
if the scene that we're currently in gets destroyed
on the next scene it will still be there it won't be destroyed in between the
scenes and if it is in fact already been
uh declared and assigned to we will just destroy this version of the object
just like that next we'll create a new function and we'll call this
actually it will be an async function and we'll call this
load scene and this will take in a same name as a parameter
now we need to start loading our scene so
we'll call this scene equals uh scene manager dot load
scene async and we'll send in the scene name there
next we'll be preventing the scene from activating
automatically so uh we're not going to allow it to
straightaway transition to the new scene as soon as it's loaded
all right next we're going to grab a few references
so this first one will be a game object and it will be my actual loader canvas
object which will be the actual main object
here which i will enable and disable and the second one will be this
foreground sprite here which by the way is just a normal image
the image type is set to filled horizontal
starting from left to right and as you can see there that's that's the
property that we'll be setting so back here
another one private image and this will be my rigorous bar
now down here in our load scene function first
set uh loader canvas set active true so we'll be showing our
loader and now we need to periodically check to
see how much the scene is loaded and ultimately change that uh image
fill amount to reflect how much has actually been loaded
so i'll do that in a do while loop uh if you don't know what a do
our loop is it's the same as a while loop the only difference is that
the logic inside will run at least once and then once it gets down to this uh
while condition here it will just act like a normal while loop after that
so our loop condition that we're going to be watching for is just saying
progress is less than 0.9 so don't ask me why unity does this but
basically the scene is loaded at ninety percent not a hundred go
figure who knows uh but make sure you're doing it two
point nine and not one or else your scene will load forever
and ever and ever and over and over so now for the purpose of this demo
i'm assuming these scenes are going to load instantly which won't be very fun
and you won't see anything so i'm just going to add an artificial wait time
here make sure you don't add this line when
you're creating your loader and now i'm going
to set the progress bar bill amount
equals to the scene dodge progress okay so
i'm looping here uh every 0.1 seconds i'm going to update our
progress fill amounts and once it's completely loaded we're
going to continue here and then all you need to do to activate
your scene again is you need to take this
and you need to change it to true as soon as you tell unity that the scene
can activate they'll activate it and you will be
taken to the next scene next we will disable our loading canvas
now that the new scene has loaded because you have to remember that this
is a singleton so when the new scene loads
this object will the very same object will still be present in the new scene
and it will have the loaded canvas showing so we have to disable that
next we actually need a way to trigger this function to load to the next scene
so back in unity you'll see i've got these
two buttons here go to scene two and then in the next scene i've got go to
scene one and on my buttons i've just got a script
here called change scene button and then in the button on click event oh
my face is in the way let me just move over here
in the button uh click event i've got i'm referencing this change scene button
and sending in the scene that i'd like to go to and i'll just show you what
that script actually looks like it's literally just a function with the
same name parameter so in this i'm going to say level
manager instance uh singleton instance and i'll
say loadsene and send in a same name
and that should actually be all we need to change these scenes here
ah no actually we got something so on the level manager let's actually
attach our level manager script and i'll send in my actual loading canvas objects
and then a foreground of our progress bar
all right now that's done we should be able to try this out so
go to cn2 yep working as expected go back to scene one back to
scene two there we go so uh this is not too
exciting and obviously it's uh it completely fills up to ninety
percent doesn't look too good uh if you'd like to show more of a
progression what we can do is we can
instead of setting our fill amount directly to the scene progress
we can do it over time so let's create a reference
a float and it will be our target now instead of setting the fill amount
directly we'll set our target to that instead and then in a update
function we'll say our fill amount equals to math
f move towards we'll send in our current
uh fill amount we'll say our target which we're trying to get to
and i don't know three times time dot delta time uh
and now also as this as we're reusing this scene manager in between the scenes
uh every time that we show it we would like to
reset uh filber wouldn't we or else it's just gonna always start at a hundred
percent okay progress bar dot fill amount
uh equal zero and we'll also set our target back to
zero as well just like that let's try it out
yeah there we go that looks more authentic
so yeah obviously you could improve on this quite a lot you could for example
add a fade in and out to the screen here to make it less
jarring you could add some tips below the
loading message or whatever you could add
you could create a level select screen where you would have a
layout group here with your 50 levels here or what have you
um if you don't know what a le what a layout group is i'll i'll put a
annotation up here to a video on it uh and then
each one of those levels would have a the the change level
change sense scripts with just a different parameter in it and you would
you would just click on it or change the level so if you learned
something leave a leave a like subscribe and i'll
see you next time for another sexy tutorial have a good day