Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

UnityAdsManager.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections;
  3. using UnityEngine.Advertisements;
  4. using UnityEngine;
  5. public class UnityAdsManager : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
  6. {
  7. public string GAME_ID = "3003911"; //replace with your gameID from dashboard. note: will be different for each platform.
  8. private const string BANNER_PLACEMENT = "banner";
  9. private const string VIDEO_PLACEMENT = "video";
  10. private const string REWARDED_VIDEO_PLACEMENT = "rewardedVideo";
  11. [SerializeField] private BannerPosition bannerPosition = BannerPosition.BOTTOM_CENTER;
  12. private bool testMode = true;
  13. private bool showBanner = false;
  14. //utility wrappers for debuglog
  15. public delegate void DebugEvent(string msg);
  16. public static event DebugEvent OnDebugLog;
  17. public void Initialize()
  18. {
  19. if (Advertisement.isSupported)
  20. {
  21. DebugLog(Application.platform + " supported by Advertisement");
  22. }
  23. Advertisement.Initialize(GAME_ID, testMode, this);
  24. }
  25. public void ToggleBanner()
  26. {
  27. showBanner = !showBanner;
  28. if (showBanner)
  29. {
  30. Advertisement.Banner.SetPosition(bannerPosition);
  31. Advertisement.Banner.Show(BANNER_PLACEMENT);
  32. }
  33. else
  34. {
  35. Advertisement.Banner.Hide(false);
  36. }
  37. }
  38. public void LoadRewardedAd()
  39. {
  40. Advertisement.Load(REWARDED_VIDEO_PLACEMENT, this);
  41. }
  42. public void ShowRewardedAd()
  43. {
  44. Advertisement.Show(REWARDED_VIDEO_PLACEMENT, this);
  45. }
  46. public void LoadNonRewardedAd()
  47. {
  48. Advertisement.Load(VIDEO_PLACEMENT, this);
  49. }
  50. public void ShowNonRewardedAd()
  51. {
  52. Advertisement.Show(VIDEO_PLACEMENT, this);
  53. }
  54. #region Interface Implementations
  55. public void OnInitializationComplete()
  56. {
  57. DebugLog("Init Success");
  58. }
  59. public void OnInitializationFailed(UnityAdsInitializationError error, string message)
  60. {
  61. DebugLog($"Init Failed: [{error}]: {message}");
  62. }
  63. public void OnUnityAdsAdLoaded(string placementId)
  64. {
  65. DebugLog($"Load Success: {placementId}");
  66. }
  67. public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
  68. {
  69. DebugLog($"Load Failed: [{error}:{placementId}] {message}");
  70. }
  71. public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
  72. {
  73. DebugLog($"OnUnityAdsShowFailure: [{error}]: {message}");
  74. }
  75. public void OnUnityAdsShowStart(string placementId)
  76. {
  77. DebugLog($"OnUnityAdsShowStart: {placementId}");
  78. }
  79. public void OnUnityAdsShowClick(string placementId)
  80. {
  81. DebugLog($"OnUnityAdsShowClick: {placementId}");
  82. }
  83. public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
  84. {
  85. DebugLog($"OnUnityAdsShowComplete: [{showCompletionState}]: {placementId}");
  86. }
  87. #endregion
  88. public void OnGameIDFieldChanged(string newInput)
  89. {
  90. GAME_ID = newInput;
  91. }
  92. public void ToggleTestMode(bool isOn)
  93. {
  94. testMode = isOn;
  95. }
  96. //wrapper around debug.log to allow broadcasting log strings to the UI
  97. void DebugLog(string msg)
  98. {
  99. OnDebugLog?.Invoke(msg);
  100. Debug.Log(msg);
  101. }
  102. }