Skip to content

Instantly share code, notes, and snippets.

@nipundavid
Last active April 13, 2016 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nipundavid/7d56884a8dbc258553a8e804cc9c0dc2 to your computer and use it in GitHub Desktop.
Save nipundavid/7d56884a8dbc258553a8e804cc9c0dc2 to your computer and use it in GitHub Desktop.
Google Play Service SignIn script in Unity3d using Google Play services Unity plugin
/*
* GooglePlayServices.cs (c) by Nipun David
*
* GooglePlayServices.cs is licensed under a
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* You should have received a copy of the license along with this
* work. If not, see <http://creativecommons.org/licenses/by-sa/4.0/>.
*/
using UnityEngine;
using System.Collections;
using GooglePlayGames;
using UnityEngine.SocialPlatforms;
public class GooglePlayServices : MonoBehaviour {
// Use this for initialization
void Start () {
// Code to initialize this object would go here
// for debug verbose
PlayGamesPlatform.DebugLogEnabled = true;
// initialize the google play services
PlayGamesPlatform.Activate ();
TrySilentSignIn ();
}
// Update is called once per frame
void Update () {
}
public void TrySilentSignIn() {
if (! PlayGamesPlatform.Instance.localUser.authenticated) {
PlayGamesPlatform.Instance.Authenticate ((bool success) => {
if (success) {
Debug.Log ("GPS - GPS - Silently signed in! Welcome " + PlayGamesPlatform.Instance.localUser.userName);
} else {
Debug.Log ("GPS - Oh... we're not signed in.");
}
}, true);
} else {
Debug.Log("GPS - We're already signed in");
}
}
public void SignInAndStart() {
if (!IsAuthenticated()) {
PlayGamesPlatform.Instance.localUser.Authenticate((bool success) => {
if (success) {
Debug.Log ("GPS - We're signed in! Welcome " + PlayGamesPlatform.Instance.localUser.userName);
// We could start our game now
} else {
Debug.Log ("GPS - Oh... we're not signed in.");
}
});
} else {
Debug.Log ("GPS - You're already signed in.");
// We could also start our game now
// Actually Multiplayer game begins
}
}
public bool IsAuthenticated() {
return PlayGamesPlatform.Instance.localUser.authenticated;
}
public void SignOut() {
PlayGamesPlatform.Instance.SignOut ();
}
public void pushScore(int score) {
// post score 12345 to leaderboard ID "Cfji293fjsie_QA")
if (IsAuthenticated ()) {
Social.ReportScore (score, GameIDs.leaderboard_score, (bool success) => {
// handle success or failure
if (success) {
Debug.Log ("Score Pushed");
} else {
Debug.Log ("Score Not Pushed");
}
});
}
}
public void showLeaderboard() {
if (IsAuthenticated()) {
Social.ShowLeaderboardUI ();
} else {
SignInAndStart ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment