249 lines
8.2 KiB
C#
249 lines
8.2 KiB
C#
|
using cfg.AD;
|
|||
|
using System;
|
|||
|
using PhxhSDK;
|
|||
|
using Framework;
|
|||
|
using System.Linq;
|
|||
|
using System.Collections.Generic;
|
|||
|
|
|||
|
namespace Gameplay.Manager
|
|||
|
{
|
|||
|
public class ADManager : Singlenton<ADManager>
|
|||
|
{
|
|||
|
private ADConfig _adConfig;
|
|||
|
private Dictionary<AdSDKManager.ADSdkName, ADHelper> _adHelpers;
|
|||
|
private List<AdSDKManager.ADSdkName> _adSdkNames;
|
|||
|
private int _currentAdIndex = 0;
|
|||
|
|
|||
|
//Debug 相关
|
|||
|
public int DebugAdSDKIndex = -1;
|
|||
|
public bool IsDebug;
|
|||
|
|
|||
|
public void Init()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_adConfig = TableManager.Instance.Tables.ADConfig;
|
|||
|
_adHelpers = new Dictionary<AdSDKManager.ADSdkName, ADHelper>();
|
|||
|
#if SDK_ADMOD
|
|||
|
_adHelpers.Add(AdSDKManager.ADSdkName.AdMod,
|
|||
|
AdSDKManager.Instance.GetSdkHelper(AdSDKManager.ADSdkName.AdMod));
|
|||
|
#endif
|
|||
|
#if SDK_UNITYADS
|
|||
|
_adHelpers.Add(AdSDKManager.ADSdkName.UnityAds,
|
|||
|
AdSDKManager.Instance.GetSdkHelper(AdSDKManager.ADSdkName.UnityAds));
|
|||
|
#endif
|
|||
|
PreLoadAD();
|
|||
|
_adSdkNames = _adHelpers.Keys.ToList();
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
DebugUtil.LogError("ADManager Init Error: {1}", e);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void PreLoadAD()
|
|||
|
{
|
|||
|
foreach (var helper in _adHelpers)
|
|||
|
{
|
|||
|
helper.Value.LoadRewardedAdWithUnitId(GetUnitIDs(helper.Key));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private List<string> GetUnitIDs(AdSDKManager.ADSdkName adSdkName)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
List<string> ids = null;
|
|||
|
switch (adSdkName)
|
|||
|
{
|
|||
|
case AdSDKManager.ADSdkName.AdMod:
|
|||
|
{
|
|||
|
#if UNITY_ANDROID
|
|||
|
ids = _adConfig?.DataList.Select(data => data.AdModUnitIdAndroid).ToList();
|
|||
|
#endif
|
|||
|
#if UNITY_IOS
|
|||
|
ids = _adConfig?.DataList.Select(data => data.AdModUnitIdIOS).ToList();
|
|||
|
#endif
|
|||
|
break;
|
|||
|
}
|
|||
|
case AdSDKManager.ADSdkName.UnityAds:
|
|||
|
{
|
|||
|
#if UNITY_ANDROID
|
|||
|
ids = _adConfig?.DataList.Select(data => data.UnityAdsUnitIdAndroid).ToList();
|
|||
|
#endif
|
|||
|
#if UNITY_IOS
|
|||
|
ids = _adConfig?.DataList.Select(data => data.UnityAdsUnitIdIOS).ToList();
|
|||
|
#endif
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return ids;
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
DebugUtil.LogError("ADManager GetUnitIDs AdHelper:{0} ,Error: {1}", adSdkName, e);
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private string GetUnitID(int id, AdSDKManager.ADSdkName adSdkName)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var dataAD = IsDebug ? _adConfig.GetOrDefault(100) : _adConfig.GetOrDefault(id);
|
|||
|
string adUnitID = "";
|
|||
|
switch (adSdkName)
|
|||
|
{
|
|||
|
case AdSDKManager.ADSdkName.AdMod:
|
|||
|
{
|
|||
|
#if UNITY_ANDROID
|
|||
|
adUnitID = dataAD?.AdModUnitIdAndroid;
|
|||
|
#endif
|
|||
|
#if UNITY_IOS
|
|||
|
adUnitID = dataAD?.AdModUnitIdIOS;
|
|||
|
#endif
|
|||
|
break;
|
|||
|
}
|
|||
|
case AdSDKManager.ADSdkName.UnityAds:
|
|||
|
{
|
|||
|
#if UNITY_ANDROID
|
|||
|
adUnitID = dataAD?.UnityAdsUnitIdAndroid;
|
|||
|
#endif
|
|||
|
#if UNITY_IOS
|
|||
|
adUnitID = dataAD?.UnityAdsUnitIdIOS;
|
|||
|
#endif
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return adUnitID;
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
DebugUtil.LogError("ADManager GetUnitID AdHelper: {0},id: {1} ,Error: {2}", adSdkName, id, e);
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void Release()
|
|||
|
{
|
|||
|
foreach (var helper in _adHelpers)
|
|||
|
{
|
|||
|
helper.Value.Release();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void ShowAdWithPreLoad(int id, Action rewardSuccess, Action rewardFail)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (_currentAdIndex >= _adHelpers.Count)
|
|||
|
{
|
|||
|
_currentAdIndex = 0;
|
|||
|
}
|
|||
|
|
|||
|
if (DebugAdSDKIndex != -1)
|
|||
|
{
|
|||
|
_currentAdIndex = DebugAdSDKIndex;
|
|||
|
}
|
|||
|
|
|||
|
var curADHelper = _adHelpers[_adSdkNames[_currentAdIndex]];
|
|||
|
|
|||
|
DebugUtil.LogWarning("当前使用:{0} 平台播放广告", _adSdkNames[_currentAdIndex]);
|
|||
|
var unitID = GetUnitID(id, _adSdkNames[_currentAdIndex]);
|
|||
|
if (!string.IsNullOrEmpty(unitID))
|
|||
|
curADHelper.ShowRewardedAd(unitID, rewardSuccess, rewardFail);
|
|||
|
_currentAdIndex++;
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
DebugUtil.LogError("ADManager ShowAd {0} with Error: {1}", id, e);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public bool CanShow(int id)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_currentAdIndex = _currentAdIndex >= _adHelpers.Count ? 0 : _currentAdIndex;
|
|||
|
var helperKey = _adSdkNames[_currentAdIndex];
|
|||
|
if (!_adHelpers.TryGetValue(helperKey, out var curADHelper))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
var unitID = GetUnitID(id, _adSdkNames[_currentAdIndex]);
|
|||
|
var index = 0;
|
|||
|
while (!curADHelper.CanShow(unitID))
|
|||
|
{
|
|||
|
if (index == _adSdkNames.Count)
|
|||
|
{
|
|||
|
DebugUtil.LogError("各个平台全都无法播放广告");
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
DebugUtil.LogWarning("{0}重新加載:{1}", _adSdkNames[_currentAdIndex], unitID);
|
|||
|
curADHelper.LoadRewardedAdWithUnitId(unitID);
|
|||
|
_currentAdIndex++;
|
|||
|
if (_currentAdIndex >= _adHelpers.Count)
|
|||
|
{
|
|||
|
_currentAdIndex = 0;
|
|||
|
}
|
|||
|
|
|||
|
unitID = GetUnitID(id, _adSdkNames[_currentAdIndex]);
|
|||
|
curADHelper = _adHelpers[_adSdkNames[_currentAdIndex]];
|
|||
|
index++;
|
|||
|
}
|
|||
|
|
|||
|
DebugUtil.LogWarning("当前可以播放的广告是:{0}", _adSdkNames[_currentAdIndex]);
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
DebugUtil.LogError("ADManager CanShow Error: {0}", e);
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//DEBUG
|
|||
|
public void ShowDebug(int helper, int id, Action rewardSuccess, Action rewardFail)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_currentAdIndex = helper;
|
|||
|
if (_currentAdIndex >= _adHelpers.Count)
|
|||
|
{
|
|||
|
_currentAdIndex = 0;
|
|||
|
DebugUtil.LogError("广告播放器错误,默认改为AdMod");
|
|||
|
}
|
|||
|
|
|||
|
var curADHelper = _adHelpers[_adSdkNames[_currentAdIndex]];
|
|||
|
DebugUtil.LogWarning("当前使用:{0} 平台播放广告", _adSdkNames[_currentAdIndex]);
|
|||
|
var unitID = GetUnitID(id, _adSdkNames[_currentAdIndex]);
|
|||
|
if (!string.IsNullOrEmpty(unitID))
|
|||
|
curADHelper.ShowRewardedAd(unitID, rewardSuccess, rewardFail);
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
DebugUtil.LogError("ADManager ShowDebug {0} with Error: {1}", id, e);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void ReloadAD(int helper, int id)
|
|||
|
{
|
|||
|
_currentAdIndex = helper;
|
|||
|
if (_currentAdIndex >= _adHelpers.Count)
|
|||
|
{
|
|||
|
_currentAdIndex = 0;
|
|||
|
DebugUtil.LogError("广告播放器错误,默认改为AdMod");
|
|||
|
}
|
|||
|
|
|||
|
var curADHelper = _adHelpers[_adSdkNames[_currentAdIndex]];
|
|||
|
DebugUtil.LogWarning("{0} 平台重新加载广告位:{1}", _adSdkNames[_currentAdIndex], id);
|
|||
|
var unitID = GetUnitID(id, _adSdkNames[_currentAdIndex]);
|
|||
|
if (!string.IsNullOrEmpty(unitID))
|
|||
|
curADHelper.LoadRewardedAdWithUnitId(unitID);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|