157 lines
4.7 KiB
C#
157 lines
4.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using GoogleMobileAds.Api;
|
|
using System.Threading.Tasks;
|
|
using System;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class AdsManager : MonoBehaviour
|
|
{
|
|
const bool ENABLE_REWARDED = false;
|
|
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;
|
|
|
|
[Header("Ids apple")]
|
|
[SerializeField]private string ios_bannerId;
|
|
[SerializeField]private string ios_interstitialId;
|
|
[SerializeField]private string ios_rewardedId;
|
|
|
|
public UnityEvent onAwardEarned = new UnityEvent();
|
|
|
|
|
|
public bool dontDestroyOnLoad=true;
|
|
void Start()
|
|
{
|
|
#if UNITY_STANDALONE_WIN
|
|
SceneManager.LoadScene("Login");
|
|
#endif
|
|
_instance =this;
|
|
if(dontDestroyOnLoad){DontDestroyOnLoad(gameObject);}
|
|
|
|
MobileAds.Initialize(OnInit);
|
|
|
|
|
|
}
|
|
float t =0;
|
|
void Update(){
|
|
if(t>0){
|
|
if(t< 30){
|
|
t += Time.deltaTime;
|
|
}else{
|
|
SceneManager.LoadScene("Login");
|
|
t=-10;
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnInit(InitializationStatus initStatus){
|
|
// MessageDialog.instance.ShowMessage("Debug","Ads inited");
|
|
SceneManager.LoadScene("Login");
|
|
Debug.Log("Google ads init,"+initStatus);
|
|
Debug.Log("Loading ads now");
|
|
|
|
if(ENABLE_REWARDED){
|
|
rewardedAd = new RewardedAd((Application.platform == RuntimePlatform.IPhonePlayer) ?ios_rewardedId :rewardedId);
|
|
rewardedAd.OnUserEarnedReward += OnRewardedComplete;
|
|
rewardedAd.OnAdFailedToLoad += OnRewardedFailed;
|
|
rewardedAd.OnAdFailedToShow += OnRewardedFailed;
|
|
LoadRewarded();
|
|
|
|
}
|
|
// RequestBanner();
|
|
LoadInterestitial();
|
|
}
|
|
|
|
private static UnityEvent onInterestitialClosed = new UnityEvent();
|
|
public static void SetOnInterestitialClosed(UnityAction e){
|
|
onInterestitialClosed = new UnityEvent();
|
|
|
|
onInterestitialClosed.AddListener(e);
|
|
}
|
|
|
|
void OnInterestitialClosed(object sender, EventArgs args){
|
|
onInterestitialClosed.Invoke();
|
|
}
|
|
|
|
void RequestBanner(){
|
|
bannerView = new BannerView((Application.platform == RuntimePlatform.IPhonePlayer) ? ios_bannerId : bannerId, AdSize.Banner, AdPosition.BottomLeft);
|
|
AdRequest request = new AdRequest.Builder().Build();
|
|
this.bannerView.LoadAd(request);
|
|
}
|
|
|
|
void LoadInterestitial(){
|
|
interstitial = new InterstitialAd((Application.platform == RuntimePlatform.IPhonePlayer) ? ios_interstitialId : interstitialId);
|
|
interstitial.OnAdClosed += OnInterestitialClosed;
|
|
|
|
// Create an empty ad request.
|
|
AdRequest request = new AdRequest.Builder().Build();
|
|
// Load the interstitial with the request.
|
|
interstitial.LoadAd(request);
|
|
}
|
|
|
|
void LoadRewarded(){
|
|
AdRequest request = new AdRequest.Builder().Build();
|
|
this.rewardedAd.LoadAd(request);
|
|
}
|
|
|
|
public async void ShowInterestitial(){
|
|
Debug.Log("Showing int ad");
|
|
interstitial.Show();
|
|
await Task.Delay(10000);
|
|
LoadInterestitial();
|
|
}
|
|
|
|
public async void ShowRewarded(UnityAction OnAwardEarned){
|
|
// MessageDialog.instance.ShowMessage("Debug","Showing rewarded, is rewareded null? : " + (rewardedAd == null).ToString());
|
|
|
|
onAwardEarned.RemoveAllListeners();
|
|
onAwardEarned.AddListener(OnAwardEarned);
|
|
Debug.Log("Show Reward : is ad null? : " + (rewardedAd == null).ToString());
|
|
|
|
if(!rewardedAd.IsLoaded()){
|
|
Debug.Log("Ad not loaded, Loading now");
|
|
LoadRewarded();
|
|
}else{
|
|
|
|
}
|
|
while(!rewardedAd.IsLoaded()){
|
|
await Task.Delay(500);
|
|
}
|
|
Debug.Log("Ad ready, showing now");
|
|
|
|
this.rewardedAd.Show();
|
|
Debug.Log("Ad shown");
|
|
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
}
|