123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- using System;
- using System.Collections;
- using UnityEngine.Advertisements;
- using UnityEngine;
-
- public class UnityAdsManager : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
- {
- public string GAME_ID = "3003911"; //replace with your gameID from dashboard. note: will be different for each platform.
-
- private const string BANNER_PLACEMENT = "banner";
- private const string VIDEO_PLACEMENT = "video";
- private const string REWARDED_VIDEO_PLACEMENT = "rewardedVideo";
-
- [SerializeField] private BannerPosition bannerPosition = BannerPosition.BOTTOM_CENTER;
-
- private bool testMode = true;
- private bool showBanner = false;
-
- //utility wrappers for debuglog
- public delegate void DebugEvent(string msg);
- public static event DebugEvent OnDebugLog;
-
- public void Initialize()
- {
- if (Advertisement.isSupported)
- {
- DebugLog(Application.platform + " supported by Advertisement");
- }
- Advertisement.Initialize(GAME_ID, testMode, this);
- }
-
- public void ToggleBanner()
- {
- showBanner = !showBanner;
-
- if (showBanner)
- {
- Advertisement.Banner.SetPosition(bannerPosition);
- Advertisement.Banner.Show(BANNER_PLACEMENT);
- }
- else
- {
- Advertisement.Banner.Hide(false);
- }
- }
-
- public void LoadRewardedAd()
- {
- Advertisement.Load(REWARDED_VIDEO_PLACEMENT, this);
- }
-
- public void ShowRewardedAd()
- {
- Advertisement.Show(REWARDED_VIDEO_PLACEMENT, this);
- }
-
- public void LoadNonRewardedAd()
- {
- Advertisement.Load(VIDEO_PLACEMENT, this);
- }
-
- public void ShowNonRewardedAd()
- {
- Advertisement.Show(VIDEO_PLACEMENT, this);
- }
-
- #region Interface Implementations
- public void OnInitializationComplete()
- {
- DebugLog("Init Success");
- }
-
- public void OnInitializationFailed(UnityAdsInitializationError error, string message)
- {
- DebugLog($"Init Failed: [{error}]: {message}");
- }
-
- public void OnUnityAdsAdLoaded(string placementId)
- {
- DebugLog($"Load Success: {placementId}");
- }
-
- public void OnUnityAdsFailedToLoad(string placementId, UnityAdsLoadError error, string message)
- {
- DebugLog($"Load Failed: [{error}:{placementId}] {message}");
- }
-
- public void OnUnityAdsShowFailure(string placementId, UnityAdsShowError error, string message)
- {
- DebugLog($"OnUnityAdsShowFailure: [{error}]: {message}");
- }
-
- public void OnUnityAdsShowStart(string placementId)
- {
- DebugLog($"OnUnityAdsShowStart: {placementId}");
- }
-
- public void OnUnityAdsShowClick(string placementId)
- {
- DebugLog($"OnUnityAdsShowClick: {placementId}");
- }
-
- public void OnUnityAdsShowComplete(string placementId, UnityAdsShowCompletionState showCompletionState)
- {
- DebugLog($"OnUnityAdsShowComplete: [{showCompletionState}]: {placementId}");
- }
- #endregion
-
- public void OnGameIDFieldChanged(string newInput)
- {
- GAME_ID = newInput;
- }
-
- public void ToggleTestMode(bool isOn)
- {
- testMode = isOn;
- }
-
- //wrapper around debug.log to allow broadcasting log strings to the UI
- void DebugLog(string msg)
- {
- OnDebugLog?.Invoke(msg);
- Debug.Log(msg);
- }
- }
|