You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

119 lines
2.8 KiB

using System.Collections;
using UnityEngine;
#if ADS_ENABLE && ADS_UNITY && (UNITY_ANDROID || UNITY_IOS)
using UnityEngine.Advertisements;
#endif
public class UnityAdsMgr : MonoBehaviour
{
#region Instance
private static UnityAdsMgr _instance;
public static UnityAdsMgr Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType(typeof(UnityAdsMgr)) as UnityAdsMgr;
if (_instance == null)
{
_instance = (new GameObject("UnityAdsManager")).AddComponent<UnityAdsMgr>();
}
}
return _instance;
}
}
#endregion
#if UNITY_ANDROID
public const string UnityAds_ID = "3690981";
#else
public const string UnityAds_ID = "3690980";
#endif
public const string PID_Reward = "rewarded_video";
#if ADS_ENABLE && ADS_UNITY && (UNITY_ANDROID || UNITY_IOS)
protected ShowOptions showOptions;
#endif
protected AdsMgr adsMgr;
void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
public void InitAdsMgr(AdsMgr adsmgr)
{
adsMgr = adsmgr;
#if ADS_ENABLE && ADS_UNITY && (UNITY_ANDROID || UNITY_IOS)
if (Advertisement.isSupported && !Advertisement.isInitialized)
{
#if VER_ADS_TEST
Advertisement.Initialize(UnityAds_ID, true);
#else
Advertisement.Initialize(UnityAds_ID);
#endif
while (!Advertisement.isInitialized || !Advertisement.IsReady())
{
yield return new WaitForSecondsRealtime(0.5f);
}
}
showOptions = new ShowOptions();
#if ADS_UNITY
showOptions.resultCallback = HandleShowResult;
#endif
#endif
}
#region Rewarded Video Ads
public void ShowAds()
{
#if ADS_ENABLE && ADS_UNITY && (UNITY_ANDROID || UNITY_IOS)
Advertisement.Show(PID_Reward, showOptions);
#endif
}
#if ADS_ENABLE && ADS_UNITY && (UNITY_ANDROID || UNITY_IOS)
public void HandleShowResult(ShowResult result)
{
if (adsMgr == null)
{
return;
}
switch (result)
{
case ShowResult.Finished:
adsMgr.OnAdsFinished(AdsMgr.eAdsFinish.Success);
break;
case ShowResult.Skipped:
adsMgr.OnAdsFinished(AdsMgr.eAdsFinish.Skip);
break;
case ShowResult.Failed:
adsMgr.OnAdsFinished(AdsMgr.eAdsFinish.Fail);
break;
default:
break;
}
}
#endif
public static bool GetAdsAvailable()
{
#if ADS_ENABLE && ADS_UNITY && (UNITY_ANDROID || UNITY_IOS)
return Advertisement.IsReady(PID_Reward);
#else
return false;
#endif
}
#endregion Rewarded Video Ads
}