You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

168 rivejä
4.9 KiB

  1. // Copyright (C) 2015 Google, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if UNITY_ANDROID
  15. using System;
  16. using UnityEngine;
  17. using GoogleMobileAds.Api;
  18. using GoogleMobileAds.Common;
  19. namespace GoogleMobileAds.Android
  20. {
  21. public class RewardBasedVideoAdClient : AndroidJavaProxy, IRewardBasedVideoAdClient
  22. {
  23. private AndroidJavaObject androidRewardBasedVideo;
  24. public event EventHandler<EventArgs> OnAdLoaded = delegate { };
  25. public event EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad = delegate { };
  26. public event EventHandler<EventArgs> OnAdOpening = delegate { };
  27. public event EventHandler<EventArgs> OnAdStarted = delegate { };
  28. public event EventHandler<EventArgs> OnAdClosed = delegate { };
  29. public event EventHandler<Reward> OnAdRewarded = delegate { };
  30. public event EventHandler<EventArgs> OnAdLeavingApplication = delegate { };
  31. public event EventHandler<EventArgs> OnAdCompleted = delegate { };
  32. public RewardBasedVideoAdClient()
  33. : base(Utils.UnityRewardBasedVideoAdListenerClassName)
  34. {
  35. AndroidJavaClass playerClass = new AndroidJavaClass(Utils.UnityActivityClassName);
  36. AndroidJavaObject activity =
  37. playerClass.GetStatic<AndroidJavaObject>("currentActivity");
  38. androidRewardBasedVideo = new AndroidJavaObject(Utils.RewardBasedVideoClassName,
  39. activity, this);
  40. }
  41. #region IRewardBasedVideoClient implementation
  42. public void CreateRewardBasedVideoAd()
  43. {
  44. androidRewardBasedVideo.Call("create");
  45. }
  46. public void LoadAd(AdRequest request, string adUnitId)
  47. {
  48. androidRewardBasedVideo.Call("loadAd", Utils.GetAdRequestJavaObject(request), adUnitId);
  49. }
  50. public bool IsLoaded()
  51. {
  52. return androidRewardBasedVideo.Call<bool>("isLoaded");
  53. }
  54. public void ShowRewardBasedVideoAd()
  55. {
  56. androidRewardBasedVideo.Call("show");
  57. }
  58. public void SetUserId(string userId)
  59. {
  60. androidRewardBasedVideo.Call("setUserId", userId);
  61. }
  62. public void DestroyRewardBasedVideoAd()
  63. {
  64. androidRewardBasedVideo.Call("destroy");
  65. }
  66. // Returns the mediation adapter class name.
  67. public string MediationAdapterClassName()
  68. {
  69. return this.androidRewardBasedVideo.Call<string>("getMediationAdapterClassName");
  70. }
  71. #endregion
  72. #region Callbacks from UnityRewardBasedVideoAdListener.
  73. void onAdLoaded()
  74. {
  75. if (this.OnAdLoaded != null)
  76. {
  77. this.OnAdLoaded(this, EventArgs.Empty);
  78. }
  79. }
  80. void onAdFailedToLoad(string errorReason)
  81. {
  82. if (this.OnAdFailedToLoad != null)
  83. {
  84. AdFailedToLoadEventArgs args = new AdFailedToLoadEventArgs()
  85. {
  86. Message = errorReason
  87. };
  88. this.OnAdFailedToLoad(this, args);
  89. }
  90. }
  91. void onAdOpened()
  92. {
  93. if (this.OnAdOpening != null)
  94. {
  95. this.OnAdOpening(this, EventArgs.Empty);
  96. }
  97. }
  98. void onAdStarted()
  99. {
  100. if (this.OnAdStarted != null)
  101. {
  102. this.OnAdStarted(this, EventArgs.Empty);
  103. }
  104. }
  105. void onAdClosed()
  106. {
  107. if (this.OnAdClosed != null)
  108. {
  109. this.OnAdClosed(this, EventArgs.Empty);
  110. }
  111. }
  112. void onAdRewarded(string type, float amount)
  113. {
  114. if (this.OnAdRewarded != null)
  115. {
  116. Reward args = new Reward()
  117. {
  118. Type = type,
  119. Amount = amount
  120. };
  121. this.OnAdRewarded(this, args);
  122. }
  123. }
  124. void onAdLeftApplication()
  125. {
  126. if (this.OnAdLeavingApplication != null)
  127. {
  128. this.OnAdLeavingApplication(this, EventArgs.Empty);
  129. }
  130. }
  131. void onAdCompleted()
  132. {
  133. if (this.OnAdCompleted != null)
  134. {
  135. this.OnAdCompleted(this, EventArgs.Empty);
  136. }
  137. }
  138. #endregion
  139. }
  140. }
  141. #endif