Google Play Games Services Tutorial (Unity) #1 - ACHIEVEMENTS and LEADERBOARDS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello welcome to reso in this tutorial you're going to learn how to use Google Play games in unity to make achievements and leaderboards we're going to take a look at saved games in another video firstly you'll need to download the unity package from the link in the description then you'll install it like this you will go to the download location while the unity editor is open and uh install it double click click on import and that's it then you want to go to the Google Play developer console go to the game services and add new game I've already added a game so I'm just going to open it then you're going to add achievements and leaderboards I've already added them and left them at the default settings except that I changed one achievement to be incremental like this so achievements incremental achievement and I've ticked that it is incremental and I've put here value of 100 this tutorial is going to be really generic because I feel that you can learn more when you don't clutter up the code and UI with unnecessary stuff we have only two achievements and one leaderboard which will basically contain the highest count of button presses simple but clutter free then after you've created achievements and leaderboards you want to go to the unity editor and change the current platform to Android you can do this by going to file build settings and choose Android from this menu and click on switch platform and now the plat form is switched to Android as you can see Google play games plugin for Unity is showing us some message we're going to click on okay and uh now we can actually use this plug-in so we'll go to window Google play games and setup android setup and as you can see we need resources definition we can obtain these resources at Google Play developer console by going to either achievements or leaderboards and clicking on get resources you want to get the Android resources just going to copy everything go back to Unity paste this in here and click on setup you can leave these fields as they are or you can change the name of the classes or the directory but I'm going to leave it a default click on set up it's going to take a while again resolution complete and Google play games configured successfully so that's awesome also if you want to build for Android just make sure that you have SDK and jdk installed you can check if you have it in preferences and external tools Android SDK and jdk however I'm not going to go over the process of their installation and setup in this video now we are ready to write some code we're going to start with the code specific to Google Play games we'll create a script called play games script so create C script play game script all right open it up in Visual Studio First St we can delete these us using statements and uh at our own using statements we're going to need to be using Google play games also Google play games basic API then in the start method we're going to need to initialize and activate Google play games Services we can delete the update method as we are not going to need it in this script and then in the void start we'll write play games client configuration games client configuration name it for example config equals new play games client configuration. Builder dot or yeah builder. built and here you can specify if you want to turn on some features like enable saved games but for now we don't want to turn anything on then we want to initialize the play games platform instance so play games platform dot initialize instance with the play games client configuration that we've just created named config then we're going to activate this play games platform by doing play games platform. activate then we want to sign in the user of our game so we're going to write a method void sign in and this method is going to do one simple thing just sign in the user so you'll write social. local user dot authenticate and this requires a call back so we're just going to do success and a little Lambda here and we don't want to do anything in the Callback however you could print something out to the screen you could Lo lock something or do whatever you want then we want to call this signin method from the start method we could put this line of code directly into the start method however this is better for clarity reasons now we can actually start dealing with achievements we'll create a region call it achievements just so that we can actually navigate the code comfortably and we want to have a method for unlocking achievement so public void unlog achievement and this method is going to accept an ID of achievement which is a string so string ID and we want to do in this method social. report progress achievement ID is the string ID that you supply to this method as an argument so ID double progress is going to be 100 because we want to straight up unlock it and we have to have a call back here we're again not going to do anything fancy then we also have an incremental achievement so we need to have a method to increment an achievement so public voyet increment achievement and this is going to accept also a string ID but also steps to increment the achievement so int steps to increment and we're not going to use social. report progress method for the this we are going to use another method because report progress is not really good for incremental achievements from Google Play games we're going to call play games platform. instance which is a Singleton instance do increment achievement and achievement ID is ID steps is steps to increment steps to increment and call back we're again going to leave it simple here we are also going to need to have a method which we are going to call to show the achievements UI Show achievements UI and we're going to Simply call social. Show achievements UI now let's go onto the leaderboards I'm going to create another region and in this region there are going to be only two methods add score to leaderboard and show leaderboards UI so public voy add score to leaderboard and this is going to accept a leaderboard ID and a long score we're going to Simply call social. report score we need to supply the long score also the board which is our leaderboard ID and again we need to supply a call back and that's it for play games script as you could already see I've created a simple UI the get point and restart button will increment some count variable and the value of that variable will be displayed on the screen however we don't have any count variable yet so let's create a small script which will handle that so we're going to create a script and call it manager script we're going to need to keep keep track of an instance of manager script this would normally be implemented as a Singleton but because I have only one scene in this let's call it the game I don't need to create a Singleton just keep that in mind when you're actually going to be coding inside your game with multiple scenes so we're going to create a public static manager script instance which is going to be a property with public getter okay get and private set then we also need the counter variable so public static int counter and this is also public get and private set then in the void start we're going to set this instance to this particular instance so instance equals this then we're going to have a method called increment counter which is going to be invoked whenever a player clicks on get point so public void increment counter and in here for now we're going to do just counter Plus+ so we're just going to increment counter we also need to have a method called restart game which is going to be invoked whenever a player clicks on restart button so again this is going to be public voy restart or rather restart game and in here we actually want to add the current value of the counter variable to the leaderboard so we're going to call a static method in our play games script class so games script do addcore to leaderboard oh yeah I see I'm a very big dummy and I actually didn't specify that these methods should be static so let's do it right now so static and this is also going to be static everything is going to be static in here all right cool so we want to add score to leaderboard and the leaderboard ID is stored in the class that was generated by the Google Play games Services plugin which is this gpgs IDs so we want to get gpgs IDs do leaderboard leaderboard and the score is the counter value then we want to reset the counter to zero now after we've made the manager script we're going to write a script that deals directly with the UI we're going to name it UI script and we're again going to keep track of a current instance of the UI script this is again something that you should Implement as a Singleton in your real project so public static UI SC script instance and again get and only private set and in the start method we're going to set the instance to this particular instance we need to keep a reference to this text here so for that to happen we actually need to be using unityengine.ui now we can create a text variable private text points text and serialize field so that it shows up in the unity editor and then we'll have methods which are going to be called from these buttons so we'll have six methods Al together the first one is for get Point button so public V get point and this is going to be calling the increment counter method in our manager script so manager script we want to get the instance of the manager script and uh increment counter then we need to have a method for the restart button so public void restart restart and we again want to call the method in the manager script so restart game then we want to have a method which is going to handle the incrementing of an achievement so public void increment and we want to call a method in our play gamees script do increment achievement and the ID of an achievement is gpgs IDs do achievement in inal achievement and we want to increment it by for example five steps we also want to have a method for unlocking an achievement again public void unlock and we want to call play game script. unlock achievement the ID is gpgs IDs do achievement standard achievement we also want to have methods here for showing the achievements UI and also the leaderboards UI public Voice show achievements this is simply going to call play games script. Show achievements UI and public void show leaderboards which is going to call play gam script. show leaderboards UI and again something's not good here oh that's because I didn't add this method in here so public static void or actually we can just copy this Show achievements and just paste it here change the name of this method to show leaderboards UI and we want to so do social. show leaderboards UI or leaderboard UI now we can go back to our UI script actually and show leaderboards UI we also need to have a method which is going to be updating this text points text so this is again going to be a public void update point points text and we're going to change the value of the points text text field or text property so points D xt. text equals manager script. instance or not instance because this is a static property so we can just call manager script counter that's to string and now we actually need to call this method in the UI script from manager script whenever we are incrementing a counter or just changing the value of a counter so UI script. instance update points text and also here UIC script instance update points text awesome now we just need to create a manager object so create empty manager and we are going to drag each and every script on here except for the one generated by the plugin the points text field is going to be this points dxt and now we need to connect buttons to their respective methods in the UI script so for example Geto button is going to call a method on the manager game object in the script UI script and the name of the method is get point now if we want to test if everything works all right we cannot do it directly from the unity editor however we can at least test the UI here so we're going to launch the game press on get point and this text should change to one and yeah it does change to one and 2 3 4 and when we press restart it should go back to zero and yes it does go back to zero to properly test our game we are going to need to build it in order to be able to communicate with Google's servers so we're going to go to build settings and build and uh we're going to build it here we're just going to call it tutorial and press on Save now we can test this APK inside an emulator and I am using an amazing emulator called nox the link is in the video description so I've already installed it and when we go to see the achievements you can see that the incremental achievement is already on 10% because I've already tested it so I've clicked two times on the increment button standard achievement is locked and I have no points on the leaderboard when I press on get point and restart I should have one point in the leaderboard and yes I do let's press five times on the get Point press restart and I have five points on the leaderboard amazing now let's press one more time on increment I should have 15% now I have 20% and when I press like crazy on the increment button it's 85% so now three more times and it should get unlocked so one two and three and achievement unlocked incremental achievement amazing so now it is unlocked and when I press on unlock it's going to unlock the stand standard achievement the non-incremental achievement so unlock and achievement unlocked standard achievement so everything works as expected if you want to get the code written in this tutorial you can do so by clicking on the link in the video description the next part is going to be about implementing saved games which is a lot of fun so stay tuned for that and subscribe if you don't want to miss it I hope that this video helped you if it did don't forget to give this video like and also share it if you want to get notified of every new video hit the Bell button and also follow me on social media and see you in the next [Music] video
Info
Channel: Reso Coder
Views: 103,744
Rating: undefined out of 5
Keywords: resocoder, tutorial, programming, code, programming tutorial, google play games, achievements, leaderboards, unity, c#, csharp, play games, gpg, gpg integration
Id: 5Ae8GeRmdH0
Channel Id: undefined
Length: 20min 58sec (1258 seconds)
Published: Fri Feb 24 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.