Rewarded ads, question popups and few fixes
This commit is contained in:
102
Assets/Game/Scripts/Ads/AdsManager.cs
Normal file
102
Assets/Game/Scripts/Ads/AdsManager.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using GoogleMobileAds.Api;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class AdsManager : MonoBehaviour
|
||||
{
|
||||
private static AdsManager _instance;
|
||||
public static AdsManager instance => _instance;
|
||||
|
||||
private BannerView bannerView;
|
||||
private InterstitialAd interstitial;
|
||||
private RewardedAd rewardedAd;
|
||||
|
||||
|
||||
|
||||
[Header("Ids")]
|
||||
[SerializeField]private string bannerId;
|
||||
[SerializeField]private string interstitialId;
|
||||
[SerializeField]private string rewardedId;
|
||||
|
||||
public UnityEvent onAwardEarned = new UnityEvent();
|
||||
|
||||
|
||||
public bool dontDestroyOnLoad=true;
|
||||
void Start()
|
||||
{
|
||||
_instance =this;
|
||||
if(dontDestroyOnLoad){DontDestroyOnLoad(gameObject);}
|
||||
|
||||
MobileAds.Initialize(initStatus => { Debug.Log(initStatus); });
|
||||
//RequestBanner();
|
||||
LoadInterestitial();
|
||||
LoadRewarded();
|
||||
}
|
||||
|
||||
|
||||
void RequestBanner(){
|
||||
bannerView = new BannerView(bannerId, AdSize.Banner, AdPosition.BottomLeft);
|
||||
AdRequest request = new AdRequest.Builder().Build();
|
||||
this.bannerView.LoadAd(request);
|
||||
}
|
||||
|
||||
void LoadInterestitial(){
|
||||
interstitial = new InterstitialAd(interstitialId);
|
||||
// Create an empty ad request.
|
||||
AdRequest request = new AdRequest.Builder().Build();
|
||||
// Load the interstitial with the request.
|
||||
interstitial.LoadAd(request);
|
||||
}
|
||||
|
||||
void LoadRewarded(){
|
||||
rewardedAd = new RewardedAd(rewardedId);
|
||||
rewardedAd.OnUserEarnedReward += OnRewardedComplete;
|
||||
rewardedAd.OnAdFailedToLoad += OnRewardedFailed;
|
||||
rewardedAd.OnAdFailedToShow += OnRewardedFailed;
|
||||
rewardedAd.OnAdClosed += OnRewardCanceled;
|
||||
|
||||
|
||||
|
||||
AdRequest request = new AdRequest.Builder().Build();
|
||||
this.rewardedAd.LoadAd(request);
|
||||
}
|
||||
|
||||
public async void ShowInterestitial(){
|
||||
interstitial.Show();
|
||||
await Task.Delay(10000);
|
||||
LoadInterestitial();
|
||||
}
|
||||
|
||||
public async void ShowRewarded(UnityAction OnAwardEarned){
|
||||
onAwardEarned.RemoveAllListeners();
|
||||
onAwardEarned.AddListener(OnAwardEarned);
|
||||
if(!rewardedAd.IsLoaded()){
|
||||
LoadRewarded();
|
||||
}
|
||||
while(!rewardedAd.IsLoaded()){
|
||||
await Task.Delay(500);
|
||||
}
|
||||
this.rewardedAd.Show();
|
||||
}
|
||||
|
||||
void OnRewardedComplete(object sender, EventArgs args){
|
||||
onAwardEarned.Invoke();
|
||||
LoadRewarded();
|
||||
}
|
||||
|
||||
void OnRewardedFailed(object sender, EventArgs args){
|
||||
MessageDialog.instance.ShowMessage("Sorry", "We couldn't Load the Ad right now. Please try again later.");
|
||||
LoadRewarded();
|
||||
|
||||
}
|
||||
|
||||
void OnRewardCanceled(object sender, EventArgs args){
|
||||
MessageDialog.instance.ShowMessage("Failed", "You closed the Ad. Please watch the full ad to receive the gift");
|
||||
LoadRewarded();
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Game/Scripts/Ads/AdsManager.cs.meta
Normal file
11
Assets/Game/Scripts/Ads/AdsManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ef88ff9cfdb829c818860c6313e1e270
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user