IosRewardedAds.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #if UNITY_IOS && !UNITY_EDITOR
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. namespace Fateforge
  6. {
  7. sealed class IosRewardedAds : IRewardedAdService, IDisposable
  8. {
  9. enum AdEvent { Earned = 1, Dismissed, PresentationFailed }
  10. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  11. delegate void AdCallback(IntPtr context, AdEvent outcome);
  12. [DllImport("__Internal")] static extern IntPtr FFAdMobCreate(string unitId, int useTestUnit, IntPtr context, AdCallback callback);
  13. [DllImport("__Internal")] static extern string FFAdMobLastError(IntPtr service);
  14. [DllImport("__Internal")] static extern void FFAdMobPoll(IntPtr service);
  15. [DllImport("__Internal")] static extern int FFAdMobReady(IntPtr service);
  16. [DllImport("__Internal")] static extern int FFAdMobShow(IntPtr service);
  17. [DllImport("__Internal")] static extern int FFAdMobPrivacyOptionsRequired(IntPtr service);
  18. [DllImport("__Internal")] static extern void FFAdMobShowPrivacyOptions(IntPtr service);
  19. [DllImport("__Internal")] static extern void FFAdMobDispose(IntPtr service);
  20. static readonly AdCallback Callback = Receive;
  21. GCHandle context;
  22. IntPtr service;
  23. AdRequest pending;
  24. public event Action<AdRequest, AdResult> Result;
  25. public bool Available => service != IntPtr.Zero && pending == null && FFAdMobReady(service) != 0;
  26. public bool PrivacyOptionsRequired => service != IntPtr.Zero && FFAdMobPrivacyOptionsRequired(service) != 0;
  27. public string LastError => service == IntPtr.Zero ? "Missing rewarded ad configuration." : FFAdMobLastError(service);
  28. public IosRewardedAds(MobileConfig config, bool useTestUnit = false)
  29. {
  30. if (!useTestUnit && string.IsNullOrWhiteSpace(config.adMobIosRewardedAdUnitId))
  31. {
  32. Debug.LogError("[Fateforge Ads] Missing AdMob rewarded ad unit ID.");
  33. return;
  34. }
  35. context = GCHandle.Alloc(this);
  36. service = FFAdMobCreate(config.adMobIosRewardedAdUnitId?.Trim(), useTestUnit ? 1 : 0, GCHandle.ToIntPtr(context), Callback);
  37. }
  38. public void Poll()
  39. {
  40. if (service != IntPtr.Zero) FFAdMobPoll(service);
  41. }
  42. public void ShowPrivacyOptions() => FFAdMobShowPrivacyOptions(service);
  43. public bool Show(AdRequest request)
  44. {
  45. if (request == null || !Available) return false;
  46. pending = request;
  47. if (FFAdMobShow(service) != 0) return true;
  48. pending = null;
  49. return false;
  50. }
  51. [AOT.MonoPInvokeCallback(typeof(AdCallback))]
  52. static void Receive(IntPtr context, AdEvent outcome)
  53. {
  54. var ads = (IosRewardedAds)GCHandle.FromIntPtr(context).Target;
  55. var request = ads.pending;
  56. if (request == null) return;
  57. if (outcome != AdEvent.Earned) ads.pending = null;
  58. var result = outcome == AdEvent.Earned ? AdResult.Rewarded :
  59. outcome == AdEvent.Dismissed ? AdResult.Closed : AdResult.Failed;
  60. ads.Result?.Invoke(request, result);
  61. }
  62. public void Dispose()
  63. {
  64. if (service != IntPtr.Zero) FFAdMobDispose(service);
  65. service = IntPtr.Zero;
  66. pending = null;
  67. if (context.IsAllocated) context.Free();
  68. }
  69. }
  70. }
  71. #endif