How to keep a timer running while your game is closed | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when you minimize a unity game on a computer or click the home button on a mobile game the app stops running and any code that was running freezes this is problematic for systems that need to run regardless of whether the app is open or closed like a game timer so I'm going to show you quickly how to make sure a timer acts as if it doesn't stop running when the game is closed and then reopened first I'm going to create a simple timer so if you already have one just skip this part of the video so creating the timer I'm going to quickly make the timer text and put in a random number maybe Center okay and then I'll create the type of script called timer and open it up cool so for now I'll just give it all this stuff so to make the timeout first we'll have a field that takes the text so call it timer text uh if you're not familiar with serialized field it's basically just the same as writing public so that it should so that the field shows in the editor but uh stops it from being public so you can still sell it to private but it will show in the editor um so if I save that and go back to Unity in my timer I can add this timer script to it and you'll see that the timer text appears and it allows us to drag the text message Pro component into our timer text script so that's all good let's go back into the script um I will make another field called the actual which will you know store the actual timer uh in seconds so put it as a float and then a third one which is a Boolean and we'll call it as timer running and this will tell us fun enough if the timer is running um and the fourth one actually will make called timer duration which can be the start duration and we'll set it to 60 seconds or 60. so make a start function and we will set the timer equal to the timer generation so this is basically setting our timer to 60 seconds and we'll set the time is time running equal to true no true cool and then we'll have the update function which will decrement the timer so we'll check if the timer is running and if it is we will time do timer time dot Delta time if you're not familiar with this uh Delta time it says here it's the insulin seconds from the last one of the car one so if it basically allows us to run something without it being dependent on the frame so if you're running a 60 FPS then you're going to have this update function run 60 times a second and that means that you want to decrement your timer by a 60th a second and so after 60 frames in a second it will go down by one if that makes sense and so 30 frames will be different but because we have Delta time uh the overall result will be the same in one second the time will go down by one cool and if the time is running we want to actually check first that time is greater than zero into that so we'll check if it's above zero and then if it's not we will set the timer equal to zero just in case it happens to go slightly below zero um also at the time of running the two false so this is basically uh this is our basic setup of a timer that runs in the update Loop and the final thing you need to do is make sure that the timer text we have in our game updates based off the time we have in the script so to do that we just reference our timer text field I get the text component and then set it to our internal timer and convert that to a string so now if I save and go back to Unity press play you'll see that we have our not so nice looking timer and that does what we needed to do so go down every second and the problem with this is that if you minimize the window it's going to freeze the timer whatever time that it was when you minimized it and when you reopen it it's going to start from that time and we don't want that in most games you want the timer to continue when smart closes or at least in online games for example so make sure that if you're in Project settings and you go to player and you're in whatever I'm doing in Windows Max landline settings if you open resolution presentation you need to make sure the running background is ticked off um because if this is tick then it's actually just going to function normally and if you minimize it it will continue to run so running background make sure that's ticked off and the reason is because like in a mobile game for example this isn't an option you can't run in the background in like iOS or Android games you you they just don't let you run code um from your game if it's closed that's that so now if I play this and minimize it at 55 seconds and then I'll wait five seconds and what we want is it to go down to 50 seconds but what's actually going to happen is it'll start it'll it'll just continue from only minimized it so 55 and that's a big problem so to fix this we can do something very very simple before you go back to our time script and we create new function we're going to use a UNT API called on application pause so on there location pause it's a callback that has a ball in it that will be true I'm going to change this to his post when this was fired the game is paused and false if it's not so what you what we need to do is we need to check that our timer is running first because this will be called on start and we don't really care um everyone's caught on Star at the beginning so we'll check if the time it's running and if the ball is equal to true so this is just this is essentially just shorthand version for that so if the Boolean is equal to true we're going to debug.log first and just so you know the game paused and then else so this will just be if it's if it is pause ball is equal to false then the game has been unpaused unpaused Okay cool so we can just check this quickly that it works I'll go back to Unity start it and I'll put the console up so now if I minimize it at 55 seconds and reopen it again you're going to see that we've had a game paused and then a game unpaused so we know that it's working and it's been called properly so now we just need to add some functionality and the best way to do this I found is to log the current time uh that the game was paused and then when it's unpaused you look at the current time it's unpaused and adjust the time by the difference so to do this we're going to create a new field called of type date time and we're going to call it dates time of pause let's go with that and then here when the game is sports we're gonna we're gonna set the date time off pause equal to oh yeah Visual Studio dinner for me we're going to set it to daytime.now which will be the current time in the current date and then in uh the unpause section we're going to set up we're going to change our timer by the difference so we're gonna get the date time dot now which is the time when we unpause it and we're going to subtract the time that we've logged when we did pause in which is that and then we need it in seconds so we're gonna we don't care about the day obviously unless I've paused it for a really really long time uh and so this is what we want and you'll see we get an error because it says complicity cover double to a float so our timer is a float but uh this total seconds is a double so let's get rid of this we can just cast it as a float and it'll be happy okay so now that we've done that well mostly there to be honest so I'll go back to Unity and I'll stop and I'll minimize it at 55 seconds no and when I unpause it uh instead of starting to get a 55 seconds it should start about 50 seconds because I'll have paused it for about five seconds if we go back okay I might pause it for 10 seconds apparently uh yeah and so that'll work so again if I minimize it at 38 seconds uh weights one two three four seconds uh it will start again at lower and so you'll see that we're just in the timer by the time we paused for wanted so that's how you make a timer that adjusts for the time that was paused hope that helps
Info
Channel: Archions
Views: 1,340
Rating: undefined out of 5
Keywords:
Id: RrDgCqQfl8g
Channel Id: undefined
Length: 8min 38sec (518 seconds)
Published: Sat Jan 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.