#if UNITY_IOS && !UNITY_EDITOR using System; using System.Runtime.InteropServices; using UnityEngine.Scripting; namespace TowerDevil { public static class GameCenterNative { [UnmanagedFunctionPointer(CallingConvention.Cdecl)] delegate void PlayerCallback([MarshalAs(UnmanagedType.LPStr)] string playerID); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] delegate void ResultCallback(int request, [MarshalAs(UnmanagedType.LPStr)] string json); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] delegate void ProfileCallback([MarshalAs(UnmanagedType.LPUTF8Str)] string playerID, [MarshalAs(UnmanagedType.LPUTF8Str)] string displayName, IntPtr png, int length); [DllImport("__Internal")] static extern void TDGameCenterObserveProfile(ProfileCallback callback); static readonly ProfileCallback receiveProfile = ReceiveProfile; static Action onProfile; [Preserve] public static void ObserveProfile(Action changed) { onProfile = changed; TDGameCenterObserveProfile(changed == null ? null : receiveProfile); } [AOT.MonoPInvokeCallback(typeof(ProfileCallback))] static void ReceiveProfile(string playerID, string displayName, IntPtr png, int length) { if (onProfile == null) return; byte[] photo = length < 0 ? null : new byte[length]; if (length > 0) Marshal.Copy(png, photo, 0, length); onProfile(playerID, displayName, photo); } [DllImport("__Internal")] static extern void TDGameCenterSync(string owner, int bestNight, int totalScore, PlayerCallback player); [DllImport("__Internal")] static extern void TDGameCenterLoad(int board, int request, ResultCallback completed); static readonly PlayerCallback signedIn = SignedIn; static readonly ResultCallback receive = Receive; static Action onSignedIn; static Action onLoaded; static int latestRequest; [Preserve] public static void Sync(string owner, int bestNight, int totalScore, Action playerSignedIn) { onSignedIn = playerSignedIn; TDGameCenterSync(owner, bestNight, totalScore, signedIn); } [AOT.MonoPInvokeCallback(typeof(PlayerCallback))] static void SignedIn(string playerID) { onSignedIn?.Invoke(playerID); } [Preserve] public static void Load(int board, Action completed) { onLoaded = completed; TDGameCenterLoad(board, ++latestRequest, receive); } [AOT.MonoPInvokeCallback(typeof(ResultCallback))] static void Receive(int request, string json) { if (request != latestRequest) return; var completed = onLoaded; onLoaded = null; completed?.Invoke(json); } } } #endif