Skip to content

Instantly share code, notes, and snippets.

@yasuakiohama
Last active December 19, 2015 06:39
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 yasuakiohama/5d8aac51f68d47221e19 to your computer and use it in GitHub Desktop.
Save yasuakiohama/5d8aac51f68d47221e19 to your computer and use it in GitHub Desktop.
GooglePlayGamesPlatformSample.cs
//リーダーボードとアチーブメントボタンを追加
/*
* Copyright (C) 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using UnityEngine;
using System.Collections;
using UnityEngine.SocialPlatforms;
#if UNITY_ANDROID
using GooglePlayGames;
#elif UNITY_IOS
using System.Runtime.InteropServices;
#endif
public class GooglePlayGamesPlatformSample : MonoBehaviour {
#if UNITY_IOS
[DllImport("__Internal")]
private static extern void _ReportAchievement( string achievementID, float progress );
#endif
private const float FontSizeMult = 0.05f;
private bool mWaitingForAuth = false;
private string mStatusText = "Ready.";
void Start () {
#if UNITY_ANDROID
// Select the Google Play Games platform as our social platform implementation
PlayGamesPlatform.Activate();
#endif
}
void OnGUI() {
GUI.skin.button.fontSize = (int)(FontSizeMult * Screen.height);
GUI.skin.label.fontSize = (int)(FontSizeMult * Screen.height);
GUI.Label(new Rect(20, 20, Screen.width, Screen.height * 0.25f),
mStatusText);
Rect buttonRect = new Rect(0.25f * Screen.width, 0.10f * Screen.height,
0.5f * Screen.width, 0.25f * Screen.height);
Rect imageRect = new Rect(buttonRect.x+buttonRect.width/4f,
buttonRect.y + buttonRect.height * 1.1f,
buttonRect.width/2f, buttonRect.width/2f);
if (mWaitingForAuth) {
return;
}
string buttonLabel;
if (Social.localUser.authenticated) {
buttonLabel = "Sign Out";
if (Social.localUser.image != null) {
GUI.DrawTexture(imageRect, Social.localUser.image,
ScaleMode.ScaleToFit);
} else {
GUI.Label(imageRect, "No image available");
}
Show ();
Post ();
mStatusText = "Ready";
} else {
buttonLabel = "Authenticate";
}
if (GUI.Button(buttonRect, buttonLabel)) {
if (!Social.localUser.authenticated) {
// Authenticate
mWaitingForAuth = true;
mStatusText = "Authenticating...";
Social.localUser.Authenticate((bool success) => {
mWaitingForAuth = false;
if (success) {
mStatusText = "Welcome " + Social.localUser.userName;
#if UNITY_ANDROID
string token = PlayGamesPlatform.Instance.GetToken();
Debug.Log(token);
#endif
} else {
mStatusText = "Authentication failed.";
}
});
} else {
#if UNITY_ANDROID
// Sign out!
mStatusText = "Signing out.";
((PlayGamesPlatform) Social.Active).SignOut();
#endif
}
}
}
void Show() {
if (GUI.Button (new Rect(0.05f * Screen.width, 0.40f * Screen.height,0.35f * Screen.width, 0.15f * Screen.height), "ShowAchievementsUI")) {
Social.ShowAchievementsUI ();
}
if (GUI.Button (new Rect(0.60f * Screen.width, 0.40f * Screen.height,0.35f * Screen.width, 0.15f * Screen.height), "ShowLeaderboardUI")) {
Social.ShowLeaderboardUI ();
}
}
void Post() {
if (GUI.Button (new Rect(0.05f * Screen.width, 0.60f * Screen.height,0.35f * Screen.width, 0.15f * Screen.height), "PostAchievements")) {
//100.0fを入れることで実績解除になります。
#if UNITY_ANDROID
Social.ReportProgress (GPGSIds.achievement_clear_1w, 100.0f, (bool success) => {});
#elif UNITY_IOS
_ReportAchievement(GPGSIds.achievement_name, 100.0f);
#endif
}
if (GUI.Button (new Rect(0.60f * Screen.width, 0.60f * Screen.height,0.35f * Screen.width, 0.15f * Screen.height), "PostLeaderboard")) {
Social.ReportScore (123456789, GPGSIds.leaderboard_name, (bool success) => {});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment