Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wangpeng1/c3f25e4ea72940dad1ce89277dee662f to your computer and use it in GitHub Desktop.
Save wangpeng1/c3f25e4ea72940dad1ce89277dee662f to your computer and use it in GitHub Desktop.
プラグインを書かずにGoogle Play Game Servicesのランキングとアチーブメントを実装【Unity】【Android】【アセット】
// AndroidRankingUtility.cs
// http://kan-kikuchi.hatenablog.com/entry/AndroidRankingUtility
//
// Created by kan.kikuchi on 2016.04.01.
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Androidのランキング用便利クラス
/// </summary>
public static class AndroidRankingUtility {
//コールバック
private static Action<bool> _reportScoreCallBack;
private static Action<bool> _reportProgressCallBack;
//=================================================================================
//初期化
//=================================================================================
/// <summary>
/// ユーザー認証
/// </summary>
public static void Auth(Action<bool> callBack = null){
//コールバックが設定されていない場合はログを設定
if(callBack == null){
callBack = (success) => {
Debug.Log(success ? "認証成功" : "認証失敗");
};
}
//コールバックを登録して、接続
GooglePlayManager.ActionScoreSubmited += ReportScoreCallBack;
GooglePlayManager.ActionAchievementUpdated += ReportProgressCallBack;
GooglePlayConnection.ActionConnectionResultReceived += (result) =>{
callBack(result.IsSuccess);
};
GooglePlayConnection.Instance.Connect ();;
}
//=================================================================================
//ランキング
//=================================================================================
/// <summary>
/// リーダーボードを表示する
/// </summary>
public static void ShowLeaderboardUI(){
GooglePlayManager.Instance.ShowLeaderBoardsUI ();
}
/// <summary>
/// リーダーボードにスコアを送信する
/// </summary>
public static void ReportScore (string leaderboardID, long score, Action<bool> callBack = null){
//コールバックが設定されていない場合はログを設定
if(callBack == null){
callBack = (success) => {
Debug.Log(success ? "スコア送信成功" : "スコア送信失敗");
};
}
_reportScoreCallBack = callBack;
GooglePlayManager.Instance.SubmitScoreById (leaderboardID, score);
}
//スコア送信後のコールバック
private static void ReportScoreCallBack(GP_LeaderboardResult result){
_reportScoreCallBack(result.IsSucceeded);
}
//=================================================================================
//実績
//=================================================================================
/// <summary>
/// 実績一覧を表示する
/// </summary>
public static void ShowAchievementsUI(){
GooglePlayManager.Instance.ShowAchievementsUI ();
}
/// <summary>
/// 実績の進捗状況を送信する
/// </summary>
public static void ReportProgress(string achievementKey, Action<bool> callBack = null){
//コールバックが設定されていない場合はログを設定
if(callBack == null){
callBack = (success) => {
Debug.Log(success ? "進捗送信成功" : "進捗送信失敗");
};
}
_reportProgressCallBack = callBack;
GooglePlayManager.Instance.UnlockAchievementById(achievementKey);
}
//実績送信後のコールバック
private static void ReportProgressCallBack(GP_AchievementResult result){
_reportProgressCallBack(result.IsSucceeded);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment