Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class GameJoltAPIManager : MonoBehaviour {
- public int gameID;
- public string privateKey;
- public string userName = "";
- public string userToken = "";
- [HideInInspector]
- public bool
- loggedIn = false;
- public float sessionPingTime = 15f;
- private float sessionTimer = 15f;
- public ArrayList achievedTrophies = new ArrayList();
- public static GameJoltAPIManager I { get; private set; }
- void Awake() {
- if (I != null && I != this) {
- DestroyImmediate(gameObject);
- }
- I = this;
- DontDestroyOnLoad(gameObject);
- }
- void Start() {
- GJAPI.Init(gameID, privateKey);
- GJAPI.Users.VerifyCallback += OnVerifyUser;
- GJAPI.Sessions.CloseCallback += OnSessionClose;
- GJAPI.Trophies.GetAllCallback += OnTrophyGetAll;
- if (userName == "" && userToken == "") {
- GJAPIHelper.Users.ShowLogin();
- } else {
- GJAPI.Users.Verify(userName, userToken);
- }
- }
- void OnVerifyUser( bool success ) {
- if (success) {
- Debug.Log("Successfully logged in.");
- GJAPI.Trophies.GetAll(true);
- GJAPI.Sessions.Open();
- loggedIn = true;
- } else {
- Debug.Log("Failed to log in.");
- }
- }
- void OnSessionClose( bool success ) {
- Application.Quit();
- }
- void Update() {
- if (loggedIn) {
- sessionTimer -= Time.deltaTime;
- if (sessionTimer <= 0) {
- GJAPI.Sessions.Ping();
- sessionTimer = sessionPingTime;
- }
- }
- }
- void OnTrophyGetAll( GJTrophy[] trophies ) {
- for (int i = 0; i < trophies.Length; i++) {
- achievedTrophies.Add(trophies[i].Id);
- }
- AddTrophy(8220);
- }
- public void AddTrophy( uint Id ) {
- GJAPI.Trophies.Add(Id);
- if (!achievedTrophies.Contains(Id)) {
- GJAPIHelper.Trophies.ShowTrophyUnlockNotification(Id);
- achievedTrophies.Add(Id);
- }
- }
- void OnApplicationQuit() {
- if (loggedIn) {
- GJAPI.Sessions.Close();
- Application.CancelQuit();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment