using System.Collections; using System.Collections.Generic; using UnityEngine; using Apple.Core.Runtime; #if UNITY_IOS using Apple.GameKit; #endif public class GameCenterManager : MonoBehaviour { public static GameCenterManager Instance { get; private set; } public bool isAuthenticated = false; private GKLocalPlayer.AuthenticateHandler authenticateHandler; //private GKLocalPlayer.AuthenticateErrorHandler authenticateErrorHandler; private void Awake() { #if UNITY_IOS if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } #endif } #if UNITY_IOS private async void Start() { if (Instance == this) InitializeGameCenterManager(); } #endif private void OnEnable() { #if UNITY_IOS authenticateHandler += AuthenticationSuccess; GKLocalPlayer.AuthenticateError += AuthenticationError; #endif } private void OnDisable() { #if UNITY_IOS authenticateHandler -= AuthenticationSuccess; GKLocalPlayer.AuthenticateError -= AuthenticationError; #endif } #if UNITY_IOS private async void InitializeGameCenterManager() { try { var player = await GKLocalPlayer.Authenticate(); } catch(GameKitException exception) { Debug.LogError("EXCEPTIONI: " + exception); Debug.LogError("INNER: " + exception.InnerException); } } #endif #if UNITY_IOS private void AuthenticationSuccess(GKLocalPlayer localPlayer) { isAuthenticated = localPlayer.IsAuthenticated; if (isAuthenticated) { Debug.Log("AUTHENTICATION PASSED!!"); } else { Debug.Log("AUTHENTICATION FAILED!!!"); } } #endif #if UNITY_IOS private void AuthenticationError(NSError error) { isAuthenticated = false; Debug.Log("AUTHENTICATION FAILED Error: " + error.Code); } #endif }