Google Play Services for Unity | In-Depth Guide | 2019 | #2 - Achievements & Leaderboards

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
my name is Alexander master Kelvin in this video I will show you how to implement leaderboards and achievements using the Google Play services plugin for unity in 2019 in the previous video we've set up the actual plugin in unity and on the Google Play console and in this video I'll show you how to implement all of the scripts and hook it up all up together so that everything is working so if you haven't watched the previous video please do so right now because you will not be able to understand anything that I'm talking about and if you have any questions or if you're stuck please join my discord down using the link in the description and ask questions there so the first thing that we want to do is we want to go back to our Google Play console click on game services and go to our GPG demo they've created previously now before we've had a leaderboard we're just gonna leave that as is we're gonna use that leaderboard that we've had we're gonna go and we're gonna add another achievement instead an achievement instead now we're gonna call this one an incremental we're gonna make two one of them is going to be an incremental achievement and another one is just going to be an unlockable so the difference between an incremental and unlockable achievement is that an incremental achievement is something that you can work your way towards and you can show the progress so for example killing 20,000 zombies if you kill one zombie you're a part of the way there if you kill you know 2,000 zambese your tongue like one tenth of the way there and whatnot so that that's sort of an incremental achievement something that you can count up to so we're gonna check incremental achievements for this one the incremental we're just gonna check that and we're gonna put a value something low like ten so that we can easily unlock it and we're gonna add another achievement called a regular so the regular achievement is going to be a non incremental and we're just going to save it a regular achievement is going to be something like killing a boss for example well it's really difficult to tell whether you work your way up to killing the boss and you don't want to annoy the player with saying like oh good job you got the boss down to 10% of his health every single every single time you hit the boss so yeah that this is going to be a regular achievement versus an incremental one we're gonna save these and we're gonna get resources once again we've done this before I'm gonna copy and we're going to go into unity and go into window google play games set up android set up we're just gonna paste these in i'm gonna click setup it's gonna say Google Play games configured successfully and if you have this Android dependencies resolution if it's working out then leave it alone but if it's stuck you can just close that thing up so now that we have everything plugged in we're going to create a couple of scripts ok so the first script that we're going to create is going to be more of a non just a utility script we can use for keeping track of the score so we can submit the score and use that for purpose of a achievement let's make a score class and we're going to just use a and have a public public static in score I'm going to set that to zero by default I'm going to do a public I'm going to be using text mesh for this if you're not using text mesh you have to be using it because it's it's basically better in every way shape and form than the default unity text setup so go and import that into your project and the other thing we're gonna be using is text mesh Pro you GUI and just score text so we're just going to keep track of the score text so we can we can see what the score actually isn't what we're submitting now the way that we're going to do this is that we're going to have a single score being counted the entire time and we're going to have a score up to update so essentially the score to update is going to be the difference between the score that we've had and the score that we have now when we send the score to the to the Google Play services console to their servers so that we kind of know the difference I'll send that difference to them they can update the score and then we reset the difference but we keep continuing our score on the local local machine so we're going to add score in the increment score function and score text is gonna update that scoretext two-string and here's what we're gonna do we're gonna do player per f-stop set int oops score to update we're gonna say that two-player perhaps dot get int so this this is what we're actually getting the difference between the scores so where is that what we're essentially doing is saying okay so get the score to get the difference of the score that we've had add one to that and set it so you're just incrementing that score that we're saving in the player prefs this is simply for the purposes of sending updates to the server once we send the update we're just gonna wipe this score to update but our score is gonna stay the same so this is a little utility script we have that ready let's just I don't know plug that into something just have that that on a score we keep a score manager if you want whatever you can you can call it whatever you want for now we don't bother about plugging anything in we'll figure that out later right now what's important is we're gonna create a few more scripts we're going to create a GP GS achievements and in the achievements script once once it opens it's not the one achievements okay so in the achievement script we're only going to be using two things here we're going to be using the Unity engine and we need to use Google Play games now the first thing we want to do is we're gonna have three functions in here we're gonna have a open achievements panel to open the actual achievements panel when you want to see what achievements you have and so on or you can do well the other methods are going to be updated incremental achievement and unlock the regular achievement so updating the incremental achievement will send that difference we talked about in the scores this difference is going to send it over to to the server's so that we can update it and it can trigger the achievement so first thing we're gonna do is are gonna go public void open achievements open achievement panel okay and here we're gonna be using the social class built into unity show achievements UI so social show achievements UI this will actually display an overlay over your app you don't have to implement anything it will simply just open up an overlay as long as you're logged in the other one we're going to be doing is update incremental here we're gonna be going play games play games platform dot instance now we've initialized this instance using the Authenticator so right here the platform we're gonna Google Play game platform dot instance this is what it's going to return so we're gonna go dot instance dot increment achievement and here's a really interesting thing do you remember when we've set up the Google Play games set up Android set up now that actually saves our achievements the strings for the achievements that we have directly into a separate class called GPG SIDS you can change this if you want but all that class does it's in right here it's in the assets folder or wherever you set it up all it does is just set those constant strings so that you don't have to you can just reference that class instead of having to type out the string every time and if something changes it will be updated so let's call GP g GP GS IDs right here dot achievement incremental okay and we're gonna increase that by one and here's where we can do a call back and the call back will take a boolean success and it will oops it'll return true or false whether the update has succeeded or did not now we're not going to bother with this right now we're gonna go to show you this a bit later we're just gonna put null so we don't want to call back when the update increments the other one we're gonna do is unlock regular regular so this one is going to use the social platform I know it's a little bit weird how this one uses the play games platform instance to increment the achievement whereas this one uses report progress but that's just the way it is so follow the steps and you should be good so GP GS IDs dot achievement regular and we're going to do a hundred F with no callback so what is this a hundred F do the 100 F is basically saying unlock this achievement just give it full progress we're reporting 100% of the progress of this achievement to Google Play now what you can also do is if you have a regular achievement you can make this a hidden achievement what this will do is essentially when you open up the achievements panel it won't show up there so use this when you have say spoilers when you have you know some sort of twist in the end of your game where the bad guy turns out to be you all along you don't want to have an achievement saying become the bad guy or or you were the bad guy all along achievement now that will kind of ruin the game if people look over and they see that achievement like that's you know that sucks so keep those hidden if you want to unlock unlock the achievement you have to use a F here but if you just want to reveal it to the player so that they are aware that that achievement exists whether they achieved it or not you have to put zero F so this will basically say update report the progress but don't give any progress to that achievement just show it to the player otherwise it will remain hidden and they won't be able to see it now this we're done with this class so we're going to go and create a new class so we're gonna have three different classes let's go ahead and create a new class called GP GS and this one's coming company called leaderboards and now the leaderboard script let's go ahead and open that up wants unity loads okay one this script is going to be using just unity engine that's it for this one we're going to create a function called open leaderboards oops leaderboard I guess leaderboard and we're gonna call social social dot show leaderboard UI in the previous one we did show achievements you why this one is going to be show leaderboard UI and what this is going to do is going to show an overlay like the achievements overlay which you don't have to implement it will just show you the leaderboards for all the players who have reported scores worldwide so then you do public void update leaderboard score now in this one we're going to be using of the player prefs score to update to kind of update the leaderboard saying hey we've made this much progress so go ahead and put that progress on the leaderboard so that we can stay up to date so essentially I'm not just competing with myself I'm sending the scores that I've had over to the servers and the server said okay well great thank you you know we're gonna put that in there and show it to other players when they open up the leaderboard UI so now in here we're going to make sure that first thing that we want to do is just make sure that the score is not zero right like we don't want to just update the score if it's zero if nothing changed now that would just be a useless call over to the server so don't don't send too many calls do it as as little as possible so do it on scene switches do it on you know if someone presses the Save button or Update button or do it occasionally once every five minutes like an auto saver cloud save but do not spam do not spam that that would that would basically it can reject your updates and sometimes if you just keep spamming with with too many you'll update the first time and then we'll just ignore you for a while so don't do that try to avoid that let's do get int we're gonna do score to update the the same one we're using in the other script default it to zero if it's zero if if we have an update of the score just return just get out of here we don't we don't need to update anything however if not then we're gonna do social dot report score oops report score not report progress the score we're gonna use is player prefs dot get int score to update with a default value of zero I guess one because we don't want to you know this would be zero anyway so let's just do one because otherwise it would be useless and the the board this is the name of the leaderboard that we want to update the score to and the name of the leaderboard is going to be the leaderboard that we've created on the Google Play console that's gonna be in GP GS IDs dot leaderboard high score and for the callback this is going to be this is we're actually going to write a callback and the callback is going to reset the difference of the progress that we get from player preps so what we're gonna do is we're going to take in a boolean called success I'm gonna do a little bit built-in blend the function okay and here if if it was successful then player perhaps dot set int score to update will be zero so essentially what we're saying is if I report my score with the difference of my score over to the server's and it succeeds and it updates the score which will send it back in a call back you'll say okay well it's successful so reset your current difference in score to 0 just say look you haven't changed anything so that you can't spam this button and when you haven't changed your score so let's save it like this and we're done with the script that's it we are done with the script so what we're gonna have to do now is create a few more classes sorry a few more objects over here so we're gonna to create two more game objects one we're gonna call GPG s achievements and of course GPG s leaderboards so for the achievements we're going to drag the achievements script down here and for the leaderboards we're going to drag the leaderboard script you can do this all in one class I just prefer to have it separate so that you know if you don't want the Authenticator available in all places I mean if it's a singleton or whatever that's fine but you don't really need it there so you could you know put it separately if you want you could just put all of those game objects on into one parent called GPG s or whatever but I just prefer having this way with all scripts are separate so very very good thing that I created is the UI so I just made a little UI canvas which has a simple buttons right like you could do this on your own time I don't want to waste time so I'm not going to do that I already made that so we have a leaderboards button that will open up the leaderboards panel the overlay we have the achievements button which will open up the achievements overlay we have the the score in the center which will just show up our score we have the increment score button which will just basically increase the score and an unlock button which will unlock the unlock the achievement unlockable achievement the regular one and we have a submit scores this will submit the scores to the leaderboard now none of the stuff is plugged in yet because we just created this script so that's what we're going to do right now let's start with leaderboards so the first thing we want to do is click that here and gbgs leaderboards which is going to do open leaderboard same thing with achievements except with GPG s achievements open achievements panel now for the increment we're gonna do a few things we're going to first of all score I'm gonna do increment score okay let me just double check one thing here ya see it doesn't really update anything now what we kind of want to do is we want to increment the score every time you send every time you update the score you want to increment that score on the for the achievement side saying like report one more score for that achievement report one more score for that achievement so you're kind of doing that over and over again so we're gonna plug in GP GS achievements and in here we're going to update incremental now remember if I go here to update incremental what does it do well it just increases the score by one it just increments the achievement called achievement incremental by one so it just updates at once saying every time you press a button it sends they're saying this person basically got one more point and this will automatically unlock the achievement once you once you reached a certain threshold so do unlock the unlock is going to be just the unlockable achievement so we're gonna do unlock regular and submit scores this one is going to take leaderboards and here we're going to do that what was the other thing that we update leaderboard score salute update leaderboard score is this function we just wrote where we check if it a score is zero and then we social report score that score two to Google Play services now I'm going to build this on my phone and we're gonna see how it works just a quick thing remember to plug in the score text over here yeah kind of forgotten they build it and it didn't work so let me go build it again and now it should work okay so so the application has started I'll show you right here okay it's asking me to login so let me go do that just okay yep just asking me to would to allow some stuff and there we go I logged in that's great okay so now when we tap the increment score their score should increment there we go once we reach 10 it should unlock the achievement five six seven eight nine ten boom there we go there's the achievement incremental antastic so that just basically automatic you don't have to do this is all an overlay I didn't program any of the overlay that pop up or anything like that so let's go check the achievements using the achievement button at the top ah yeah so that that's also an overlay and it shows that we've unlocked oh man this is a nightmare to put this like that okay so it shows that we've unlocked two achievements oh sorry we we have two achievements and we will unlock one and it shows that the other one is locked right there so let's go ahead and unlock that one for us so just press the unlock button and that should unlock the achievement for us whatever right so let's click on that and that should bring it up to there we got two other two achievements so the achievements are working as intended now let's try to see the leaderboard so I'm gonna tap the leaderboard and we're gonna notice that there is a leaderboard rank for me as I press the button so if I press submit scores there we go you can see that it is Supes just tap it again that it is right there the entry is right there and now let's try to increment this say 236 or something I'm gonna click Submit scores click leaderboards now you see that it's 26 the score has updated and this will be able to be done so the problem is that if you keep incrementing score and then submitting it and then incremented a little bit submitted increment submitted this kind of counts as spam so the scores might just not update they just might ignore that and say you know what this guy's been spamming so many scores every three seconds so we're not going to to count that so be careful with that make sure that when you implement these scores you are sending them very very occasionally so for example if you have some sort of puzzle or whatever you're solving and then at the end you get this huge reward of like two thousand tokens and the tokens is what the leaderboard accounts submitted once once the player solves the puzzle if the puzzle is not like every two seconds or something like that solve it at the end when you get one you know how much player is gonna get when you know the reward send that big chunky reward over to the services don't do it very often because it's sometimes it just won't update so that's been achievements and leaderboards in a unity using the Google Play services plugin I thank you for watching please go to the description join my discord channel if you have any questions or if you're stuck and I'll see
Info
Channel: AMastryukov
Views: 15,712
Rating: undefined out of 5
Keywords: Google Play Services, Google Play Services Plugin, Google Play Plugin, Google Play Services Unity, Google Play Plugin Unity, Achievements, Leaderboards, Google Play Achievements, Google Play Leaderboards, 64bit Android, Unity Android, Unity Google Play Services, Unity Guide, Unity 2019, Unity Tutorial, google play
Id: joY-RQguwI4
Channel Id: undefined
Length: 22min 58sec (1378 seconds)
Published: Thu Jul 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.