Monday, April 20, 2015

How to set up google play services with unity plugin for android



below notes are taken directly from https://github.com/playgameservices/play-games-plugin-for-unity  webpage. here I copied that article,  only making a quick access for myself. Also I wrote some extra notes. For full page please goto https://github.com/playgameservices/play-games-plugin-for-unity website.

how to google play services with unity plugin:

First of all, build your game and upload it to google play services.



1) goto https://github.com/playgameservices/play-games-plugin-for-unity and clone git repository, or download and unzip the zip file.
2) find latest unity package, it is under the current_build directory, its name is something like "current-build/GooglePlayGamesPluginForUnity-X.YY.ZZ.unitypackage"
3) open the Android SDK manager and verify that you have downloaded the Google Play Services package. The Android SDK manager is usually available in your SDK installation directory, under the "tools" subdirectory, and is called android (or android.exe on Windows). The Google Play Services package is available under the Extras folder. If it is not installed or is out of date, install or update it before proceeding.
4) set up the path to your Android SDK installation in Unity. This is located in the preferences menu, under the External Tools section.
5) configure your game's package name. To do this, click File | Build Settings, select the Android platform and click Player Settings to show Unity's Player Settings window. In that window, look for the Bundle Identifier setting under Other Settings. Enter your package name there (for example com.example.my.awesome.game).
6) click the Window |Google Play Games|Setup - Android setup menu item. This will display the Android setup screen, where you must input your Application ID (e.g. 12345678012). You will get your application id from google services page. click the Setup button.

Important: The application ID and package name settings must match exactly the values you used when setting up your project on the Developer Console.


=======================


How to manage with your code:
-------------------------------
The Google Play Games plugin implements a subset of Unity's social interface. The standard API calls can be accessed through the Social.Active object, which is a reference to an ISocialPlatform interface. The non-standard Google Play Games extensions can be accessed by casting the Social.Active object to the PlayGamesPlatform class, where the additional methods are available.



Sign in:
------------------------------------------
To sign in, call Social.localUser.Authenticate, which is part of the standard Unity social platform interface.

    using GooglePlayGames;
    using UnityEngine.SocialPlatforms;
    ...
    // authenticate user:
    Social.localUser.Authenticate((bool success) => {
        // handle success or failure
    });
Authentication will show the required consent dialogs. If the user has already signed into the game in the past, this process will be silent and the user will not have to interact with any dialogs.

Note that you cannot make any games API calls (unlock achievements, post scores, etc) until you get a successful return value from Authenticate, so it is good practice to put up a standby screen until the callback is called, to make sure the user can't start playing the game until the authentication process completes.

WARNING: dont forget to set googleplayservices ad default social platform!!!

void Awake () {
// Select the Google Play Games platform as our social platform implementation
GooglePlayGames.PlayGamesPlatform.Activate();
}
how to check if an user is logged in or not?
public bool isLoggedInGooglePlayServices() {
bool sonuc = false;
if(Social.localUser.authenticated) {
sonuc = true;
}
return sonuc;
}


Sign out
------------------------------------------
To sign the user out, use the PlayGamesPlatform.SignOut method.

    using GooglePlayGames;
    using UnityEngine.SocialPlatforms;

    // sign out
    PlayGamesPlatform.Instance.SignOut();
After signing out, no further API calls can be made until the user authenticates again.




Revealing/Unlocking an Achievement
------------------------------------------
To unlock an achievement, use the Social.ReportProgress method with a progress value of 100.0f:

    using GooglePlayGames;
    using UnityEngine.SocialPlatforms;
    ...
    // unlock achievement (achievement ID "Cfjewijawiu_QA")
    Social.ReportProgress("Cfjewijawiu_QA", 100.0f, (bool success) => {
      // handle success or failure
    });
Notice that according to the expected behavior of Social.ReportProgress, a progress of 0.0f means revealing the achievement and a progress of 100.0f means unlocking the achievement. Therefore, to reveal an achievement (that was previously hidden) without unlocking it, simply call Social.ReportProgress with a progress of 0.0f.


If your achievement is incremental, the Play Games implementation of Social.ReportProgress will try to behave as closely as possible to the expected behavior according to Unity's social API, but may not be exact. For this reason, we recommend that you do not use Social.ReportProgress for incremental achievements. Instead, use the PlayGamesPlatform.IncrementAchievement method, which is a Play Games extension.

    using GooglePlayGames;
    using UnityEngine.SocialPlatforms;
    ...
    // increment achievement (achievement ID "Cfjewijawiu_QA") by 5 steps
    PlayGamesPlatform.Instance.IncrementAchievement(
        "Cfjewijawiu_QA", 5, (bool success) => {
            // handle success or failure
    });




Showing the Achievements UI
------------------------------------------
To show the built-in UI for all achievements, call Social.ShowAchievementsUI.

    using GooglePlayGames;
    using UnityEngine.SocialPlatforms;
    ...
    // show achievements UI
    Social.ShowAchievementsUI();
This will show a standard UI appropriate for the look and feel of the platform (Android or iOS).




Posting a Score to a Leaderboard
------------------------------------------
To post a score to a leaderboard, call Social.ReportScore.

    using GooglePlayGames;
    using UnityEngine.SocialPlatforms;
    ...
    // post score 12345 to leaderboard ID "Cfji293fjsie_QA")
    Social.ReportScore(12345, "Cfji293fjsie_QA", (bool success) => {
        // handle success or failure
    });
Note that the platform and the server will automatically discard scores that are lower than the player's existing high score, so you can submit scores freely without any checks to test whether or not the score is greater than the player's existing score.




To show the built-in UI for all leaderboards, call Social.ShowLeaderboardUI.
------------------------------------------
    using GooglePlayGames;
    using UnityEngine.SocialPlatforms;
    ...
    // show leaderboard UI
    Social.ShowLeaderboardUI();
If you wish to show a particular leaderboard instead of all leaderboards, you can pass a leaderboard ID to the method. This, however, is a Play Games extension, so the Social.Active object needs to be cast to a PlayGamesPlatform object first:

    using GooglePlayGames;
    using UnityEngine.SocialPlatforms;
    ...
    // show leaderboard UI
    PlayGamesPlatform.Instance.ShowLeaderboardUI("Cfji293fjsie_QA");



There are more data about cloud saving, reading from cloud or multiplayer issues on https://github.com/playgameservices/play-games-plugin-for-unity page. Since I dont need them yet, I did not gather data for them here.