, using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_ADS
using UnityEngine.Advertisements;
public class OwnAdManager : MonoBehaviour, IUnityAdsListener
{
public static OwnAdManager Instance;
[SerializeField] string AppId, BannerId, InterstitialId, VideoId;
[SerializeField] bool IsBanner = false, isInterstitial = false, IsVideo = false;
private float LastAdShowTime = 0f;
[SerializeField] float Ads_Delay_Time = 0f;
[SerializeField] bool testMode = false;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
}
void Start()
{
Advertisement.Initialize(AppId, testMode);
LoadVideoAd();
}
#region ------------------------VideoAD--------------------------
public void LoadVideoAd()
{
Advertisement.Load(VideoId);
}
public void ShowVideoAd()
{
if (!SaveData.Instance.RemoveAds && IsVideo && LastAdShowTime > Ads_Delay_Time)
{
Advertisement.AddListener(this);
// Check if UnityAds ready before calling Show method:
if (Advertisement.IsReady(VideoId))
{
Advertisement.Show(VideoId);
LastAdShowTime = 0;
Invoke(nameof(LoadVideoAd), 5f);
}
else
{
Debug.Log("Rewarded video is not ready at the moment! Please try again later!");
LoadVideoAd();
}
}
}
// Implement IUnityAdsListener interface methods:
public void OnUnityAdsDidFinish(string surfacingId, ShowResult showResult)
{
// Define conditional logic for each ad completion status:
if (showResult == ShowResult.Finished)
{
// Reward the user for watching the ad to completion.
}
else if (showResult == ShowResult.Skipped)
{
// Do not reward the user for skipping the ad.
}
else if (showResult == ShowResult.Failed)
{
Debug.LogWarning("The ad did not finish due to an error.");
}
Advertisement.RemoveListener(this);
}
public void OnUnityAdsReady(string surfacingId)
{
// If the ready Ad Unit or legacy Placement is rewarded, show the ad:
//if (surfacingId == mySurfacingId)
{
// Optional actions to take when theAd Unit or legacy Placement becomes ready (for example, enable the rewarded ads button)
}
}
public void OnUnityAdsDidError(string message)
{
// Log the error.
Advertisement.RemoveListener(this);
}
public void OnUnityAdsDidStart(string surfacingId)
{
// Optional actions to take when the end-users triggers an ad.
}
// When the object that subscribes to ad events is destroyed, remove the listener:
public void OnDestroy()
{
Advertisement.RemoveListener(this);
}
#endregion
#region -----------------BannerAD--------------------------
public void RequestMainMenuBanner()
{
if (!SaveData.Instance.RemoveAds)
{
if (Advertisement.Banner.isLoaded)
{
Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
Advertisement.Banner.Show(BannerId);
}
else
{
Advertisement.Banner.Load();
}
}
}
public void HideMainMenuBanner()
{
if (!Advertisement.Banner.isLoaded)
Advertisement.Banner.Hide(false);
}
#endregion
#region ----------------InterstitialAd-------------------
public void LoadInterstitialAd()
{
Advertisement.Load(InterstitialId);
}
public void ShowInterstitialAd()
{
if (!SaveData.Instance.RemoveAds)
{
// Check if UnityAds ready before calling Show method:
if (Advertisement.IsReady(InterstitialId))
{
Advertisement.Show(InterstitialId);
// Replace mySurfacingId with the ID of the placements you wish to display as shown in your Unity Dashboard.
LoadInterstitialAd();
}
else
{
LoadInterstitialAd();
Debug.Log("Interstitial ad not ready at the moment! Please try again later!");
}
}
}
#endregion
private void Update()
{
LastAdShowTime += Time.deltaTime;
}
public void PermanentRemoveAds()
{
SaveData.Instance.RemoveAds = true;
}
}
#endif
↧