RewardedAds.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using UnityEngine;
  3. namespace HexaCrazy.Ads
  4. {
  5. public enum RewardedAdStatus { Preparing, Loading, Ready, NetworkError, NoFill, LoadFailed, PrivacyUnavailable }
  6. public abstract class RewardedAds : MonoBehaviour
  7. {
  8. public RewardedAdStatus Status { get; protected set; } = RewardedAdStatus.Preparing;
  9. public virtual void Retry() { }
  10. public abstract bool Supported { get; }
  11. public virtual bool NeedsPrivacyOptions => false;
  12. public virtual void ShowPrivacyOptions() { }
  13. public bool IsShowing { get; private set; }
  14. // The provider calls both delegates on Unity's main thread. A closed
  15. // impression can never award a later impression's reward.
  16. public bool TryShow(Action onEarned, Action<bool> onClosed)
  17. {
  18. if (IsShowing || !Supported) return false;
  19. IsShowing = true;
  20. bool earned = false, ended = false;
  21. void Earn()
  22. {
  23. if (earned || ended) return;
  24. earned = true;
  25. onEarned?.Invoke();
  26. }
  27. void Close()
  28. {
  29. if (ended) return;
  30. ended = true;
  31. IsShowing = false;
  32. onClosed(earned);
  33. }
  34. if (Present(Earn, Close)) return true;
  35. ended = true;
  36. IsShowing = false;
  37. return false;
  38. }
  39. protected abstract bool Present(Action earn, Action close);
  40. }
  41. }