PlacementContent.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UnityEngine.Monetization
  4. {
  5. public struct CustomEvent
  6. {
  7. public string category;
  8. public string type;
  9. public IDictionary<string, object> data;
  10. public CustomEvent(string type, IDictionary<string, object> data = null) : this(null, type, data)
  11. {
  12. }
  13. public CustomEvent(string category, string type, IDictionary<string, object> data)
  14. {
  15. this.category = category;
  16. this.type = type;
  17. this.data = data;
  18. }
  19. internal IDictionary<string, object> dictionaryValue
  20. {
  21. get => new Dictionary<string, object>
  22. {
  23. {"category", category},
  24. {"type", type},
  25. {"data", data}
  26. };
  27. }
  28. }
  29. public class PlacementContent
  30. {
  31. public PlacementContent(string placementId, IPlacementContentOperations operations)
  32. {
  33. this.placementId = placementId;
  34. this.placementContentOperations = operations;
  35. }
  36. public string placementId { get; set; }
  37. public IDictionary<string, object> extras { get; internal set; }
  38. internal IPlacementContentOperations placementContentOperations { get; set; }
  39. public bool ready => placementContentOperations.ready;
  40. public PlacementContentState state => placementContentOperations.state;
  41. public virtual void SendCustomEvent(CustomEvent customEvent)
  42. {
  43. placementContentOperations.SendCustomEvent(customEvent);
  44. }
  45. }
  46. public class RewardablePlacementContent : PlacementContent
  47. {
  48. public RewardablePlacementContent(string placementId, IRewardedOperations operations) : base(placementId, operations)
  49. {
  50. rewardedOperations = operations;
  51. }
  52. private IRewardedOperations rewardedOperations { get; }
  53. public bool rewarded => rewardedOperations.IsRewarded();
  54. public string rewardId => rewardedOperations.rewardId;
  55. }
  56. public class ShowAdYield : CustomYieldInstruction
  57. {
  58. public ShowResult result { get; internal set; }
  59. internal bool showing { get; set; }
  60. public override bool keepWaiting => showing;
  61. }
  62. public class ShowAdPlacementContent : RewardablePlacementContent
  63. {
  64. public ShowAdPlacementContent(string placementId, IShowAdOperations operations) : base(placementId, operations)
  65. {
  66. this.showAdOperations = operations;
  67. }
  68. public string gamerSid { get; set; }
  69. public bool showing { get; private set; }
  70. private IShowAdOperations showAdOperations { get; }
  71. private ShowAdYield adYield;
  72. public ShowAdYield Show(ShowAdCallbacks? callbacks = null)
  73. {
  74. if (!string.IsNullOrEmpty(gamerSid))
  75. {
  76. var player = new MetaData("player");
  77. player.Set("server_id", gamerSid);
  78. #pragma warning disable 0618
  79. Monetization.SetMetaData(player);
  80. #pragma warning restore 0618
  81. }
  82. adYield = new ShowAdYield();
  83. adYield.showing = true;
  84. var adCallbacks = new ShowAdCallbacks
  85. {
  86. finishCallback = finishState =>
  87. {
  88. adYield.result = finishState;
  89. adYield.showing = showing = false;
  90. callbacks?.finishCallback?.Invoke(finishState);
  91. },
  92. startCallback = () =>
  93. {
  94. showing = true;
  95. callbacks?.startCallback?.Invoke();
  96. }
  97. };
  98. showAdOperations.Show(adCallbacks);
  99. return adYield;
  100. }
  101. public ShowAdYield Show(ShowAdFinishCallback finishCallback)
  102. {
  103. return Show(new ShowAdCallbacks
  104. {
  105. finishCallback = finishCallback
  106. });
  107. }
  108. }
  109. public class PromoAdPlacementContent : ShowAdPlacementContent
  110. {
  111. public PromoAdPlacementContent(string placementId, IPromoAdOperations operations) : base(placementId, operations)
  112. {
  113. this.promoAdOperations = operations;
  114. }
  115. private IPromoAdOperations promoAdOperations { get; set; }
  116. public PromoMetadata metadata => promoAdOperations.metadata;
  117. }
  118. }