Appodeal
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
using AppodealAds.Unity.Api;
|
||||
using AppodealAds.Unity.Common;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using GoogleMobileAds.Api;
|
||||
//using GoogleMobileAds.Api;
|
||||
using UnityEngine;
|
||||
|
||||
public class AdsManager : MonoBehaviour
|
||||
public class AdsManager : MonoBehaviour, IAppodealInitializationListener, IInterstitialAdListener, IRewardedVideoAdListener
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
|
||||
@@ -20,143 +22,139 @@ public class AdsManager : MonoBehaviour
|
||||
{
|
||||
DontDestroyOnLoad(gameObject);
|
||||
// Initialize the Google Mobile Ads SDK.
|
||||
MobileAds.Initialize(initStatus => { Debug.Log("admob Init status : " + initStatus.ToString());});
|
||||
// MobileAds.Initialize(initStatus => { Debug.Log("admob Init status : " + initStatus.ToString());});
|
||||
int adTypes = Appodeal.INTERSTITIAL | Appodeal.REWARDED_VIDEO;
|
||||
string appKey = "010153e7ddbb96a5639a058bce268953b4d74b1038e266d1";
|
||||
|
||||
StartCoroutine(ReloadAds(true));
|
||||
Appodeal.initialize(appKey, adTypes,this);
|
||||
|
||||
Appodeal.setAutoCache(Appodeal.INTERSTITIAL, true);
|
||||
Appodeal.setAutoCache(Appodeal.REWARDED_VIDEO, true);
|
||||
}
|
||||
|
||||
private InterstitialAd interstitialAd;
|
||||
public void onInitializationFinished(List<string> errors) { }
|
||||
//private InterstitialAd interstitialAd;
|
||||
|
||||
/// <summary>
|
||||
/// Loads the interstitial ad.
|
||||
/// </summary>
|
||||
public void LoadInterstitialAd()
|
||||
{
|
||||
// Clean up the old ad before loading a new one.
|
||||
if (interstitialAd != null)
|
||||
{
|
||||
interstitialAd.Destroy();
|
||||
interstitialAd = null;
|
||||
}
|
||||
|
||||
Debug.Log("Loading the interstitial ad.");
|
||||
|
||||
// create our request used to load the ad.
|
||||
var adRequest = new AdRequest.Builder()
|
||||
.AddKeyword("unity-admob-sample")
|
||||
.Build();
|
||||
|
||||
// send the request to load the ad.
|
||||
InterstitialAd.Load(intAdId, adRequest,
|
||||
(InterstitialAd ad, LoadAdError error) =>
|
||||
{
|
||||
// if error is not null, the load request failed.
|
||||
if (error != null || ad == null)
|
||||
{
|
||||
Debug.LogError("interstitial ad failed to load an ad " +
|
||||
"with error : " + error);
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.Log("Interstitial ad loaded with response : "
|
||||
+ ad.GetResponseInfo());
|
||||
|
||||
interstitialAd = ad;
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void ShowIntAd()
|
||||
public void ShowInterstitial()
|
||||
{
|
||||
if (interstitialAd != null && interstitialAd.CanShowAd())
|
||||
if (Appodeal.isLoaded(Appodeal.INTERSTITIAL) && Appodeal.canShow(Appodeal.INTERSTITIAL, "interestitial"))
|
||||
{
|
||||
Debug.Log("Showing interstitial ad.");
|
||||
interstitialAd.Show();
|
||||
}
|
||||
else
|
||||
Appodeal.show(Appodeal.INTERSTITIAL, "interestitial");
|
||||
}else if (!Appodeal.isAutoCacheEnabled(Appodeal.INTERSTITIAL))
|
||||
{
|
||||
Debug.LogError("Interstitial ad is not ready yet.");
|
||||
Appodeal.cache(Appodeal.INTERSTITIAL);
|
||||
Debug.Log("Caching INT");
|
||||
}
|
||||
StartCoroutine(ReloadAds(false));
|
||||
|
||||
|
||||
DataManager.AddAdImpression();
|
||||
}
|
||||
|
||||
|
||||
private RewardedAd rewardedAd;
|
||||
|
||||
/// <summary>
|
||||
/// Loads the rewarded ad.
|
||||
/// </summary>
|
||||
public void LoadRewardedAd()
|
||||
public void ShowRewardedVideo()
|
||||
{
|
||||
// Clean up the old ad before loading a new one.
|
||||
if (rewardedAd != null)
|
||||
if (Appodeal.isLoaded(Appodeal.REWARDED_VIDEO) && Appodeal.canShow(Appodeal.REWARDED_VIDEO, "Reward"))
|
||||
{
|
||||
rewardedAd.Destroy();
|
||||
rewardedAd = null;
|
||||
Appodeal.show(Appodeal.REWARDED_VIDEO, "Reward");
|
||||
}
|
||||
else if (!Appodeal.isAutoCacheEnabled(Appodeal.REWARDED_VIDEO))
|
||||
{
|
||||
Appodeal.cache(Appodeal.REWARDED_VIDEO);
|
||||
Debug.Log("Caching REWARD");
|
||||
}
|
||||
|
||||
Debug.Log("Loading the rewarded ad.");
|
||||
|
||||
// create our request used to load the ad.
|
||||
var adRequest = new AdRequest.Builder().Build();
|
||||
|
||||
// send the request to load the ad.
|
||||
RewardedAd.Load(rewardedAdId, adRequest,
|
||||
(RewardedAd ad, LoadAdError error) =>
|
||||
{
|
||||
// if error is not null, the load request failed.
|
||||
if (error != null || ad == null)
|
||||
{
|
||||
Debug.LogError("Rewarded ad failed to load an ad " +
|
||||
"with error : " + error);
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.Log("Rewarded ad loaded with response : "
|
||||
+ ad.GetResponseInfo());
|
||||
|
||||
rewardedAd = ad;
|
||||
|
||||
//rewardedAd.OnAdPaid += OnRewardSuccess;
|
||||
});
|
||||
}
|
||||
|
||||
private void OnRewardSuccess()
|
||||
#region Interstitial callback handlers
|
||||
|
||||
// Called when interstitial was loaded (precache flag shows if the loaded ad is precache)
|
||||
public void onInterstitialLoaded(bool isPrecache)
|
||||
{
|
||||
Debug.Log("Rewarded Ad success, code:1032");
|
||||
Debug.Log("Interstitial loaded");
|
||||
}
|
||||
|
||||
// Called when interstitial failed to load
|
||||
public void onInterstitialFailedToLoad()
|
||||
{
|
||||
Debug.Log("Interstitial failed to load");
|
||||
}
|
||||
|
||||
// Called when interstitial was loaded, but cannot be shown (internal network errors, placement settings, etc.)
|
||||
public void onInterstitialShowFailed()
|
||||
{
|
||||
Debug.Log("Interstitial show failed");
|
||||
}
|
||||
|
||||
// Called when interstitial is shown
|
||||
public void onInterstitialShown()
|
||||
{
|
||||
Debug.Log("Interstitial shown");
|
||||
}
|
||||
|
||||
// Called when interstitial is closed
|
||||
public void onInterstitialClosed()
|
||||
{
|
||||
Debug.Log("Interstitial closed");
|
||||
}
|
||||
|
||||
// Called when interstitial is clicked
|
||||
public void onInterstitialClicked()
|
||||
{
|
||||
Debug.Log("Interstitial clicked");
|
||||
}
|
||||
|
||||
// Called when interstitial is expired and can not be shown
|
||||
public void onInterstitialExpired()
|
||||
{
|
||||
Debug.Log("Interstitial expired");
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region Rewarded Video callback handlers
|
||||
|
||||
//Called when rewarded video was loaded (precache flag shows if the loaded ad is precache).
|
||||
public void onRewardedVideoLoaded(bool isPrecache)
|
||||
{
|
||||
Debug.Log("RewardedVideo loaded");
|
||||
}
|
||||
|
||||
// Called when rewarded video failed to load
|
||||
public void onRewardedVideoFailedToLoad()
|
||||
{
|
||||
Debug.Log("RewardedVideo failed to load");
|
||||
}
|
||||
|
||||
// Called when rewarded video was loaded, but cannot be shown (internal network errors, placement settings, etc.)
|
||||
public void onRewardedVideoShowFailed()
|
||||
{
|
||||
Debug.Log("RewardedVideo show failed");
|
||||
}
|
||||
|
||||
// Called when rewarded video is shown
|
||||
public void onRewardedVideoShown()
|
||||
{
|
||||
Debug.Log("RewardedVideo shown");
|
||||
}
|
||||
|
||||
// Called when reward video is clicked
|
||||
public void onRewardedVideoClicked()
|
||||
{
|
||||
Debug.Log("RewardedVideo clicked");
|
||||
}
|
||||
|
||||
// Called when rewarded video is closed
|
||||
public void onRewardedVideoClosed(bool finished)
|
||||
{
|
||||
Debug.Log("RewardedVideo closed");
|
||||
}
|
||||
|
||||
// Called when rewarded video is viewed until the end
|
||||
public void onRewardedVideoFinished(double amount, string name)
|
||||
{
|
||||
Debug.Log("RewardedVideo finished");
|
||||
|
||||
GameManager.AdWatched();
|
||||
}
|
||||
|
||||
public void ShowRewardedAd()
|
||||
//Called when rewarded video is expired and can not be shown
|
||||
public void onRewardedVideoExpired()
|
||||
{
|
||||
const string rewardMsg =
|
||||
"Rewarded ad rewarded the user. Type: {0}, amount: {1}.";
|
||||
|
||||
if (rewardedAd != null && rewardedAd.CanShowAd())
|
||||
{
|
||||
rewardedAd.Show((Reward reward) =>
|
||||
{
|
||||
OnRewardSuccess();
|
||||
// TODO: Reward the user.
|
||||
Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
|
||||
});
|
||||
}
|
||||
|
||||
StartCoroutine(ReloadAds(true));
|
||||
Debug.Log("RewardedVideo expired");
|
||||
}
|
||||
|
||||
|
||||
IEnumerator ReloadAds(bool rewarded){
|
||||
yield return new WaitForSeconds(2);
|
||||
LoadInterstitialAd();
|
||||
if(rewarded){
|
||||
LoadRewardedAd();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ using UnityEngine;
|
||||
|
||||
public class CheckUpdates : MonoBehaviour
|
||||
{
|
||||
public static int ClientVersion = 4;
|
||||
public static int ClientVersion = 5;
|
||||
public static int ServerVersion;
|
||||
|
||||
public float interval = 30;
|
||||
|
||||
@@ -141,11 +141,11 @@ public class GameManager : MonoBehaviour
|
||||
adCounter++;
|
||||
|
||||
if(DataManager.userData.faucetId > 0){
|
||||
AdsManager.instance.ShowIntAd();
|
||||
AdsManager.instance.ShowInterstitial();
|
||||
}
|
||||
else if(adCounter > 1){
|
||||
adCounter=0;
|
||||
AdsManager.instance.ShowIntAd();
|
||||
AdsManager.instance.ShowInterstitial();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,8 +259,9 @@ public class GameManager : MonoBehaviour
|
||||
public void WatchAd(){
|
||||
#if UNITY_EDITOR
|
||||
AdWatched();
|
||||
|
||||
#else
|
||||
AdsManager.instance.ShowRewardedAd();
|
||||
AdsManager.instance.ShowRewardedVideo();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user