WaterChicken

Unity3d GameJolt API Manager

Dec 21st, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class GameJoltAPIManager : MonoBehaviour {
  6.     public int gameID;
  7.     public string privateKey;
  8.     public string userName = "";
  9.     public string userToken = "";
  10.     [HideInInspector]
  11.     public bool
  12.         loggedIn = false;
  13.  
  14.     public float sessionPingTime = 15f;
  15.     private float sessionTimer = 15f;
  16.  
  17.     public ArrayList achievedTrophies = new ArrayList();
  18.          
  19.     public static GameJoltAPIManager I { get; private set; }
  20.  
  21.     void Awake() {
  22.         if (I != null && I != this) {
  23.             DestroyImmediate(gameObject);
  24.         }
  25.         I = this;
  26.         DontDestroyOnLoad(gameObject);
  27.     }
  28.  
  29.     void Start() {
  30.         GJAPI.Init(gameID, privateKey);
  31.         GJAPI.Users.VerifyCallback += OnVerifyUser;
  32.         GJAPI.Sessions.CloseCallback += OnSessionClose;
  33.         GJAPI.Trophies.GetAllCallback += OnTrophyGetAll;
  34.         if (userName == "" && userToken == "") {
  35.             GJAPIHelper.Users.ShowLogin();
  36.         } else {
  37.             GJAPI.Users.Verify(userName, userToken);
  38.         }
  39.     }
  40.  
  41.     void OnVerifyUser( bool success ) {
  42.         if (success) {
  43.             Debug.Log("Successfully logged in.");
  44.             GJAPI.Trophies.GetAll(true);
  45.             GJAPI.Sessions.Open();
  46.             loggedIn = true;
  47.         } else {
  48.             Debug.Log("Failed to log in.");
  49.         }
  50.     }
  51.  
  52.     void OnSessionClose( bool success ) {
  53.         Application.Quit();
  54.     }
  55.  
  56.     void Update() {
  57.         if (loggedIn) {
  58.             sessionTimer -= Time.deltaTime;
  59.  
  60.             if (sessionTimer <= 0) {
  61.                 GJAPI.Sessions.Ping();
  62.                 sessionTimer = sessionPingTime;
  63.             }
  64.         }
  65.     }
  66.  
  67.     void OnTrophyGetAll( GJTrophy[] trophies ) {
  68.         for (int i = 0; i < trophies.Length; i++) {
  69.             achievedTrophies.Add(trophies[i].Id);
  70.         }
  71.         AddTrophy(8220);
  72.     }
  73.  
  74.     public void AddTrophy( uint Id ) {
  75.         GJAPI.Trophies.Add(Id);
  76.         if (!achievedTrophies.Contains(Id)) {
  77.             GJAPIHelper.Trophies.ShowTrophyUnlockNotification(Id);
  78.             achievedTrophies.Add(Id);
  79.         }
  80.     }
  81.  
  82.     void OnApplicationQuit() {
  83.         if (loggedIn) {
  84.             GJAPI.Sessions.Close();
  85.             Application.CancelQuit();
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment