Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Advertisements;
  4.  
  5. namespace GibbestBow.Ads
  6. {
  7. public class UnityAdsNetwork : AdNetwork, IUnityAdsListener
  8. {
  9.  
  10. const string UNITY_ADS_GAME_ID_ANDROID = "2596860";
  11. const string UNITY_ADS_GAME_ID_IOS = "2596861";
  12.  
  13. Action<ShowResult> onRewardedVideo;
  14.  
  15. public override bool GetState(string placementId, AdsType adsType)
  16. {
  17. return Advertisement.IsReady(placementId); // Any type
  18. }
  19.  
  20. public override void Init()
  21. {
  22. NetworkType = AdNetworks.UnityAds;
  23. IncludeInterstitial = true;
  24. IncludeRewardVideo = true;
  25.  
  26. if (!Advertisement.isSupported)
  27. return;
  28.  
  29. if (Application.platform == RuntimePlatform.Android)
  30. Advertisement.Initialize(UNITY_ADS_GAME_ID_ANDROID, Constants.DEBUG_MODE);
  31. else if (Application.platform == RuntimePlatform.IPhonePlayer)
  32. Advertisement.Initialize(UNITY_ADS_GAME_ID_IOS, Constants.DEBUG_MODE);
  33. else
  34. Advertisement.Initialize(UNITY_ADS_GAME_ID_ANDROID, true);
  35.  
  36. Subscribe();
  37.  
  38. }
  39.  
  40. public override bool ShowInterstitial(string placementId)
  41. {
  42. Debug.Log("Unity ads ShowInterstitial");
  43.  
  44. onRewardedVideo = null;
  45.  
  46. if (!Advertisement.isInitialized)
  47. {
  48. return false;
  49. }
  50.  
  51. if (Advertisement.IsReady(placementId))
  52. {
  53. DNSoundManager.GetInstance().Pause();
  54.  
  55. if (GameData.GetInstance().GetCanShowIap())
  56. {
  57. GameData.GetInstance().SetShowIap(true);
  58. }
  59.  
  60. try
  61. {
  62. Advertisement.Show(placementId);
  63. }
  64. catch
  65. {
  66.  
  67. }
  68.  
  69. return true;
  70. }
  71. else
  72. {
  73. return false;
  74. }
  75. }
  76.  
  77.  
  78. public override bool ShowRewardedVideo(Action<ShowResult> callback, string placementId)
  79. {
  80.  
  81. Debug.Log("Unity ads ShowRewardedVideo");
  82.  
  83. if (!Advertisement.isInitialized)
  84. return false;
  85.  
  86. if (!Advertisement.IsReady(placementId))
  87. return false;
  88.  
  89. onRewardedVideo = callback;
  90.  
  91. DNSoundManager.GetInstance().Pause();
  92.  
  93. try
  94. {
  95. Advertisement.Show(placementId);
  96. }
  97. catch
  98. {
  99.  
  100. }
  101.  
  102. return true;
  103. }
  104.  
  105.  
  106. public override void Subscribe()
  107. {
  108. Debug.Log("Unity ads Subscribe");
  109. Advertisement.AddListener(this);
  110. }
  111.  
  112. public override void UnSubscribe()
  113. {
  114. try
  115. {
  116. DNSoundManager.GetInstance().Unpause();
  117. }
  118. catch
  119. {
  120. Debug.LogWarning("Some error in Unpause method");
  121. }
  122.  
  123. Debug.Log("Unity ads UnSubscribe");
  124. Advertisement.RemoveListener(this);
  125. }
  126.  
  127. public void OnUnityAdsDidError(string message)
  128. {
  129. try
  130. {
  131. DNSoundManager.GetInstance().Unpause();
  132. }
  133. catch
  134. {
  135. Debug.LogWarning("Some error in Unpause method");
  136. }
  137. Debug.Log("Unity ads OnUnityAdsDidError");
  138. onRewardedVideo?.Invoke(ShowResult.Failed);
  139. }
  140.  
  141. public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
  142. {
  143. try
  144. {
  145. DNSoundManager.GetInstance().Unpause();
  146. }
  147. catch
  148. {
  149. Debug.LogWarning("Some error in Unpause method");
  150. }
  151. Debug.Log("Unity ads OnUnityAdsDidFinish");
  152. onRewardedVideo?.Invoke(showResult);
  153. }
  154.  
  155. public void OnUnityAdsDidStart(string placementId)
  156. {
  157. }
  158.  
  159. public void OnUnityAdsReady(string placementId)
  160. {
  161. }
  162.  
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement