995 lines
43 KiB
C#
995 lines
43 KiB
C#
#if UNITY_WEBGL && WEBGL_WX
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using UnityEngine;
|
||
using WeChatWASM;
|
||
using SystemInfo = WeChatWASM.SystemInfo;
|
||
|
||
namespace HC
|
||
{
|
||
public class HCWXAds : HCSingleton<HCWXAds>, HCIAds
|
||
{
|
||
private SystemInfo _sysInfo;
|
||
private Action<HCADSstatuscallback, string, int, string, Dictionary<string, object>> _callback;
|
||
|
||
public void InitializeSdk(Action<HCADSstatuscallback, string, int, string, Dictionary<string, object>> callback)
|
||
{
|
||
_callback = callback;
|
||
HCWxTools.Instance.WxCallUnityAction += WxCallUnity;
|
||
}
|
||
|
||
#region Rewarded
|
||
|
||
private readonly Dictionary<string, WXRewardedVideoAd> _rewardedDictionary = new Dictionary<string, WXRewardedVideoAd>();
|
||
private readonly Dictionary<string, bool> _rewardedStatus = new Dictionary<string, bool>();
|
||
|
||
|
||
public void RewardedLoad(string adUnitId)
|
||
{
|
||
WXRewardedVideoAd wxRewardedVideoAd = null;
|
||
|
||
if (_rewardedDictionary.TryGetValue(adUnitId, out var value))
|
||
{
|
||
wxRewardedVideoAd = value;
|
||
HCDebugger.LogDebug($"[ADManager] [InitRewardAd] adUnitId = {adUnitId} - 2");
|
||
}
|
||
|
||
if (wxRewardedVideoAd == null)
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [InitRewardAd] adUnitId = {adUnitId} - 1");
|
||
wxRewardedVideoAd = WXBase.CreateRewardedVideoAd(new WXCreateRewardedVideoAdParam
|
||
{
|
||
adUnitId = adUnitId
|
||
});
|
||
|
||
_rewardedDictionary[adUnitId] = wxRewardedVideoAd;
|
||
|
||
wxRewardedVideoAd.OnLoad(loadResponse =>
|
||
{
|
||
HCDebugger.LogDebug("[ADManager] [InitRewardAd] [OnLoad] RV Onload succ, rewardValue : " + loadResponse.rewardValue +
|
||
" , shareValue : " +
|
||
loadResponse.shareValue + " , errorMsg : " + loadResponse.errMsg);
|
||
_rewardedStatus[adUnitId] = true;
|
||
_callback?.Invoke(HCADSstatuscallback.RewardedLoadSuccess, adUnitId, 0, "", new Dictionary<string, object>
|
||
{
|
||
["type"] = "callback"
|
||
});
|
||
});
|
||
|
||
wxRewardedVideoAd.OnClose(res =>
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [InitRewardAd] [OnClose] res is ended = {res.isEnded}");
|
||
_callback?.Invoke(HCADSstatuscallback.RewardedClose, adUnitId, 0, res.errMsg, new Dictionary<string, object>
|
||
{
|
||
["isEnded"] = res.isEnded
|
||
});
|
||
});
|
||
wxRewardedVideoAd.OnError(error =>
|
||
{
|
||
HCDebugger.LogDebug("[ADManager] [InitRewardAd] [OnError] RV OnError, errorCode : " + error.errCode +
|
||
" , errorMsg : " + error.errMsg);
|
||
_rewardedStatus[adUnitId] = false;
|
||
|
||
_callback?.Invoke(HCADSstatuscallback.RewardedLoadFailed, adUnitId, error.errCode, error.errMsg, new Dictionary<string, object>
|
||
{
|
||
["type"] = "callback"
|
||
});
|
||
});
|
||
}
|
||
|
||
|
||
wxRewardedVideoAd.Load(success =>
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [InitRewardAd] [OnLoad] RV [load] succ, rewardValue : success {success.errMsg} {success.errCode}");
|
||
// _rewardedStatus[adUnitId] = true;
|
||
// _callback?.Invoke(HCADSstatuscallback.RewardedLoadSuccess, adUnitId, 0, "", new Dictionary<string, object>
|
||
// {
|
||
// ["type"] = "load"
|
||
// });
|
||
}, error =>
|
||
{
|
||
_rewardedStatus[adUnitId] = false;
|
||
_callback?.Invoke(HCADSstatuscallback.RewardedLoadFailed, adUnitId, error.errCode, error.errMsg, new Dictionary<string, object>
|
||
{
|
||
["type"] = "load"
|
||
});
|
||
});
|
||
}
|
||
|
||
public bool IsRewardedAdReady(string adUnitId)
|
||
{
|
||
return _rewardedStatus.TryGetValue(adUnitId, out var status) && status;
|
||
}
|
||
|
||
public void ShowRewardedAd(string adUnitId)
|
||
{
|
||
if (_rewardedDictionary.TryGetValue(adUnitId, out var wxRewardedVideoAd))
|
||
{
|
||
wxRewardedVideoAd.Show(response =>
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [ShowRewardedAd] response success : {response.errCode} {response.errMsg}");
|
||
|
||
_callback?.Invoke(HCADSstatuscallback.RewardedShowSuccess, adUnitId, response.errCode, response.errMsg, null);
|
||
},
|
||
failed =>
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [ShowRewardedAd] response failed : {failed.errCode} {failed.errMsg}");
|
||
|
||
_callback?.Invoke(HCADSstatuscallback.RewardedShowFailed, adUnitId, failed.errCode, failed.errMsg, null);
|
||
});
|
||
}
|
||
else
|
||
{
|
||
_callback?.Invoke(HCADSstatuscallback.RewardedShowFailed, adUnitId, -99, "adUnitId does not exist", null);
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
private readonly Dictionary<string, WXInterstitialAd> _interstitialDictionary = new Dictionary<string, WXInterstitialAd>();
|
||
private readonly Dictionary<string, bool> _interstitialStatus = new Dictionary<string, bool>();
|
||
|
||
|
||
public void LoadInterstitial(string adUnitId)
|
||
{
|
||
WXInterstitialAd interstitialAd = null;
|
||
if (_interstitialDictionary.TryGetValue(adUnitId, out var temp))
|
||
{
|
||
interstitialAd = temp;
|
||
}
|
||
|
||
|
||
if (interstitialAd == null)
|
||
{
|
||
interstitialAd = WXBase.CreateInterstitialAd(new WXCreateInterstitialAdParam()
|
||
{
|
||
adUnitId = adUnitId
|
||
});
|
||
_interstitialDictionary[adUnitId] = interstitialAd;
|
||
|
||
interstitialAd.OnLoad(loadResponse =>
|
||
{
|
||
HCDebugger.LogDebug("[ADManager] [InitInterstitialAd] iv OnLoad, IsIVReady = true, _rewardValue: " +
|
||
loadResponse.rewardValue +
|
||
" , _shareValue : " + loadResponse.shareValue + " , msg : " + loadResponse.errMsg);
|
||
_interstitialStatus[adUnitId] = true;
|
||
|
||
_callback?.Invoke(HCADSstatuscallback.InterstitialLoadSuccess, adUnitId, 0, loadResponse.errMsg, new Dictionary<string, object>
|
||
{
|
||
["type"] = "callback"
|
||
});
|
||
});
|
||
interstitialAd.OnError(errorResponse =>
|
||
{
|
||
HCDebugger.LogDebug("[ADManager] [InitInterstitialAd] iv onError, IsIVReady = false, errorCode: " +
|
||
errorResponse.errCode + " , errorMsg : " +
|
||
errorResponse.errMsg);
|
||
_interstitialStatus[adUnitId] = false;
|
||
_callback?.Invoke(HCADSstatuscallback.InterstitialLoadFailed, adUnitId, errorResponse.errCode, errorResponse.errMsg, new Dictionary<string, object>
|
||
{
|
||
["type"] = "callback"
|
||
});
|
||
});
|
||
interstitialAd.OnClose(() =>
|
||
{
|
||
HCDebugger.LogDebug("[ADManager] [InitInterstitialAd] OnClose");
|
||
|
||
_callback?.Invoke(HCADSstatuscallback.InterstitialClose, adUnitId, 0, "", new Dictionary<string, object>
|
||
{
|
||
["type"] = "callback"
|
||
});
|
||
});
|
||
}
|
||
|
||
interstitialAd.Load(loadResponse =>
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [InterstitialLoad] loadResponse success = {loadResponse.errMsg} {loadResponse.errCode}");
|
||
// _interstitialStatus[adUnitId] = true;
|
||
// _callback?.Invoke(HCADSstatuscallback.InterstitialLoadSuccess, adUnitId, loadResponse.errCode, loadResponse.errMsg, new Dictionary<string, object>
|
||
// {
|
||
// ["type"] = "load"
|
||
// });
|
||
},
|
||
failed =>
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [InterstitialLoad] loadResponse failed = {failed.errMsg} {failed.errCode}");
|
||
_interstitialStatus[adUnitId] = false;
|
||
_callback?.Invoke(HCADSstatuscallback.InterstitialLoadFailed, adUnitId, failed.errCode, failed.errMsg, new Dictionary<string, object>
|
||
{
|
||
["type"] = "load"
|
||
});
|
||
});
|
||
}
|
||
|
||
|
||
public bool IsInterstitialReady(string adUnitId)
|
||
{
|
||
return _interstitialStatus.TryGetValue(adUnitId, out var status) && status;
|
||
}
|
||
|
||
public void ShowInterstitial(string adUnitId)
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [ShowInterstitial] ShowInterstitial - 1 adUnitId : {adUnitId} ");
|
||
if (_interstitialDictionary.TryGetValue(adUnitId, out var interstitialAd))
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [ShowInterstitial] ShowInterstitial - 2 adUnitId : {adUnitId} interstitialAd = {interstitialAd}");
|
||
interstitialAd.Show(response =>
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [ShowInterstitial] response success : {response.errCode} {response.errMsg} _callback = {_callback}");
|
||
|
||
_callback?.Invoke(HCADSstatuscallback.InterstitialShowSuccess, adUnitId, response.errCode, response.errMsg, null);
|
||
},
|
||
failed =>
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [ShowInterstitial] response failed : {failed.errCode} {failed.errMsg} _callback = {_callback}");
|
||
|
||
_callback?.Invoke(HCADSstatuscallback.InterstitialShowFailed, adUnitId, failed.errCode, failed.errMsg, null);
|
||
});
|
||
}
|
||
else
|
||
{
|
||
_callback?.Invoke(HCADSstatuscallback.InterstitialShowFailed, adUnitId, -98, "adUnitId does not exist", null);
|
||
}
|
||
}
|
||
|
||
|
||
#region BannerAd
|
||
|
||
private readonly Dictionary<string, WXBannerAd> _bannerDictionary = new Dictionary<string, WXBannerAd>();
|
||
|
||
public void CreateBanner(string adUnitId)
|
||
{
|
||
_sysInfo = WX.GetSystemInfoSync();
|
||
var wxBannerAd = WXBase.CreateBannerAd(new WXCreateBannerAdParam
|
||
{
|
||
adUnitId = adUnitId,
|
||
adIntervals = 30,
|
||
style = new Style
|
||
{
|
||
left = 0,
|
||
top = (int)_sysInfo.windowHeight - 100,
|
||
width = (int)_sysInfo.windowWidth,
|
||
height = 100
|
||
}
|
||
});
|
||
wxBannerAd.OnError(res =>
|
||
{
|
||
HCDebugger.LogDebug("[ADManager] [InitBanner] bannerad error response, errCode : " + res.errCode +
|
||
" , errMsg : " + res.errMsg);
|
||
|
||
_callback?.Invoke(HCADSstatuscallback.BannerLoadFailed, adUnitId, res.errCode, res.errMsg, null);
|
||
});
|
||
wxBannerAd.OnLoad(res =>
|
||
{
|
||
// TODO callback
|
||
HCDebugger.LogDebug("[ADManager] [InitBanner] bannerad loaded response, rewardValue : " + res.rewardValue +
|
||
" , shareValue : " +
|
||
res.shareValue + " , errMsg : " + res.errMsg);
|
||
|
||
_callback?.Invoke(HCADSstatuscallback.BannerLoadSuccess, adUnitId, 0, res.errMsg, null);
|
||
});
|
||
wxBannerAd.OnResize(res =>
|
||
{
|
||
HCDebugger.LogDebug("[ADManager] [InitBanner] bannerAd on resize, errMsg: " + res.errMsg + " , width : " + res.width +
|
||
" , height : " +
|
||
res.height);
|
||
//拉取的广告可能跟设置的不一样,需要动态调整位置
|
||
wxBannerAd.style.top = (int)_sysInfo.windowHeight - res.height;
|
||
|
||
_callback?.Invoke(HCADSstatuscallback.BannerResize, adUnitId, 0, res.errMsg, null);
|
||
});
|
||
|
||
_bannerDictionary[adUnitId] = wxBannerAd;
|
||
}
|
||
|
||
public void DestroyBanner(string adUnitId)
|
||
{
|
||
if (_bannerDictionary.TryGetValue(adUnitId, out var wxBannerAd))
|
||
{
|
||
wxBannerAd.Destroy();
|
||
|
||
_callback?.Invoke(HCADSstatuscallback.BannerDestroySuccess, adUnitId, 0, "", null);
|
||
}
|
||
else
|
||
{
|
||
_callback?.Invoke(HCADSstatuscallback.BannerDestroyFailed, adUnitId, -97, "adUnitId does not exist", null);
|
||
}
|
||
}
|
||
|
||
|
||
public void ShowBanner(string adUnitId)
|
||
{
|
||
if (_bannerDictionary.TryGetValue(adUnitId, out var wxBannerAd))
|
||
{
|
||
wxBannerAd.Show(success => { _callback?.Invoke(HCADSstatuscallback.BannerShowSuccess, adUnitId, success.errCode, success.errMsg, null); },
|
||
failed => { _callback?.Invoke(HCADSstatuscallback.BannerShowFailed, adUnitId, failed.errCode, failed.errMsg, null); });
|
||
}
|
||
else
|
||
{
|
||
_callback?.Invoke(HCADSstatuscallback.BannerShowFailed, adUnitId, -96, "adUnitId does not exist", null);
|
||
}
|
||
}
|
||
|
||
public void HideBanner(string adUnitId)
|
||
{
|
||
if (_bannerDictionary.TryGetValue(adUnitId, out var wxBannerAd))
|
||
{
|
||
wxBannerAd.Hide();
|
||
_callback?.Invoke(HCADSstatuscallback.BannerHideSuccess, adUnitId, 0, "", null);
|
||
}
|
||
else
|
||
{
|
||
_callback?.Invoke(HCADSstatuscallback.BannerHideFailed, adUnitId, -95, "adUnitId does not exist", null);
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
public void SetLogEnable(bool _enable)
|
||
{
|
||
}
|
||
|
||
public void LoadNative(string adUnit)
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [LoadNative] adUnit = {adUnit}");
|
||
try
|
||
{
|
||
if (!HCTools.HasKey(adUnit)) return;
|
||
var playerPrefsString = HCTools.GetPlayerPrefsString(adUnit);
|
||
var gridAdPositionInfo = JsonUtility.FromJson<GridAdPositionInfo>(playerPrefsString);
|
||
HCDebugger.LogDebug($"[ADManager] [LoadNative] adUnit = {adUnit} playerPrefsString = {playerPrefsString}");
|
||
InitNative(adUnit, gridAdPositionInfo._fixed, gridAdPositionInfo.adUnit, gridAdPositionInfo.left, gridAdPositionInfo.top, gridAdPositionInfo.width, gridAdPositionInfo.height);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
HCDebugger.LogError(e.Message);
|
||
}
|
||
}
|
||
|
||
|
||
// 记录广告已经初始化 key : Position value adId
|
||
private Dictionary<string, string> ShowNativeAdUnit = new Dictionary<string, string>();
|
||
|
||
private static Dictionary<string, NativeCallback> nativeCallbackDictionary = new Dictionary<string, NativeCallback>();
|
||
|
||
// show的时候,未初始化的时候,进行缓存
|
||
private readonly List<string> _cacheAdPosition = new();
|
||
|
||
private readonly HashSet<string> _initAdPosition = new();
|
||
|
||
|
||
public void RemoveNative(string adUnit)
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [RemoveNative] adUnit = {adUnit}");
|
||
if (_cacheAdPosition.Contains(adUnit))
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [RemoveNative] 清除缓存 adUnit = {adUnit}");
|
||
// 移除缓存
|
||
_cacheAdPosition.Remove(adUnit);
|
||
}
|
||
|
||
if (ShowNativeAdUnit.TryGetValue(adUnit, out var showAdUnit))
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [RemoveNative] native 移除 adUnit = {adUnit}");
|
||
HCWxTools.UnityToWxMiniGame(JsonUtility.ToJson(new WxNativeAdEntity
|
||
{
|
||
cmd = "RemoveNative",
|
||
adUnit = showAdUnit,
|
||
adPostion = adUnit
|
||
}));
|
||
}
|
||
else
|
||
{
|
||
HCDebugger.LogError($"[ADManager] [RemoveNative] error adUnit = {adUnit}");
|
||
}
|
||
}
|
||
|
||
public bool IsNativeAdReady(string adPosition)
|
||
{
|
||
// if (!ShowNativeAdUnit.TryGetValue(adPosition, out var showAdUnit))
|
||
// {
|
||
// HCDebugger.LogDebug("[IsNativeAdReady] 对象为空,没有初始化来的,直接给返回True");
|
||
// return true;
|
||
// }
|
||
//
|
||
// var result = HCWxTools.UnityToWxMiniGame(JsonUtility.ToJson(new WxNativeAdEntity
|
||
// {
|
||
// cmd = "isShow",
|
||
// adUnit = showAdUnit,
|
||
// adPostion = adPosition
|
||
// }));
|
||
|
||
var result = _initAdPosition.Contains(adPosition);
|
||
HCDebugger.LogDebug($"[ADManager] [IsNativeAdReady] native ============ IsNativeAdReady : {result}");
|
||
|
||
|
||
return result;
|
||
}
|
||
|
||
// 广告标题的高度
|
||
private const int AdTitleHeight = 21;
|
||
|
||
// 广告格子的高度
|
||
private const int AdHeight = 69;
|
||
private const int AdWidth = 60;
|
||
|
||
public void ShowNative(string adPosition, float x, float y, float width, float height, string pAdPos, Action<int, string> _showFailedCallback = null, Action _closeCallback = null)
|
||
{
|
||
HCAnalyticsManager.Instance.TrackEvent(HCInnerStaticSting.HC_NA_Position, new Dictionary<string, object>
|
||
{
|
||
{ "adPosition", adPosition },
|
||
{ "left", x },
|
||
{ "top", y },
|
||
{ "width", width },
|
||
{ "height", height },
|
||
{ "pAdPos", pAdPos },
|
||
});
|
||
|
||
HCDebugger.LogDebug($"[ADManager] [ShowNative] adPosition {adPosition} x = {x} y = {y} width = {width} height = {height} _showFailedCallback = {_showFailedCallback} _closeCallback = {_closeCallback}");
|
||
try
|
||
{
|
||
// 记录回调
|
||
nativeCallbackDictionary[adPosition] = new NativeCallback
|
||
{
|
||
adUnit = adPosition,
|
||
showFailedCallback = _showFailedCallback,
|
||
closeCallback = _closeCallback
|
||
};
|
||
|
||
|
||
ParameterConversion(adPosition, ref x, ref y, width, height, pAdPos, out var adUnitsBean, out var adViewHeight, out var adViewWidth);
|
||
if (HCTools.HasKey(adPosition))
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [ShowNative]存在缓存,开始对比缓存的数据 adPosition = {adPosition}");
|
||
var playerPrefsString = HCTools.GetPlayerPrefsString(adPosition);
|
||
var gridAdPositionInfo = JsonUtility.FromJson<GridAdPositionInfo>(playerPrefsString);
|
||
if (Math.Abs(x - gridAdPositionInfo.left) > 1 || Math.Abs(y - gridAdPositionInfo.top) > 1 || Math.Abs(adViewWidth - gridAdPositionInfo.width) > 1 || Math.Abs(adViewHeight - gridAdPositionInfo.height) > 1)
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [ShowNative] 位置信息有改变,需要更新 {playerPrefsString} x = {x} y = {y} width = {adViewHeight} height = {adViewWidth}");
|
||
// 这里还可以调用销毁格子广告。
|
||
ShowNativeAdUnit.Remove(adPosition);
|
||
_initAdPosition.Remove(adPosition);
|
||
DestroyNative(adUnitsBean.AdUnit);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
HCDebugger.LogWarning($"[ADManager] [ShowNative] 没有缓存数据 adPosition = {adPosition}");
|
||
}
|
||
|
||
// 已经初始化
|
||
if (ShowNativeAdUnit.TryGetValue(adPosition, out var adUnit))
|
||
{
|
||
if (IsNativeAdReady(adPosition))
|
||
{
|
||
HCAnalyticsManager.Instance.TrackEvent(HCInnerStaticSting.HC_NA_AchieveShow_Match, new Dictionary<string, object>
|
||
{
|
||
{ "adPosition", adPosition },
|
||
{ "left", x },
|
||
{ "top", y },
|
||
{ "width", width },
|
||
{ "height", height },
|
||
{ "pAdPos", pAdPos },
|
||
});
|
||
|
||
HCDebugger.LogDebug($"[ADManager] [ShowNative] start adPosition = {adPosition} AdUnit = {adUnit}");
|
||
// 显示native
|
||
_ShowNative(adUnit);
|
||
}
|
||
else
|
||
{
|
||
HCAnalyticsManager.Instance.TrackEvent(HCInnerStaticSting.HC_NA_ShowFail_Not_Ready, new Dictionary<string, object>
|
||
{
|
||
{ "adPosition", adPosition },
|
||
{ "left", x },
|
||
{ "top", y },
|
||
{ "width", width },
|
||
{ "height", height },
|
||
{ "pAdPos", pAdPos },
|
||
});
|
||
|
||
HCDebugger.LogWarning($"[ADManager] [ShowNative] native 没有缓存 adPosition = {adPosition} AdUnit = {adUnit}");
|
||
nativeCallbackDictionary[adPosition]?.ShowFailedCallback(-102, "没有缓存");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
HCAnalyticsManager.Instance.TrackEvent("NA_AchieveShow_Match_no_cache", new Dictionary<string, object>
|
||
{
|
||
{ "adPosition", adPosition },
|
||
{ "left", x },
|
||
{ "top", y },
|
||
{ "width", width },
|
||
{ "height", height },
|
||
{ "pAdPos", pAdPos },
|
||
});
|
||
|
||
// 解析参数
|
||
var gridAdPositionInfo = new GridAdPositionInfo
|
||
{
|
||
ADPosition = adPosition,
|
||
_fixed = 0,
|
||
adUnit = adUnitsBean.AdUnit,
|
||
left = x,
|
||
top = y,
|
||
width = adViewWidth,
|
||
height = adViewHeight
|
||
};
|
||
|
||
HCTools.SavePlayerPrefsString(adPosition, JsonUtility.ToJson(gridAdPositionInfo));
|
||
_cacheAdPosition.Add(adPosition);
|
||
|
||
HCDebugger.LogDebug($"[ADManager] [ShowNative] start InitNative adPosition = {adPosition} AdUnit = {adUnitsBean.AdUnit} x = {x} y = {y} adViewWidth = {adViewWidth} adViewHeight = {adViewHeight}");
|
||
|
||
// 调用初始化,然后初始化回调中,对sdk判断是否显示
|
||
InitNative(adPosition, 0, adUnitsBean.AdUnit, x, y, adViewWidth, adViewHeight);
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
HCAnalyticsManager.Instance.TrackEvent(HCInnerStaticSting.HC_NA_ShowFail, new Dictionary<string, object>
|
||
{
|
||
{ "adPosition", adPosition },
|
||
{ "left", x },
|
||
{ "top", y },
|
||
{ "width", width },
|
||
{ "height", height },
|
||
{ "pAdPos", pAdPos },
|
||
{ "e", e.Message },
|
||
});
|
||
|
||
nativeCallbackDictionary[adPosition]?.ShowFailedCallback(-100, e.Message);
|
||
nativeCallbackDictionary.Remove(adPosition);
|
||
}
|
||
}
|
||
|
||
private void DestroyNative(string adUnit)
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [DestroyNative] adUnit = {adUnit}");
|
||
// 显示native
|
||
HCWxTools.UnityToWxMiniGame(JsonUtility.ToJson(new WxNativeAdEntity
|
||
{
|
||
cmd = "DestroyNative",
|
||
adUnit = adUnit,
|
||
}));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 真实的广告id
|
||
/// </summary>
|
||
/// <param name="adUnit"></param>
|
||
private void _ShowNative(string adUnit)
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [_ShowNative] _ShowNative adUnit = {adUnit}");
|
||
// 显示native
|
||
HCWxTools.UnityToWxMiniGame(JsonUtility.ToJson(new WxNativeAdEntity
|
||
{
|
||
cmd = "ShowNative",
|
||
adUnit = adUnit,
|
||
}));
|
||
}
|
||
|
||
private void InitNative(string adPosition, int _fixed, string adUnit, float left, float top, int width, int height)
|
||
{
|
||
HCAnalyticsManager.Instance.TrackEvent(HCInnerStaticSting.HC_NA_Start_Load, new Dictionary<string, object>
|
||
{
|
||
{ "adPosition", adPosition },
|
||
{ "_fixed", _fixed },
|
||
{ "adUnit", adUnit },
|
||
{ "left", left },
|
||
{ "top", top },
|
||
{ "width", width },
|
||
{ "height", height },
|
||
});
|
||
|
||
HCDebugger.LogDebug($"[ADManager] [InitNative] adPosition = {adPosition} _fixed = {_fixed} adUnit = {adUnit} left = {left} top = {top} width = {width} height = {height}");
|
||
// 显示native
|
||
HCWxTools.UnityToWxMiniGame(JsonUtility.ToJson(new WxNativeAdEntity
|
||
{
|
||
cmd = "LoadNative",
|
||
adPostion = adPosition,
|
||
_fixed = _fixed,
|
||
adUnit = adUnit,
|
||
left = (int)left,
|
||
top = (int)top,
|
||
width = width,
|
||
height = height
|
||
}));
|
||
}
|
||
|
||
private void ParameterConversion(string adUnit, ref float x, ref float y, float width, float height, string pAdPos, out HCNativeAdUnit adUnitsBean, out int adViewHeight, out int adViewWidth)
|
||
{
|
||
var h = Screen.height;
|
||
var screenHeight = WX.GetSystemInfoSync().screenHeight;
|
||
|
||
|
||
var w = Screen.width;
|
||
var windowHeight = WX.GetSystemInfoSync().windowHeight;
|
||
var screenWidth = WX.GetSystemInfoSync().screenWidth;
|
||
var windowWidth = WX.GetSystemInfoSync().windowWidth;
|
||
|
||
/**
|
||
* 【HCSDK】 [HCWXAds] h : 1334 screenHeight : 667 windowHeight : 667
|
||
* 【HCSDK】 [HCWXAds] w : 750 screenWidth : 375 windowWidth : 375
|
||
*/
|
||
HCDebugger.LogDebug($"[HCWXAds] h : {h} screenHeight : {screenHeight} windowHeight : {windowHeight}");
|
||
HCDebugger.LogDebug($"[HCWXAds] w : {w} screenWidth : {screenWidth} windowWidth : {windowWidth}");
|
||
HCDebugger.LogDebug($"[HCWXAds] adUnit:{adUnit} x :{x} y : {y} width : {width} height : {height} pAdPos : {pAdPos}");
|
||
|
||
|
||
// 0.5
|
||
var scaleSize = WX.GetSystemInfoSync().pixelRatio;
|
||
HCDebugger.LogDebug($"[HCWXAds] scaleSize {scaleSize}");
|
||
|
||
// 真实宽度
|
||
width = (float)(width / scaleSize);
|
||
// 真实高度
|
||
height = (float)(height / scaleSize);
|
||
// 真实x
|
||
x = (float)(x / scaleSize);
|
||
// 真实y
|
||
y = (float)(y / scaleSize);
|
||
|
||
|
||
HCDebugger.LogDebug($"[HCWXAds] scaleSize >>>>>>> x :{x} y : {y} width : {width} height : {height} pAdPos : {pAdPos}");
|
||
|
||
var rows = (int)(height - AdTitleHeight) / AdHeight;
|
||
|
||
HCDebugger.LogDebug($"[HCWXAds] rows = {rows}");
|
||
if (!HCStaticParams.NativeAdUnitIDDictionary.TryGetValue(adUnit, out adUnitsBean))
|
||
{
|
||
HCDebugger.LogError($"[HCWXAds] adUnit {adUnit} 加载失败");
|
||
adViewHeight = 0;
|
||
adViewWidth = 0;
|
||
throw new Exception($"adUnit {adUnit} 加载失败");
|
||
}
|
||
|
||
while (adUnitsBean != null)
|
||
{
|
||
HCDebugger.LogDebug($"[HCWXAds] while >>>>>> rows = {rows} adUnitsBean.Rows = {adUnitsBean.Rows}");
|
||
if (rows >= adUnitsBean.Rows)
|
||
{
|
||
break;
|
||
}
|
||
|
||
adUnitsBean = adUnitsBean.Fit;
|
||
}
|
||
|
||
if (adUnitsBean == null)
|
||
{
|
||
HCDebugger.LogError($"[HCWXAds] adUnit {adUnit} 加载失败 adUnitsBean == null 配置中没有可展示完全的广告id");
|
||
adViewHeight = 0;
|
||
adViewWidth = 0;
|
||
throw new Exception($"adUnit {adUnit} 加载失败 adUnitsBean == null");
|
||
}
|
||
|
||
// var tempWidth = AdWidth;
|
||
// var tempHeight = AdHeight;
|
||
// var tempAdTitleHeight = AdTitleHeight;
|
||
|
||
|
||
// if (adUnitsBean.IsFull)
|
||
// {
|
||
// tempWidth = (int)(screenWidth / adUnitsBean.Columns);
|
||
// HCDebugger.LogDebug($"[HCWXAds] 全屏格子广告 : width {tempWidth}");
|
||
// tempHeight = tempWidth * AdHeight / AdWidth;
|
||
// tempAdTitleHeight = tempWidth * AdHeight / AdTitleHeight;
|
||
// }
|
||
|
||
adViewHeight = adUnitsBean.Rows * AdHeight + AdTitleHeight;
|
||
adViewWidth = adUnitsBean.Columns * AdWidth;
|
||
|
||
if (adUnitsBean.IsFull)
|
||
{
|
||
HCDebugger.LogDebug($"[HCWXAds] 全屏格子广告 原 adViewWidth = {adViewWidth} adViewHeight = {adViewHeight}");
|
||
adViewHeight = adViewHeight * (int)screenWidth / adViewWidth;
|
||
adViewWidth = (int)screenWidth;
|
||
HCDebugger.LogDebug($"[HCWXAds] 全屏格子广告 fix adViewWidth = {adViewWidth} adViewHeight = {adViewHeight}");
|
||
}
|
||
|
||
// ShowNativeAdUnit[adUnit] = adUnitsBean.AdUnit;
|
||
|
||
HCDebugger.LogDebug($"[HCWXAds] [ShowNative] x = {x} y = {y} width = {width} height = {height} adViewHeight = {adViewHeight} adViewWidth = {adViewWidth} {adUnitsBean.Anchor}");
|
||
|
||
switch (adUnitsBean.Anchor)
|
||
{
|
||
case TextAnchor.UpperLeft:
|
||
break;
|
||
case TextAnchor.UpperCenter:
|
||
// 只处理盒子宽度大于广告宽度的情况
|
||
if (adViewWidth < width)
|
||
{
|
||
x += (width - adViewWidth) / 2;
|
||
}
|
||
|
||
break;
|
||
case TextAnchor.UpperRight:
|
||
// 只处理盒子宽度大于广告宽度的情况
|
||
if (adViewWidth < width)
|
||
{
|
||
x += (width - adViewWidth);
|
||
}
|
||
|
||
break;
|
||
case TextAnchor.MiddleLeft:
|
||
y += (height - adViewHeight) / 2;
|
||
break;
|
||
case TextAnchor.MiddleCenter:
|
||
y += (height - adViewHeight) / 2;
|
||
// 只处理盒子宽度大于广告宽度的情况
|
||
if (adViewWidth < width)
|
||
{
|
||
x += (width - adViewWidth) / 2;
|
||
}
|
||
|
||
break;
|
||
case TextAnchor.MiddleRight:
|
||
y += (height - adViewHeight) / 2;
|
||
// 只处理盒子宽度大于广告宽度的情况
|
||
if (adViewWidth < width)
|
||
{
|
||
x += (width - adViewWidth);
|
||
}
|
||
|
||
break;
|
||
case TextAnchor.LowerLeft:
|
||
y += (height - adViewHeight);
|
||
break;
|
||
case TextAnchor.LowerCenter:
|
||
y += (height - adViewHeight);
|
||
// 只处理盒子宽度大于广告宽度的情况
|
||
if (adViewWidth < width)
|
||
{
|
||
x += (width - adViewWidth) / 2;
|
||
}
|
||
|
||
break;
|
||
case TextAnchor.LowerRight:
|
||
y += (height - adViewHeight);
|
||
|
||
// 只处理盒子宽度大于广告宽度的情况
|
||
if (adViewWidth < width)
|
||
{
|
||
x += (width - adViewWidth);
|
||
}
|
||
|
||
break;
|
||
default:
|
||
throw new ArgumentOutOfRangeException();
|
||
}
|
||
|
||
HCDebugger.LogDebug($"[HCWXAds] [ShowNative] x = {x} y = {y} width = {width} height = {height} adViewHeight = {adViewHeight} adViewWidth = {adViewWidth} {adUnitsBean.Anchor}");
|
||
// y += (int)WX.GetSystemInfoSync().statusBarHeight;
|
||
|
||
if (adUnitsBean.Rows != 1)
|
||
{
|
||
y -= 30;
|
||
}
|
||
|
||
if (adUnitsBean.Rows == 1 && adUnitsBean.Anchor is TextAnchor.LowerCenter or TextAnchor.LowerLeft or TextAnchor.LowerRight)
|
||
{
|
||
y += 5;
|
||
}
|
||
|
||
if (width >= adViewWidth && !adUnitsBean.IsFull && adUnitsBean.Anchor is TextAnchor.LowerRight or TextAnchor.MiddleRight or TextAnchor.UpperRight or TextAnchor.LowerCenter or TextAnchor.MiddleCenter or TextAnchor.UpperCenter)
|
||
{
|
||
x += adUnitsBean.Rows == 1 ? 10 : 5;
|
||
}
|
||
|
||
if (y <= 0)
|
||
{
|
||
y = 1;
|
||
}
|
||
|
||
if (x <= 0)
|
||
{
|
||
x = 1;
|
||
}
|
||
|
||
HCDebugger.LogDebug($"[HCWXAds] [ShowNative] x = {x} y = {y} width = {width} height = {height} adViewHeight = {adViewHeight} adViewWidth = {adViewWidth} {adUnitsBean.Anchor}");
|
||
}
|
||
|
||
|
||
public class NativeCallback
|
||
{
|
||
public string adUnit;
|
||
public Action<int, string> showFailedCallback = null;
|
||
public Action closeCallback = null;
|
||
|
||
public void ShowFailedCallback(int code, string msg)
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [NativeCallback] [ShowFailedCallback] code = {code} msg = {msg}");
|
||
showFailedCallback?.Invoke(code, msg);
|
||
nativeCallbackDictionary.Remove(adUnit);
|
||
}
|
||
|
||
public void CloseCallback()
|
||
{
|
||
HCDebugger.LogDebug($"[ADManager] [NativeCallback] [CloseCallback] closeCallback={closeCallback}");
|
||
closeCallback?.Invoke();
|
||
nativeCallbackDictionary.Remove(adUnit);
|
||
}
|
||
}
|
||
|
||
public class GridAdPositionInfo
|
||
{
|
||
public string ADPosition;
|
||
public int _fixed;
|
||
public string adUnit;
|
||
public float left;
|
||
public float top;
|
||
public int width;
|
||
public int height;
|
||
}
|
||
|
||
private void WxCallUnity(string cmd, string content)
|
||
{
|
||
var wxNativeCallback = JsonUtility.FromJson<WxNativeCallback>(content);
|
||
HCDebugger.LogDebug($"[ADManager] [WxCallUnity] 收到wx的回调 cmd = {cmd} content = {content}");
|
||
|
||
switch (cmd)
|
||
{
|
||
// 加载失败
|
||
case "onError":
|
||
HCAnalyticsManager.Instance.TrackEvent(HCInnerStaticSting.HC_NA_Fail_Loaded, new Dictionary<string, object>
|
||
{
|
||
{ "adPosition", wxNativeCallback.adPostion },
|
||
{ "msg", wxNativeCallback.msg },
|
||
{ "cmd", wxNativeCallback.cmd },
|
||
{ "adUnit", wxNativeCallback.adUnit },
|
||
{ "code", wxNativeCallback.code },
|
||
});
|
||
|
||
_initAdPosition.Remove(wxNativeCallback.adPostion);
|
||
HCDebugger.LogDebug($" [WxCallUnity] [onLoad] 收到wx的回调 wxNativeCallback.adPostion = {wxNativeCallback.adPostion} {_cacheAdPosition.Contains(wxNativeCallback.adPostion)}");
|
||
// load 成功,同时还有缓存,那么给他显示一下
|
||
if (_cacheAdPosition.Contains(wxNativeCallback.adPostion))
|
||
{
|
||
if (nativeCallbackDictionary.TryGetValue(wxNativeCallback.adPostion, out var callback3))
|
||
{
|
||
callback3.ShowFailedCallback(wxNativeCallback.code, wxNativeCallback.msg);
|
||
}
|
||
}
|
||
|
||
_cacheAdPosition.Remove(wxNativeCallback.adPostion);
|
||
break;
|
||
// 加载成功
|
||
case "onLoad":
|
||
HCAnalyticsManager.Instance.TrackEvent(HCInnerStaticSting.HC_NA_Loaded, new Dictionary<string, object>
|
||
{
|
||
{ "adPosition", wxNativeCallback.adPostion },
|
||
{ "msg", wxNativeCallback.msg },
|
||
{ "cmd", wxNativeCallback.cmd },
|
||
{ "adUnit", wxNativeCallback.adUnit },
|
||
{ "code", wxNativeCallback.code },
|
||
});
|
||
|
||
_initAdPosition.Add(wxNativeCallback.adPostion);
|
||
|
||
HCDebugger.LogDebug($" [WxCallUnity] [onLoad] 收到wx的回调 wxNativeCallback.adPostion = {wxNativeCallback.adPostion} {_cacheAdPosition.Contains(wxNativeCallback.adPostion)}");
|
||
// load 成功,同时还有缓存,那么给他显示一下
|
||
if (_cacheAdPosition.Contains(wxNativeCallback.adPostion))
|
||
{
|
||
if (ShowNativeAdUnit.TryGetValue(wxNativeCallback.adPostion, out var temp))
|
||
{
|
||
_ShowNative(temp);
|
||
}
|
||
}
|
||
|
||
_cacheAdPosition.Remove(wxNativeCallback.adPostion);
|
||
|
||
break;
|
||
// 关闭
|
||
case "onHide":
|
||
case "onClose":
|
||
HCDebugger.LogDebug($"关闭回调 cache : {_cacheAdPosition.Contains(wxNativeCallback.adPostion)} close : {nativeCallbackDictionary.ContainsKey(wxNativeCallback.adPostion)}");
|
||
if (_cacheAdPosition.Contains(wxNativeCallback.adPostion))
|
||
{
|
||
// 移除缓存
|
||
_cacheAdPosition.Remove(wxNativeCallback.adPostion);
|
||
}
|
||
|
||
if (nativeCallbackDictionary.TryGetValue(wxNativeCallback.adPostion, out var callback))
|
||
{
|
||
callback.CloseCallback();
|
||
}
|
||
|
||
if (cmd.Equals("onClose"))
|
||
{
|
||
var remove1 = ShowNativeAdUnit.Remove(wxNativeCallback.adPostion);
|
||
var remove2 = _initAdPosition.Remove(wxNativeCallback.adPostion);
|
||
HCDebugger.LogDebug($"[WxCallUnity] [onClose] adPosition = {wxNativeCallback.adPostion} remove1 = {remove1} remove2 = {remove2}");
|
||
|
||
LoadNative(wxNativeCallback.adPostion);
|
||
}
|
||
|
||
HCAnalyticsManager.Instance.TrackEvent("NA_Close", new Dictionary<string, object>
|
||
{
|
||
{ "adPosition", wxNativeCallback.adPostion },
|
||
{ "msg", wxNativeCallback.msg },
|
||
{ "cmd", wxNativeCallback.cmd },
|
||
{ "adUnit", wxNativeCallback.adUnit },
|
||
{ "code", wxNativeCallback.code },
|
||
});
|
||
|
||
break;
|
||
// 显示成功
|
||
case "onShowSuccess":
|
||
HCAnalyticsManager.Instance.TrackEvent(HCInnerStaticSting.HC_NA_Show, new Dictionary<string, object>
|
||
{
|
||
{ "adPosition", wxNativeCallback.adPostion },
|
||
{ "msg", wxNativeCallback.msg },
|
||
{ "cmd", wxNativeCallback.cmd },
|
||
{ "adUnit", wxNativeCallback.adUnit },
|
||
{ "code", wxNativeCallback.code },
|
||
});
|
||
break;
|
||
// 显示失败
|
||
case "onShowFail":
|
||
var adShowFailPosition = wxNativeCallback.GetAdUnit(ShowNativeAdUnit);
|
||
if (nativeCallbackDictionary.TryGetValue(adShowFailPosition, out var callback2))
|
||
{
|
||
callback2.ShowFailedCallback(wxNativeCallback.code, wxNativeCallback.msg);
|
||
}
|
||
HCAnalyticsManager.Instance.TrackEvent(HCInnerStaticSting.HC_NA_ShowFail, new Dictionary<string, object>
|
||
{
|
||
{ "adPosition", wxNativeCallback.adPostion },
|
||
{ "msg", wxNativeCallback.msg },
|
||
{ "cmd", wxNativeCallback.cmd },
|
||
{ "adUnit", wxNativeCallback.adUnit },
|
||
{ "code", wxNativeCallback.code },
|
||
});
|
||
|
||
break;
|
||
case "onHideSuccess":
|
||
if (nativeCallbackDictionary.TryGetValue(wxNativeCallback.adPostion, out var callback6))
|
||
{
|
||
callback6.CloseCallback();
|
||
}
|
||
|
||
HCAnalyticsManager.Instance.TrackEvent("NA_onHideSuccess", new Dictionary<string, object>
|
||
{
|
||
{ "adPosition", wxNativeCallback.adPostion },
|
||
{ "msg", wxNativeCallback.msg },
|
||
{ "cmd", wxNativeCallback.cmd },
|
||
{ "adUnit", wxNativeCallback.adUnit },
|
||
{ "code", wxNativeCallback.code },
|
||
});
|
||
break;
|
||
case "onHideFail":
|
||
HCAnalyticsManager.Instance.TrackEvent("NA_onHideFail", new Dictionary<string, object>
|
||
{
|
||
{ "adPosition", wxNativeCallback.adPostion },
|
||
{ "msg", wxNativeCallback.msg },
|
||
{ "cmd", wxNativeCallback.cmd },
|
||
{ "adUnit", wxNativeCallback.adUnit },
|
||
{ "code", wxNativeCallback.code },
|
||
});
|
||
break;
|
||
case "newNativeAdObject":
|
||
ShowNativeAdUnit[wxNativeCallback.adPostion] = wxNativeCallback.adUnit;
|
||
break;
|
||
case "onDestroy":
|
||
HCAnalyticsManager.Instance.TrackEvent("NA_onDestroy", new Dictionary<string, object>
|
||
{
|
||
{ "adPosition", wxNativeCallback.adPostion },
|
||
{ "msg", wxNativeCallback.msg },
|
||
{ "cmd", wxNativeCallback.cmd },
|
||
{ "adUnit", wxNativeCallback.adUnit },
|
||
{ "code", wxNativeCallback.code },
|
||
});
|
||
var adShowFailPosition2 = wxNativeCallback.GetAdUnit(ShowNativeAdUnit);
|
||
if (!string.IsNullOrEmpty(adShowFailPosition2))
|
||
{
|
||
ShowNativeAdUnit.Remove(adShowFailPosition2);
|
||
_initAdPosition.Remove(adShowFailPosition2);
|
||
}
|
||
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
#endif |