GameCenterNative.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #if UNITY_IOS && !UNITY_EDITOR
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine.Scripting;
  5. namespace TowerDevil
  6. {
  7. public static class GameCenterNative
  8. {
  9. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  10. delegate void PlayerCallback([MarshalAs(UnmanagedType.LPStr)] string playerID);
  11. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  12. delegate void ResultCallback(int request, [MarshalAs(UnmanagedType.LPStr)] string json);
  13. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  14. delegate void ProfileCallback([MarshalAs(UnmanagedType.LPUTF8Str)] string playerID,
  15. [MarshalAs(UnmanagedType.LPUTF8Str)] string displayName, IntPtr png, int length);
  16. [DllImport("__Internal")]
  17. static extern void TDGameCenterObserveProfile(ProfileCallback callback);
  18. static readonly ProfileCallback receiveProfile = ReceiveProfile;
  19. static Action<string, string, byte[]> onProfile;
  20. [Preserve]
  21. public static void ObserveProfile(Action<string, string, byte[]> changed)
  22. {
  23. onProfile = changed;
  24. TDGameCenterObserveProfile(changed == null ? null : receiveProfile);
  25. }
  26. [AOT.MonoPInvokeCallback(typeof(ProfileCallback))]
  27. static void ReceiveProfile(string playerID, string displayName, IntPtr png, int length)
  28. {
  29. if (onProfile == null) return;
  30. byte[] photo = length < 0 ? null : new byte[length];
  31. if (length > 0) Marshal.Copy(png, photo, 0, length);
  32. onProfile(playerID, displayName, photo);
  33. }
  34. [DllImport("__Internal")]
  35. static extern void TDGameCenterSync(string owner, int bestNight, int totalScore, PlayerCallback player);
  36. [DllImport("__Internal")]
  37. static extern void TDGameCenterLoad(int board, int request, ResultCallback completed);
  38. static readonly PlayerCallback signedIn = SignedIn;
  39. static readonly ResultCallback receive = Receive;
  40. static Action<string> onSignedIn;
  41. static Action<string> onLoaded;
  42. static int latestRequest;
  43. [Preserve]
  44. public static void Sync(string owner, int bestNight, int totalScore, Action<string> playerSignedIn)
  45. {
  46. onSignedIn = playerSignedIn;
  47. TDGameCenterSync(owner, bestNight, totalScore, signedIn);
  48. }
  49. [AOT.MonoPInvokeCallback(typeof(PlayerCallback))]
  50. static void SignedIn(string playerID)
  51. {
  52. onSignedIn?.Invoke(playerID);
  53. }
  54. [Preserve]
  55. public static void Load(int board, Action<string> completed)
  56. {
  57. onLoaded = completed;
  58. TDGameCenterLoad(board, ++latestRequest, receive);
  59. }
  60. [AOT.MonoPInvokeCallback(typeof(ResultCallback))]
  61. static void Receive(int request, string json)
  62. {
  63. if (request != latestRequest) return;
  64. var completed = onLoaded;
  65. onLoaded = null;
  66. completed?.Invoke(json);
  67. }
  68. }
  69. }
  70. #endif