Last active
April 7, 2019 15:38
-
-
Save cerenagdas/d84a1239186b20c4766d6d5523ff6d4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| using UnityEngine.Advertisements; | |
| using System; | |
| public class MoPubController : MonoBehaviour | |
| { | |
| private static DateTime lastTime; | |
| private static DateTime currentTime; | |
| private static bool isTimeInitialized; | |
| public int interstitialTime = 30; | |
| public static MoPubController instance; | |
| // Flag indicating that the General Data Protection Regulation (GDPR) applies to this user | |
| private bool? _isGdprApplicable = false; | |
| // Flag indicating that the General Data Protection Regulation (GDPR) has been forcibly applied by the publisher | |
| private bool _isGdprForced = false; | |
| public bool isRewardedShown = false; | |
| private bool interstitialIsReady = false; | |
| private bool isRewardedReady = false; | |
| private bool giveReward = false; | |
| private bool _isRewardedLoading = false; | |
| private bool _isInterstitialLoading = false; | |
| #if UNITY_IOS | |
| private readonly string bannerAdUnit = "0ac59b0996d947309c33f59d6676399f"; | |
| private readonly string interstitialAdUnit = "4f117153f5c24fa6a3a92b818a5eb630"; | |
| private readonly string rewardedVideoAdUnit = "8f000bd5e00246de9c789eed39ff6096"; | |
| private readonly string rewardedRichMediaAdUnit = ""; | |
| #elif UNITY_ANDROID || UNITY_EDITOR | |
| private readonly string bannerAdUnit = "b195f8dd8ded45fe847ad89ed1d016da"; | |
| private readonly string interstitialAdUnit = "24534e1901884e398f1253216226017e"; | |
| private readonly string rewardedVideoAdUnit = "920b6145fb1546cf8b5cf2ac34638bb7"; | |
| private readonly string rewardedRichMediaAdUnit = "a96ae2ef41d44822af45c6328c4e1eb1"; | |
| #endif | |
| void MakeInstance() | |
| { | |
| if (instance == null) | |
| { | |
| instance = this; | |
| } | |
| } | |
| private void Awake() | |
| { | |
| if (!isTimeInitialized) | |
| { | |
| lastTime = DateTime.Now; | |
| isTimeInitialized = true; | |
| } | |
| MakeInstance(); | |
| DontDestroyOnLoad(this); | |
| SubscribeEvents(); | |
| } | |
| private void InitializeMoPub() | |
| { | |
| Debug.Log("[MoPubController] InitializeMoPub()"); | |
| var anyAdUnitId = interstitialAdUnit; | |
| Debug.Log("[MoPubController] InitializeMoPub() anyAdUnitId : " + anyAdUnitId); | |
| MoPub.InitializeSdk(new MoPubBase.SdkConfiguration | |
| { | |
| AdUnitId = anyAdUnitId, | |
| // Set desired log level here to override default level of MPLogLevelNone | |
| LogLevel = MoPubBase.LogLevel.MPLogLevelDebug, | |
| // Uncomment the following line to allow supported SDK networks to collect user information on the basis | |
| // of legitimate interest. | |
| //AllowLegitimateInterest = true, | |
| // Specify the mediated networks you are using here: | |
| MediatedNetworks = new MoPub.MediatedNetwork[] | |
| { | |
| new MoPub.SupportedNetwork.AppLovin() | |
| }, | |
| }); | |
| } | |
| private static bool _startLock = false; | |
| private void Start() | |
| { | |
| if (_startLock) | |
| return; | |
| _startLock = true; | |
| AppLovinInitializer appLovin = new AppLovinInitializer(); | |
| appLovin.Initialize(); | |
| // NOTE: the MoPub SDK needs to be initialized on Start() to ensure all other objects have been enabled first. | |
| InitializeMoPub(); | |
| MoPub.LoadBannerPluginsForAdUnits(new[] {bannerAdUnit}); | |
| MoPub.LoadInterstitialPluginsForAdUnits(new[] {interstitialAdUnit}); | |
| MoPub.LoadRewardedVideoPluginsForAdUnits(new[] {rewardedVideoAdUnit}); | |
| #if !(UNITY_ANDROID || UNITY_IOS) | |
| Debug.LogError("Please switch to either Android or iOS platforms to run sample app!"); | |
| #endif | |
| #if UNITY_EDITOR | |
| Debug.LogWarning("No SDK was loaded since this is not on a mobile device! Real ads will not load."); | |
| #endif | |
| } | |
| private void SubscribeEvents() | |
| { | |
| Debug.Log("[MoPubController] SubscribeEvents()"); | |
| MoPubManager.OnSdkInitializedEvent += OnSdkInitializedEvent; | |
| MoPubManager.OnConsentStatusChangedEvent += OnConsentStatusChangedEvent; | |
| MoPubManager.OnConsentDialogLoadedEvent += OnConsentDialogLoadedEvent; | |
| MoPubManager.OnConsentDialogFailedEvent += OnConsentDialogFailedEvent; | |
| MoPubManager.OnConsentDialogShownEvent += OnConsentDialogShownEvent; | |
| MoPubManager.OnInterstitialLoadedEvent += OnInterstitialLoadedEvent; | |
| MoPubManager.OnInterstitialFailedEvent += OnInterstitialFailedEvent; | |
| MoPubManager.OnInterstitialShownEvent += OnInterstitialShownEvent; | |
| MoPubManager.OnInterstitialClickedEvent += OnInterstitialClickedEvent; | |
| MoPubManager.OnInterstitialDismissedEvent += OnInterstitialDismissedEvent; | |
| MoPubManager.OnInterstitialExpiredEvent += OnInterstitialExpiredEvent; | |
| MoPubManager.OnRewardedVideoLoadedEvent += OnRewardedVideoLoadedEvent; | |
| MoPubManager.OnRewardedVideoFailedEvent += OnRewardedVideoFailedEvent; | |
| MoPubManager.OnRewardedVideoExpiredEvent += OnRewardedVideoExpiredEvent; | |
| MoPubManager.OnRewardedVideoShownEvent += OnRewardedVideoShownEvent; | |
| MoPubManager.OnRewardedVideoClickedEvent += OnRewardedVideoClickedEvent; | |
| MoPubManager.OnRewardedVideoFailedToPlayEvent += OnRewardedVideoFailedToPlayEvent; | |
| MoPubManager.OnRewardedVideoReceivedRewardEvent += OnRewardedVideoReceivedRewardEvent; | |
| MoPubManager.OnRewardedVideoClosedEvent += OnRewardedVideoClosedEvent; | |
| MoPubManager.OnRewardedVideoLeavingApplicationEvent += OnRewardedVideoLeavingApplicationEvent; | |
| } | |
| private void UnsubscribeEvents() | |
| { | |
| MoPubManager.OnSdkInitializedEvent -= OnSdkInitializedEvent; | |
| MoPubManager.OnConsentStatusChangedEvent += OnConsentStatusChangedEvent; | |
| MoPubManager.OnConsentDialogLoadedEvent += OnConsentDialogLoadedEvent; | |
| MoPubManager.OnConsentDialogFailedEvent += OnConsentDialogFailedEvent; | |
| MoPubManager.OnConsentDialogShownEvent += OnConsentDialogShownEvent; | |
| MoPubManager.OnInterstitialLoadedEvent -= OnInterstitialLoadedEvent; | |
| MoPubManager.OnInterstitialFailedEvent -= OnInterstitialFailedEvent; | |
| MoPubManager.OnInterstitialShownEvent -= OnInterstitialShownEvent; | |
| MoPubManager.OnInterstitialClickedEvent -= OnInterstitialClickedEvent; | |
| MoPubManager.OnInterstitialDismissedEvent -= OnInterstitialDismissedEvent; | |
| MoPubManager.OnInterstitialExpiredEvent -= OnInterstitialExpiredEvent; | |
| MoPubManager.OnRewardedVideoLoadedEvent -= OnRewardedVideoLoadedEvent; | |
| MoPubManager.OnRewardedVideoFailedEvent -= OnRewardedVideoFailedEvent; | |
| MoPubManager.OnRewardedVideoExpiredEvent -= OnRewardedVideoExpiredEvent; | |
| MoPubManager.OnRewardedVideoShownEvent -= OnRewardedVideoShownEvent; | |
| MoPubManager.OnRewardedVideoClickedEvent -= OnRewardedVideoClickedEvent; | |
| MoPubManager.OnRewardedVideoFailedToPlayEvent -= OnRewardedVideoFailedToPlayEvent; | |
| MoPubManager.OnRewardedVideoReceivedRewardEvent -= OnRewardedVideoReceivedRewardEvent; | |
| MoPubManager.OnRewardedVideoClosedEvent -= OnRewardedVideoClosedEvent; | |
| MoPubManager.OnRewardedVideoLeavingApplicationEvent -= OnRewardedVideoLeavingApplicationEvent; | |
| } | |
| public bool InterstitialIsReady() | |
| { | |
| return interstitialIsReady; | |
| } | |
| public bool RewardedIsReady() | |
| { | |
| return MoPub.HasRewardedVideo(rewardedVideoAdUnit); | |
| } | |
| public bool GiveReward() | |
| { | |
| return giveReward; | |
| } | |
| public void RemoveReward() | |
| { | |
| giveReward = false; | |
| //isRewardedShown = false; | |
| } | |
| private void InitAds() | |
| { | |
| RequestInterstitialVideo(); | |
| RequestRewardedVideo(); | |
| Debug.Log("[MopubController] InitAds()"); | |
| } | |
| public void RequestRewardedVideo() | |
| { | |
| if (!_isRewardedLoading) | |
| { | |
| _isRewardedLoading = true; | |
| MoPub.RequestRewardedVideo(rewardedVideoAdUnit); | |
| } | |
| Debug.LogWarning("Rewarded Video Request"); | |
| } | |
| public void RequestInterstitialVideo() | |
| { | |
| if (!_isInterstitialLoading) | |
| { | |
| _isInterstitialLoading = true; | |
| MoPub.RequestInterstitialAd(interstitialAdUnit); | |
| } | |
| Debug.LogWarning("Interstitial Video Request"); | |
| } | |
| public void ShowRewardedVideo() | |
| { | |
| if (!MoPub.HasRewardedVideo(rewardedVideoAdUnit)) | |
| { | |
| Debug.LogWarning("Rewarded Video is not exists. Requesting new one."); | |
| RequestRewardedVideo(); | |
| return; | |
| } | |
| isRewardedReady = false; | |
| _isRewardedLoading = false; | |
| MoPub.ShowRewardedVideo(rewardedVideoAdUnit); | |
| Debug.LogWarning("Rewarded Video Showing"); | |
| } | |
| public void ShowInterstitialVideo() | |
| { | |
| if (!InterstitialIsReady()) { | |
| RequestInterstitialVideo(); | |
| return; | |
| } | |
| currentTime = DateTime.Now; | |
| Debug.Log("ShowInterstitialVideo"); | |
| Debug.LogWarning("currentTime : " + currentTime); | |
| Debug.LogWarning("lastTime : " + lastTime); | |
| Debug.LogWarning("Difference between currentTime & lastTime : " + (currentTime - lastTime).TotalSeconds); | |
| if ((currentTime - lastTime).TotalSeconds > interstitialTime) | |
| { | |
| Debug.Log("30 second passed"); | |
| interstitialIsReady = false; | |
| _isInterstitialLoading = false; | |
| MoPub.ShowInterstitialAd(interstitialAdUnit); | |
| Debug.LogWarning("Interstitial Video Showing"); | |
| lastTime = DateTime.Now; | |
| } | |
| } | |
| private void OnSdkInitializedEvent(string adUnitId) | |
| { | |
| Debug.Log("OnSdkInitializedEvent: " + adUnitId); | |
| Debug.Log("[MopubController] OnSdkInitializedEvent() MoPub.ShouldShowConsentDialog : " + | |
| MoPub.ShouldShowConsentDialog); | |
| if (MoPub.ShouldShowConsentDialog) | |
| { | |
| MoPub.LoadConsentDialog(); | |
| } | |
| else | |
| { | |
| InitAds(); | |
| } | |
| } | |
| void OnConsentDialogLoaded() | |
| { | |
| MoPub.ShowConsentDialog(); | |
| } | |
| private void OnConsentStatusChangedEvent(MoPub.Consent.Status oldStatus, MoPub.Consent.Status newStatus, | |
| bool canCollectPersonalInfo) | |
| { | |
| Debug.Log("OnConsetStatusChangedEvent: old=" + oldStatus + " new=" + newStatus + " personalInfoOk=" + | |
| canCollectPersonalInfo); | |
| InitAds(); | |
| } | |
| private void OnConsentDialogLoadedEvent() | |
| { | |
| Debug.Log("OnConsentDialogLoadedEvent: dialog loaded"); | |
| } | |
| private void OnConsentDialogFailedEvent(string err) | |
| { | |
| Debug.Log("OnConsentDialogFailedEvent: " + err); | |
| InitAds(); | |
| } | |
| private void OnConsentDialogShownEvent() | |
| { | |
| Debug.Log("OnConsentDialogShownEvent: dialog shown"); | |
| } | |
| private void OnInterstitialLoadedEvent(string adUnitId) | |
| { | |
| Debug.Log("OnInterstitialLoadedEvent: " + adUnitId); | |
| interstitialIsReady = true; | |
| _isInterstitialLoading = false; | |
| } | |
| private void OnInterstitialFailedEvent(string adUnitId, string error) | |
| { | |
| _isInterstitialLoading = false; | |
| } | |
| private void OnInterstitialShownEvent(string adUnitId) | |
| { | |
| Debug.Log("OnInterstitialShownEvent: " + adUnitId); | |
| } | |
| private void OnInterstitialClickedEvent(string adUnitId) | |
| { | |
| Debug.Log("OnInterstitialClickedEvent: " + adUnitId); | |
| } | |
| private void OnInterstitialDismissedEvent(string adUnitId) | |
| { | |
| Debug.Log("OnInterstitialDismissedEvent: " + adUnitId); | |
| RequestInterstitialVideo(); | |
| } | |
| private void OnInterstitialExpiredEvent(string adUnitId) | |
| { | |
| Debug.Log("OnInterstitialExpiredEvent: " + adUnitId); | |
| RequestInterstitialVideo(); | |
| } | |
| // Rewarded Video Events | |
| private void OnRewardedVideoLoadedEvent(string adUnitId) | |
| { | |
| Debug.Log("OnRewardedVideoLoadedEvent: " + adUnitId); | |
| isRewardedReady = true; | |
| _isRewardedLoading = false; | |
| Debug.Log("OnRewardedVideoLoadedEvent: isRewardedReady " + isRewardedReady); | |
| } | |
| private void OnRewardedVideoFailedEvent(string adUnitId, string error) | |
| { | |
| _isRewardedLoading = false; | |
| } | |
| private void OnRewardedVideoExpiredEvent(string adUnitId) | |
| { | |
| Debug.Log("OnRewardedVideoExpiredEvent: " + adUnitId); | |
| } | |
| private void OnRewardedVideoShownEvent(string adUnitId) | |
| { | |
| Debug.Log("OnRewardedVideoShownEvent: " + adUnitId); | |
| } | |
| private void OnRewardedVideoClickedEvent(string adUnitId) | |
| { | |
| Debug.Log("OnRewardedVideoClickedEvent: " + adUnitId); | |
| } | |
| private void OnRewardedVideoFailedToPlayEvent(string adUnitId, string error) | |
| { | |
| } | |
| private void OnRewardedVideoReceivedRewardEvent(string adUnitId, string label, float amount) | |
| { | |
| Debug.Log("OnRewardedVideoReceivedRewardEvent for ad unit id " + adUnitId + " currency:" + label + " amount:" + | |
| amount); | |
| giveReward = true; | |
| isRewardedShown = true; | |
| lastTime = DateTime.Now; | |
| } | |
| private void OnRewardedVideoClosedEvent(string adUnitId) | |
| { | |
| Debug.Log("OnRewardedVideoClosedEvent: " + adUnitId); | |
| RequestRewardedVideo(); | |
| lastTime = DateTime.Now; | |
| } | |
| private void OnRewardedVideoLeavingApplicationEvent(string adUnitId) | |
| { | |
| Debug.Log("OnRewardedVideoLeavingApplicationEvent: " + adUnitId); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment