| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #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<string, string, byte[]> onProfile;
- [Preserve]
- public static void ObserveProfile(Action<string, string, byte[]> 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<string> onSignedIn;
- static Action<string> onLoaded;
- static int latestRequest;
- [Preserve]
- public static void Sync(string owner, int bestNight, int totalScore, Action<string> 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<string> 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
|