| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #if UNITY_IOS && !UNITY_EDITOR
- using System;
- using System.Runtime.InteropServices;
- using UnityEngine;
- namespace Fateforge
- {
- sealed class IosRewardedAds : IRewardedAdService, IDisposable
- {
- enum AdEvent { Earned = 1, Dismissed, PresentationFailed }
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- delegate void AdCallback(IntPtr context, AdEvent outcome);
- [DllImport("__Internal")] static extern IntPtr FFAdMobCreate(string unitId, int useTestUnit, IntPtr context, AdCallback callback);
- [DllImport("__Internal")] static extern string FFAdMobLastError(IntPtr service);
- [DllImport("__Internal")] static extern void FFAdMobPoll(IntPtr service);
- [DllImport("__Internal")] static extern int FFAdMobReady(IntPtr service);
- [DllImport("__Internal")] static extern int FFAdMobShow(IntPtr service);
- [DllImport("__Internal")] static extern int FFAdMobPrivacyOptionsRequired(IntPtr service);
- [DllImport("__Internal")] static extern void FFAdMobShowPrivacyOptions(IntPtr service);
- [DllImport("__Internal")] static extern void FFAdMobDispose(IntPtr service);
- static readonly AdCallback Callback = Receive;
- GCHandle context;
- IntPtr service;
- AdRequest pending;
- public event Action<AdRequest, AdResult> Result;
- public bool Available => service != IntPtr.Zero && pending == null && FFAdMobReady(service) != 0;
- public bool PrivacyOptionsRequired => service != IntPtr.Zero && FFAdMobPrivacyOptionsRequired(service) != 0;
- public string LastError => service == IntPtr.Zero ? "Missing rewarded ad configuration." : FFAdMobLastError(service);
- public IosRewardedAds(MobileConfig config, bool useTestUnit = false)
- {
- if (!useTestUnit && string.IsNullOrWhiteSpace(config.adMobIosRewardedAdUnitId))
- {
- Debug.LogError("[Fateforge Ads] Missing AdMob rewarded ad unit ID.");
- return;
- }
- context = GCHandle.Alloc(this);
- service = FFAdMobCreate(config.adMobIosRewardedAdUnitId?.Trim(), useTestUnit ? 1 : 0, GCHandle.ToIntPtr(context), Callback);
- }
- public void Poll()
- {
- if (service != IntPtr.Zero) FFAdMobPoll(service);
- }
- public void ShowPrivacyOptions() => FFAdMobShowPrivacyOptions(service);
- public bool Show(AdRequest request)
- {
- if (request == null || !Available) return false;
- pending = request;
- if (FFAdMobShow(service) != 0) return true;
- pending = null;
- return false;
- }
- [AOT.MonoPInvokeCallback(typeof(AdCallback))]
- static void Receive(IntPtr context, AdEvent outcome)
- {
- var ads = (IosRewardedAds)GCHandle.FromIntPtr(context).Target;
- var request = ads.pending;
- if (request == null) return;
- if (outcome != AdEvent.Earned) ads.pending = null;
- var result = outcome == AdEvent.Earned ? AdResult.Rewarded :
- outcome == AdEvent.Dismissed ? AdResult.Closed : AdResult.Failed;
- ads.Result?.Invoke(request, result);
- }
- public void Dispose()
- {
- if (service != IntPtr.Zero) FFAdMobDispose(service);
- service = IntPtr.Zero;
- pending = null;
- if (context.IsAllocated) context.Free();
- }
- }
- }
- #endif
|