把下载下来的maxnativelibrary-release-文件放在Plugins/Android下
将这一行加入到mainTemplate.gradle文件中
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
添加下面的两个脚本
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NativeSDKManager : MonoBehaviour
{
public static NativeSDKManager Instance;
#region IOSMethod
#if UNITY_IOS
[DllImport("__Internal")]
internal extern static void IOSInitMax();
[DllImport("__Internal")]
internal extern static void IOSLoadNativeAd(string adId);
[DllImport("__Internal")]
internal extern static void IOSShowAd(string adId, int cd, bool hasRemoveAds);
#endif
#endregion
AndroidJavaClass androidJava;
AndroidJavaObject androidJavaObject;
//public MaxPriceData NativePrice;
//public MaxPriceData NativeNormalPrice;
private MaxPriceData MaxInterPrice;
private MaxPriceData MaxInterPrice2;
private MaxPriceData MaxRewardPrice;
private MaxPriceData NativePrice_1;
private MaxPriceData NativePrice_2;
private MaxPriceData NativePrice_3;
private MaxPriceData NativePrice_4;
private MaxPriceData NativePrice_5;
private MaxPriceData NativePrice_6;
private MaxPriceData NativePrice_7;
private MaxPriceData NativePrice_8;
private MaxPriceData NativePrice_9;
private MaxPriceData NativePrice_10;
#if UNITY_ANDROID
//public const string adUnitId = "id";
//public const string priceAdUnitId = "id";
private const string priceADUnitId_1 = "id";
private const string priceADUnitId_2 = "id";
private const string priceADUnitId_3 = "id";
private const string priceADUnitId_4 = "id";
private const string priceADUnitId_5 = "id";
private const string priceADUnitId_6 = "id";
private const string priceADUnitId_7 = "id";
private const string priceADUnitId_8 = "id";
private const string priceADUnitId_9 = "id";
private const string priceADUnitId_10 = "id";
#elif UNITY_IOS
public const string adUnitId = "id";
public const string priceAdUnitId = "id";
#endif
private bool m_useOnlyNative = false;
void Awake()
{
DontDestroyOnLoad(gameObject);
Instance = this;
transform.name = "NativeSDK";
#if UNITY_ANDROID
androidJava = new AndroidJavaClass("com.android.unityad.maxnativelibrary.AndroidUnityConnector");
#elif UNITY_IOS && !UNITY_EDITOR
IOSInitMax();
#endif
MaxInterPrice = new MaxPriceData(AdMaxType.Interstitial_android, string.Empty, "Max插屏广告id");
MaxInterPrice2 = new MaxPriceData(AdMaxType.Interstitial_android_new, string.Empty, "Max插屏广告2id");
MaxRewardPrice = new MaxPriceData(AdMaxType.Reward_Android, string.Empty, "Max激励广告id");
NativePrice_1 = new MaxPriceData(priceADUnitId_1, String.Empty, "原生比价id1", true);
NativePrice_2 = new MaxPriceData(priceADUnitId_2, String.Empty, "原生比价id2", true);
NativePrice_3 = new MaxPriceData(priceADUnitId_3, String.Empty, "原生比价id3", true);
NativePrice_4 = new MaxPriceData(priceADUnitId_4, String.Empty, "原生比价id4", true);
NativePrice_5 = new MaxPriceData(priceADUnitId_5, String.Empty, "原生比价id5", true);
NativePrice_6 = new MaxPriceData(priceADUnitId_6, String.Empty, "原生比价id6", true);
NativePrice_7 = new MaxPriceData(priceADUnitId_7, String.Empty, "原生比价id7", true);
NativePrice_8 = new MaxPriceData(priceADUnitId_8, String.Empty, "原生比价id8", true);
NativePrice_9 = new MaxPriceData(priceADUnitId_9, String.Empty, "原生比价id9", true);
NativePrice_10 = new MaxPriceData(priceADUnitId_10, String.Empty, "原生比价id10", true);
}
void Start()
{
InitMax();
}
//ToAndroid
public void InitMax()
{
#if UNITY_ANDROID
Debug.Log(GlobalFunc.NativeFlag + "NativeSDKManager Call initNativeAd");
androidJava.CallStatic("initMaxNativeAd");
cacheNativeVideo();
Debug.Log(GlobalFunc.NativeFlag + "NativeSDK Init Succeed");
#endif
}
#region 请求广告
public bool adRequest = false;
public bool priceAdRequest = false;
private void RequestPriceAd_1()
{
RequestPriceAd(priceADUnitId_1);
}
private void RequestPriceAd_2()
{
RequestPriceAd(priceADUnitId_2);
}
private void RequestPriceAd_3()
{
RequestPriceAd(priceADUnitId_3);
}
private void RequestPriceAd_4()
{
RequestPriceAd(priceADUnitId_4);
}
private void RequestPriceAd_5()
{
RequestPriceAd(priceADUnitId_5);
}
private void RequestPriceAd_6()
{
RequestPriceAd(priceADUnitId_6);
}
private void RequestPriceAd_7()
{
RequestPriceAd(priceADUnitId_7);
}
private void RequestPriceAd_8()
{
RequestPriceAd(priceADUnitId_8);
}
private void RequestPriceAd_9()
{
RequestPriceAd(priceADUnitId_9);
}
private void RequestPriceAd_10()
{
RequestPriceAd(priceADUnitId_10);
}
private void RequestPriceAd(string unitId)
{
Debug.Log(GlobalFunc.NativeFlag + "RequestAdLoad Start" + unitId);
AnalyticDataHelper.Track("native_request", new Dictionary<string, object>()
{
{"ad_unit_id", unitId},
});
#if UNITY_ANDROID
androidJava.CallStatic("loadNativeAd", unitId);
#elif UNITY_IOS && !UNITY_EDITOR
IOSLoadNativeAd(priceAdUnitId);
#endif
}
public void cacheNativeVideo()
{
BuryingPointMgr.Instance.SendShuShuAndFireBaseEvent(Event_DataType.ads_request,new Dictionary<string, object>()
{
{"ad_type","native"}
});
if (NativePrice_1.IsNullPrice())
{
RequestPriceAd_1();
}
if (NativePrice_2.IsNullPrice())
{
RequestPriceAd_2();
}
if (NativePrice_3.IsNullPrice())
{
RequestPriceAd_3();
}
if (NativePrice_4.IsNullPrice())
{
RequestPriceAd_4();
}
if (NativePrice_5.IsNullPrice())
{
RequestPriceAd_5();
}
if (NativePrice_6.IsNullPrice())
{
RequestPriceAd_6();
}
if (NativePrice_7.IsNullPrice())
{
RequestPriceAd_7();
}
if (NativePrice_8.IsNullPrice())
{
RequestPriceAd_8();
}
if (NativePrice_9.IsNullPrice())
{
RequestPriceAd_9();
}
if (NativePrice_10.IsNullPrice())
{
RequestPriceAd_10();
}
}
#endregion
enum AdLoadStatus
{
Idle,
Loading,
Done,
}
public enum AdType
{
Normal,
Price,
Price_1,
Price_2,
Price_3,
Price_4,
Price_5,
Price_6,
Price_7,
Price_8,
Price_9,
Price_10,
}
private Action SuccCallback;
private AdLoadStatus adLoadStatus = AdLoadStatus.Idle;
public bool IsShow;
public string NativeType = "native";
/// <summary>
/// 展示Native 唯一方法
/// </summary>
/// <param name="adUnitId"></param>
/// <param name="nativeType"></param>
/// <param name="failCallback"></param>
/// <param name="succCallback"></param>
public void ShowNativeAd(string adUnitId, string nativeType = "adi", Action failCallback = null,
Action succCallback = null)
{
GlobalFunc.SetOpenAdWaitTime(60);
Debug.Log(GlobalFunc.NativeFlag + "RequestAdLoad Start is In" + adUnitId);
if (IsShow) //如果正在展示还没关 就跳过
{
return;
}
adLoadStatus = AdLoadStatus.Loading;
// if (adLoadStatus == AdLoadStatus.Loading)
// {
// return;
// }
SuccCallback = succCallback;
NativeType = nativeType;
Debug.Log(GlobalFunc.NativeFlag + "ShowNativeAd Start");
#if UNITY_ANDROID
androidJava.CallStatic("showAd", adUnitId, UIConstDataManger.Instance.NativeShowTime);
#elif UNITY_IOS && !UNITY_EDITOR
IOSShowAd(adUnitId,DataManager.NativeDisplayCD,false);
#endif
Debug.Log(GlobalFunc.NativeFlag + "ShowNativeAd Succeed" + adUnitId);
}
#region 回调约定error string: id*errorcode*error----------其他的:revenue*network*id------------------------------------------------------------------------------------
public void AdRemoveCallBack(string str)
{
Debug.Log(GlobalFunc.NativeFlag + "Receive Native AdRemoveCallBack" + str);
string[] strs = str.Split('*');
AnalyticDataHelper.Track("adremove_click", new Dictionary<string, object>()
{
{"page", "native"},
});
// UIRoot.Instance.RemoveAds();
}
public void AdClickedCallBack(string str)
{
Debug.Log(GlobalFunc.NativeFlag + "Receive Native AdClickedCallBack" + str);
string[] strs = str.Split('*');
string ad_position = "";
if (m_useOnlyNative)
{
ad_position = "native";
}
else
{
if (m_useNativeInsteadAdv)
{
ad_position = "adv";
}
else
{
ad_position = "adi";
}
}
AnalyticDataHelper.Track("native_click", new Dictionary<string, object>()
{
{"page", WindowManager.ActiveWindow},
{"event_situation", WindowManager.ActiveWindow},
{"ad_position", ad_position},
{"isNative", true},
{"ad_unit_id", strs[2]},
{"network", strs[1]}
});
}
private int retryNative1Attempt = 0;
private int retryNative2Attempt = 0;
public void AdRequestErrorCallBack(string str)
{
Debug.Log(GlobalFunc.NativeFlag + "Receive Native AdRequestErrorCallBack" + str);
AdsManager.instance.mAdPlayState = AdPlayState.FAIL;
string[] strs = str.Split('*');
if (strs[0].Equals(priceADUnitId_1))
{
NativePrice_1.ResetPrice();
}
else if (strs[0].Equals(priceADUnitId_2))
{
NativePrice_2.ResetPrice();
}
else if (strs[0].Equals(priceADUnitId_3))
{
NativePrice_3.ResetPrice();
}
else if (strs[0].Equals(priceADUnitId_4))
{
NativePrice_4.ResetPrice();
}
else if (strs[0].Equals(priceADUnitId_5))
{
NativePrice_5.ResetPrice();
}
else if (strs[0].Equals(priceADUnitId_6))
{
NativePrice_6.ResetPrice();
}
else if (strs[0].Equals(priceADUnitId_7))
{
NativePrice_7.ResetPrice();
}
else if (strs[0].Equals(priceADUnitId_8))
{
NativePrice_8.ResetPrice();
}
else if (strs[0].Equals(priceADUnitId_9))
{
NativePrice_9.ResetPrice();
}
else if (strs[0].Equals(priceADUnitId_10))
{
NativePrice_10.ResetPrice();
}
else
{
//NativePrice.ResetPrice();
double retryDelay = Mathf.Pow(2, retryNative2Attempt);
retryNative2Attempt++;
if (retryDelay > 64)
{
retryDelay = 0;
retryNative2Attempt = 0;
}
priceAdRequest = false;
// Invoke("RequestPriceAdLoad", (float) retryDelay);
}
}
//FromAndroid
private int loadCount = 0;
private int showCount = 0;
private int requestCount = 0;
public void AdRequestLoadedCallBack(string str)
{
Debug.Log(GlobalFunc.NativeFlag + "Receive Native AdRequestLoadedCallBack CallBack" + str);
string[] strs = str.Split('*');
if (!string.IsNullOrEmpty(strs[0]))
{
AdType adType = AdType.Normal;
if (!string.IsNullOrEmpty(strs[2]))
{
if (strs[2].Equals(priceADUnitId_1))
{
adType = AdType.Price_1;
}
else if (strs[2].Equals(priceADUnitId_2))
{
adType = AdType.Price_2;
}
else if (strs[2].Equals(priceADUnitId_3))
{
adType = AdType.Price_3;
}
else if (strs[2].Equals(priceADUnitId_4))
{
adType = AdType.Price_4;
}
else if (strs[2].Equals(priceADUnitId_5))
{
adType = AdType.Price_5;
}
else if (strs[2].Equals(priceADUnitId_6))
{
adType = AdType.Price_6;
}
else if (strs[2].Equals(priceADUnitId_7))
{
adType = AdType.Price_7;
}
else if (strs[2].Equals(priceADUnitId_8))
{
adType = AdType.Price_8;
}
else if (strs[2].Equals(priceADUnitId_9))
{
adType = AdType.Price_9;
}
else if (strs[2].Equals(priceADUnitId_10))
{
adType = AdType.Price_10;
}
}
AnalyticDataHelper.Track("native_loaded", new Dictionary<string, object>()
{
{"ad_unit_id", strs[2]},
});
if (adType == AdType.Price_1)
{
NativePrice_1.Price = strs[0];
priceAdRequest = false;
}
else if (adType == AdType.Price_2)
{
NativePrice_2.Price = strs[0];
priceAdRequest = false;
}
else if (adType == AdType.Price_3)
{
NativePrice_3.Price = strs[0];
priceAdRequest = false;
}
else if (adType == AdType.Price_4)
{
NativePrice_4.Price = strs[0];
priceAdRequest = false;
}
else if (adType == AdType.Price_5)
{
NativePrice_5.Price = strs[0];
priceAdRequest = false;
}
else if (adType == AdType.Price_6)
{
NativePrice_6.Price = strs[0];
priceAdRequest = false;
}
else if (adType == AdType.Price_7)
{
NativePrice_7.Price = strs[0];
priceAdRequest = false;
}
else if (adType == AdType.Price_8)
{
NativePrice_8.Price = strs[0];
priceAdRequest = false;
}
else if (adType == AdType.Price_9)
{
NativePrice_9.Price = strs[0];
priceAdRequest = false;
}
else if (adType == AdType.Price_10)
{
NativePrice_10.Price = strs[0];
priceAdRequest = false;
}
BuryingPointMgr.Instance.SendShuShuAndFireBaseEvent(Event_DataType.ads_load_success,new Dictionary<string, object>()
{
{"ad_type","native"}
});
Debug.Log($"{GlobalFunc.NativeFlag} 拿到: {adType},价格:{strs[0]}");
}
}
private System.DateTime mLastNativeTime;
public void AdShowCallBack(string str)
{
IsShow = true;
// #if UNITY_ANDROID
//NativeSDK AdShowCallBack price
Debug.Log(GlobalFunc.NativeFlag + "Receive Native AdShowCallBack CallBack" + str);
// (AdsManager.instance.mAdNetwork as AdMax).NativeDisplay();
AdsManager.instance.mAdPlayState = AdPlayState.Load;
string[] strs = str.Split('*');
ADRevenue adRevenue = new ADRevenue
{
AdFormat = "native",
Revenue = double.Parse(strs[0]),
NetworkName = strs[1],
AdUnitIdentifier = strs[2]
};
(AdsManager.instance.mAdNetwork as AdMax).OnNativeAdPaidEvent("", adRevenue);
// AnalyticHelper.FireEvent(Event_Type.adi_show_success, new Dictionary<string, object>() {
// {"ad_position",WindowManager.ActiveWindow }
// });
GlobalFunc.SetOpenAdWaitTime(60);
string ad_position = "";
if (m_useOnlyNative)
{
ad_position = "native";
}
else
{
if (m_useNativeInsteadAdv)
{
ad_position = "adv";
}
else
{
ad_position = "adi";
}
}
BuryingPointMgr.Instance.SendShuShuAndFireBaseEvent(Event_DataType.ads_show_success,new Dictionary<string, object>()
{
{"ad_type","native"},
{"source",AdsManager.instance.SourceType.ToString()},
{"net_work",strs[1]},
{"position", ad_position},
{"isNative", true},
{"ad_unit_id", strs[2]},
{"type", NativeType}
});
AnalyticDataHelper.Track("native_show_success", new Dictionary<string, object>()
{
{"page", WindowManager.ActiveWindow},
{"event_situation", WindowManager.ActiveWindow},
{"ad_position", ad_position},
{"position", ad_position},
{"isNative", true},
{"ad_unit_id", strs[2]},
{"type", NativeType},
{"network", strs[1]}
});
showCount++;
Debug.Log(GlobalFunc.NativeFlag + "Times Show:" + showCount);
// #endif
}
public void AdCloseCallBack(string str)
{
IsShow = false;
// #if UNITY_ANDROID
Debug.Log(GlobalFunc.NativeFlag + "Receive Native AdCloseCallBack" + str);
adLoadStatus = AdLoadStatus.Done;
AdsManager.instance.mAdPlayState = AdPlayState.CLOSE;
if (NativeType.Equals("adi"))
{
AdsManager.instance.m_LastIntersTime = System.DateTime.Now;
}
else if (NativeType.Equals("native"))
{
mLastNativeTime = System.DateTime.Now;
}
SuccCallback?.Invoke();
Debug.Log(GlobalFunc.NativeFlag + "mLastIntersTime=" + AdsManager.instance.m_LastIntersTime);
GlobalFunc.SetOpenAdWaitTime(0);
string[] strs = str.Split('*');
Debug.Log(strs);
if (strs[1].Equals(priceADUnitId_1))
{
NativePrice_1.ResetPrice();
RequestPriceAd_1();
}
else if (strs[1].Equals(priceADUnitId_2))
{
NativePrice_2.ResetPrice();
RequestPriceAd_2();
}
else if (strs[1].Equals(priceADUnitId_3))
{
NativePrice_3.ResetPrice();
RequestPriceAd_3();
}
else if (strs[1].Equals(priceADUnitId_4))
{
NativePrice_4.ResetPrice();
RequestPriceAd_4();
}
else if (strs[1].Equals(priceADUnitId_5))
{
NativePrice_5.ResetPrice();
RequestPriceAd_5();
}
else if (strs[1].Equals(priceADUnitId_6))
{
NativePrice_6.ResetPrice();
RequestPriceAd_6();
}
else if (strs[1].Equals(priceADUnitId_7))
{
NativePrice_7.ResetPrice();
RequestPriceAd_7();
}
else if (strs[1].Equals(priceADUnitId_8))
{
NativePrice_8.ResetPrice();
RequestPriceAd_8();
}
else if (strs[1].Equals(priceADUnitId_9))
{
NativePrice_9.ResetPrice();
RequestPriceAd_9();
}
else if (strs[1].Equals(priceADUnitId_10))
{
NativePrice_10.ResetPrice();
RequestPriceAd_10();
}
BuryingPointMgr.Instance.SendShuShuAndFireBaseEvent(Event_DataType.ads_request,new Dictionary<string, object>()
{
{"ad_type","native"}
});
Debug.LogError(GlobalFunc.NativeFlag + "广告关闭获取奖励" + m_useNativeInsteadAdv);
if (m_useNativeInsteadAdv)
{
// AdsManager.instance.RewardedVideoAdDidClose();
// AdsManager.instance.RewardedVideoAdDidGiven(strs[1]);
m_useNativeInsteadAdv = false;
}
else
{
// AdsManager.instance.InterstitialAdClosedEvent();
}
//GlobalFunc.OnNativeClose(strs[1]);
}
#endregion
private List<MaxPriceData> ComparePrice = new List<MaxPriceData>(12);
public MaxPriceData
NativeSDKPriceHigh(bool useInter = false, bool useReward = false) //useInter:插屏,useReward激励//都为False为native
{
m_useNativeInsteadAdv = useReward;
ComparePrice.Clear();
//bool isHigh = false;
if (!useInter && !useReward)
{
m_useOnlyNative = true;
}
else
{
m_useOnlyNative = false;
}
if (CheckRuByIp()) //‒ RU地区 adi双ID+Native10条ID 12个ID比价,展示最高价ID(这个ID是adi就播adi,是Naitve就播Naitve)‒ adv同理
{
if (useInter)
{
if (!MaxInterPrice.IsNullPrice())
{
ComparePrice.Add(MaxInterPrice);
Debug.Log($"{GlobalFunc.NativeFlag} 添加比价: {MaxInterPrice.Flag},价格:{MaxInterPrice.Price}");
}
if (!MaxInterPrice2.IsNullPrice())
{
ComparePrice.Add(MaxInterPrice2);
Debug.Log($"{GlobalFunc.NativeFlag} 添加比价: {MaxInterPrice2.Flag},价格:{MaxInterPrice2.Price}");
}
}
if (useReward)
{
if (!MaxRewardPrice.IsNullPrice())
{
ComparePrice.Add(MaxRewardPrice);
Debug.Log($"{GlobalFunc.NativeFlag} 添加比价: {MaxRewardPrice.Flag},价格:{MaxRewardPrice.Price}");
}
}
}
if (!this.NativePrice_1.IsNullPrice())
{
ComparePrice.Add(this.NativePrice_1);
Debug.Log($"{GlobalFunc.NativeFlag} 添加比价: {NativePrice_1.Flag},价格:{NativePrice_1.Price}");
}
if (!this.NativePrice_2.IsNullPrice())
{
ComparePrice.Add(this.NativePrice_2);
Debug.Log($"{GlobalFunc.NativeFlag} 添加比价: {NativePrice_2.Flag},价格:{NativePrice_2.Price}");
}
if (!this.NativePrice_3.IsNullPrice())
{
ComparePrice.Add(this.NativePrice_3);
Debug.Log($"{GlobalFunc.NativeFlag} 添加比价: {NativePrice_3.Flag},价格:{NativePrice_3.Price}");
}
if (!this.NativePrice_4.IsNullPrice())
{
ComparePrice.Add(this.NativePrice_4);
Debug.Log($"{GlobalFunc.NativeFlag} 添加比价: {NativePrice_4.Flag},价格:{NativePrice_4.Price}");
}
if (!this.NativePrice_5.IsNullPrice())
{
ComparePrice.Add(this.NativePrice_5);
Debug.Log($"{GlobalFunc.NativeFlag} 添加比价: {NativePrice_5.Flag},价格:{NativePrice_5.Price}");
}
if (!this.NativePrice_6.IsNullPrice())
{
ComparePrice.Add(this.NativePrice_6);
Debug.Log($"{GlobalFunc.NativeFlag} 添加比价: {NativePrice_6.Flag},价格:{NativePrice_6.Price}");
}
if (!this.NativePrice_7.IsNullPrice())
{
ComparePrice.Add(this.NativePrice_7);
Debug.Log($"{GlobalFunc.NativeFlag} 添加比价: {NativePrice_7.Flag},价格:{NativePrice_7.Price}");
}
if (!this.NativePrice_8.IsNullPrice())
{
ComparePrice.Add(this.NativePrice_8);
Debug.Log($"{GlobalFunc.NativeFlag} 添加比价: {NativePrice_8.Flag},价格:{NativePrice_8.Price}");
}
if (!this.NativePrice_9.IsNullPrice())
{
ComparePrice.Add(this.NativePrice_9);
Debug.Log($"{GlobalFunc.NativeFlag} 添加比价: {NativePrice_9.Flag},价格:{NativePrice_9.Price}");
}
if (!this.NativePrice_10.IsNullPrice())
{
ComparePrice.Add(this.NativePrice_10);
Debug.Log($"{GlobalFunc.NativeFlag} 添加比价: {NativePrice_10.Flag},价格:{NativePrice_10.Price}");
}
MaxPriceData data = null;
if (ComparePrice.Count > 0)
{
ComparePrice.Sort((x, y) => x.CompareTo(y));
data = ComparePrice[0];
}
if (data != null)
{
if (data.Native)
{
string ad_position = "";
if (m_useOnlyNative)
{
ad_position = "native";
}
else
{
if (m_useNativeInsteadAdv)
{
ad_position = "adv";
}
else
{
ad_position = "adi";
}
}
AnalyticDataHelper.Track("native_shouldshow", new Dictionary<string, object>()
{
{"event_situation", WindowManager.ActiveWindow},
{"ad_unit_id", data.adUnitId},
{"type", NativeType},
{"ad_position", ad_position}
});
}
}
else
{
if (!CheckRuByIp())
{
if (useInter)
{
if (!MaxInterPrice.IsNullPrice())
{
data = MaxInterPrice;
}
else if (!MaxInterPrice2.IsNullPrice())
{
data = MaxInterPrice2;
}
}
else if (useReward)
{
if (!MaxRewardPrice.IsNullPrice())
{
data = MaxRewardPrice;
}
}
}
}
Debug.Log($" 拿到最后比价获胜的是:{(data == null ? "无" : data.Flag)} {(data != null ? data.adUnitId : "kong")}");
return data;
}
public bool CanShowNative()
{
return !NativePrice_1.IsNullPrice() ||
!NativePrice_2.IsNullPrice() ||
!NativePrice_3.IsNullPrice() ||
!NativePrice_4.IsNullPrice() ||
!NativePrice_5.IsNullPrice() ||
!NativePrice_6.IsNullPrice() ||
!NativePrice_7.IsNullPrice() ||
!NativePrice_8.IsNullPrice() ||
!NativePrice_9.IsNullPrice() ||
!NativePrice_10.IsNullPrice();
}
public void SetMaxInterPrice(MaxSdkBase.AdInfo adInfo)
{
if (adInfo.AdUnitIdentifier.Equals(MaxInterPrice.adUnitId))
{
MaxInterPrice.Price = adInfo.Revenue.ToString();
}
}
public void SetMaxInterPrice2(MaxSdkBase.AdInfo adInfo)
{
if (adInfo.AdUnitIdentifier.Equals(MaxInterPrice2.adUnitId))
{
MaxInterPrice2.Price = adInfo.Revenue.ToString();
}
}
public void SetMaxRewardPrice(MaxSdkBase.AdInfo adInfo)
{
if (adInfo.AdUnitIdentifier.Equals(MaxRewardPrice.adUnitId))
{
MaxRewardPrice.Price = adInfo.Revenue.ToString();
}
}
private bool m_useNativeInsteadAdv = false;
// public MaxPriceData MaxPrice2;
public void SetMaxInterClosed(MaxSdkBase.AdInfo adInfo)
{
if (adInfo.AdUnitIdentifier.Equals(MaxInterPrice.adUnitId))
{
MaxInterPrice.ResetPrice();
}
}
public void SetMaxInter2Closed(MaxSdkBase.AdInfo adInfo)
{
if (adInfo.AdUnitIdentifier.Equals(MaxInterPrice2.adUnitId))
{
MaxInterPrice2.ResetPrice();
}
}
public void SetMaxRewardClosed(MaxSdkBase.AdInfo adInfo)
{
if (adInfo.AdUnitIdentifier.Equals(MaxRewardPrice.adUnitId))
{
MaxRewardPrice.ResetPrice();
}
}
public bool ChechAdiTime(float interval=10)
{
float time = GetShowIntersPastTime();
Debug.Log(GlobalFunc.NativeFlag+"Nativeinterval="+time);
return time >= interval|| time==0;
}
private float GetShowIntersPastTime()
{
return (float) (System.DateTime.Now - mLastNativeTime).TotalSeconds;
}
bool CheckRuByIp()
{
Debug.Log($"{GlobalFunc.NativeFlag}+IP:{DataManager.CountryCodeByIp}---NativePriority:{UIConstDataManger.Instance.NativePriority}");
return DataManager.CountryCodeByIp.ToUpper().Equals("RU")
||DataManager.CountryCodeByIp.ToUpper().Equals("IN")
|| UIConstDataManger.Instance.NativePriority == 0;
}
}
using System;
public class MaxPriceData:IComparable
{
public string adUnitId;
public string Price;
public bool Native;
public string Flag;
public MaxPriceData(string adUnitId,string price,string flag,bool _native = false)
{
this.adUnitId = adUnitId;
this.Price = price;
this.Native = _native;
this.Flag = flag;
}
public bool IsNullPrice()
{
return string.IsNullOrEmpty(Price);
}
public void ResetPrice()
{
Price = string.Empty;
}
public void SetPrice(MaxSdkBase.AdInfo adInfo)
{
if (adInfo.AdUnitIdentifier.Equals(MaxPrice1.adUnitId))
{
MaxPrice1.Price = adInfo.Revenue.ToString();
}
// if (adInfo.AdUnitIdentifier.Equals(MaxPrice2.adUnitId))
// {
// MaxPrice2.Price = adInfo.Revenue.ToString();
// }
}
public MaxPriceData MaxPrice1;
// public MaxPriceData MaxPrice2;
public void SetMaxClosed(MaxSdkBase.AdInfo adInfo)
{
if (adInfo.AdUnitIdentifier.Equals(MaxPrice1.adUnitId))
{
MaxPrice1.ResetPrice();
}
// if (adInfo.AdUnitIdentifier.Equals(MaxPrice2.adUnitId))
// {
// MaxPrice2.ResetPrice();
// }
}
public int CompareTo(object obj)
{
if (obj == null) return 1; // 当前对象总是比 null 大
if (obj is not MaxPriceData other)
{
return 1;
}
// Null 价格的处理:认为 Null 价格比非 Null 价格大
if (this.IsNullPrice() && !other.IsNullPrice())
{
return 1; // 当前对象的 NullPrice 排在后面
}
if (!this.IsNullPrice() && other.IsNullPrice())
{
return -1; // 当前对象的非 NullPrice 排在前面
}
// 非 Null 价格比较
if (!this.IsNullPrice() && !other.IsNullPrice())
{
// 使用 double.TryParse 保护解析
if (double.TryParse(this.Price, out double thisPrice) && double.TryParse(other.Price, out double otherPrice))
{
// 降序比较:价格大的排前面
return otherPrice.CompareTo(thisPrice);
}
else
{
// 如果解析失败,可根据需求定义逻辑,这里假设解析失败的价格最小
if (!double.TryParse(this.Price, out _))
return 1; // 当前对象解析失败,排后面
if (!double.TryParse(other.Price, out _))
return -1; // 对比对象解析失败,排后面
}
}
// 如果两个价格都是 Null,则相等
return 0;
}
}
展示最高价格的native
public void ShowOnlyNative(string pos, System.Action succeessCallback = null,
System.Action failCallback = null,int CD=-1)
{
if (DataManager.hasRemoveAds || DataManager.IsVIP)
{
failCallback?.Invoke();
return;
}
if (CD>=0)
{
if (!NativeSDKManager.Instance. ChechAdiTime(CD))
{
failCallback?.Invoke();
return;
}
}
MaxPriceData maxPriceData;
maxPriceData = NativeSDKManager.Instance.NativeSDKPriceHigh(false,false);
if (maxPriceData!=null&&maxPriceData.Native)
{
AnalyticDataHelper.Track("native_ready", new Dictionary<string, object>()
{
{"event_situation", pos},
{"ad_unit_id", maxPriceData.adUnitId},
});
AnalyticDataHelper.Track("native_fill", new Dictionary<string, object>()
{
{"is_fill", 1},
{"position", "native"}
});
AnalyticDataHelper.Track("should_show", new Dictionary<string, object>()
{
{"position", "native"},
});
NativeSDKManager.Instance.ShowNativeAd(maxPriceData.adUnitId,"native", failCallback,succeessCallback);
return;
}
failCallback?.Invoke();
AnalyticDataHelper.Track("native_fill", new Dictionary<string, object>()
{
{"is_fill", NativeSDKManager.Instance.CanShowNative()?1:0 },
{"position", "native" },
});
AnalyticDataHelper.Track("should_show", new Dictionary<string, object>()
{
{"position", "native"},
});
}
加入adi adv native比价逻辑
在adi的OnAdLoadedEvent里设置adi的价格
if (adInfo.AdUnitIdentifier.Equals(AdMaxType.Interstitial_android_new))
{
NativeSDKManager.Instance.SetMaxInterPrice2(adInfo);
Debug.Log(GlobalFunc.NativeFlag+"拿到MaxPrice2广告的价格" + adInfo.Revenue);
//DataManager.networkNameAdi = adInfo.NetworkName;
}
else if (adInfo.AdUnitIdentifier.Equals(AdMaxType.Interstitial_android))
{
NativeSDKManager.Instance.SetMaxInterPrice(adInfo);
Debug.Log(GlobalFunc.NativeFlag+"拿到MaxPrice广告的价格" +adInfo.Revenue);
//DataManager.networkNameAdi = adinfo.NetworkName;
}
在adi的OnAdHiddenEvent重置adi价格
if (adInfo.AdUnitIdentifier.Equals(AdMaxType.Interstitial_android_new))
{
NativeSDKManager.Instance.SetMaxInter2Closed(adInfo);
}
else
{
NativeSDKManager.Instance.SetMaxInterClosed(adInfo);
}
在adv的OnAdLoadedEvent里设置adv的价格
NativeSDKManager.Instance.SetMaxRewardPrice(adInfo);
在adv的OnAdHiddenEvent重置adv价格
NativeSDKManager.Instance.SetMaxRewardClosed(adInfo);
处理展示adi的方法
maxPriceData = NativeSDKManager.Instance.NativeSDKPriceHigh(true,false);
if (maxPriceData!=null&&maxPriceData.Native)
{
AnalyticDataHelper.Track("native_fill", new Dictionary<string, object>()
{
{"is_fill", 1},
{"position", "adi"}
});
AnalyticDataHelper.Track("should_show", new Dictionary<string, object>()
{
{"position", "adi"},
});
NativeSDKManager.Instance.ShowNativeAd(maxPriceData.adUnitId,"adi", failCallback,succeessCallback);
return;
}else if (maxPriceData!=null&&!maxPriceData.Native)
{
//这里可以处理双adi
}
AnalyticDataHelper.Track("native_fill", new Dictionary<string, object>()
{
{"is_fill", NativeSDKManager.Instance.CanShowNative()?1:0 },
{"position", "adi" },
});
AnalyticDataHelper.Track("should_show", new Dictionary<string, object>()
{
{"position", "adi"},
});
//走之前的adi逻辑
同理adv也是
var data = NativeSDKManager.Instance.NativeSDKPriceHigh(false, true);
if (data!=null && data.Native)
{
AnalyticDataHelper.Track("native_fill", new Dictionary<string, object>()
{
{"is_fill", 1 },
{"position", "adv"}
});
AnalyticDataHelper.Track("should_show", new Dictionary<string, object>()
{
{"position", "adv"},
});
NativeSDKManager.Instance.ShowNativeAd(data.adUnitId,"adv", () => { failCallback?.Invoke("");}, () =>
{
m_onRewardAdDidClose?.Invoke();
});
}
else
{
AnalyticDataHelper.Track("native_fill", new Dictionary<string, object>()
{
{"is_fill", NativeSDKManager.Instance.CanShowNative()?1:0 },
{"position", "adv"}
});
AnalyticDataHelper.Track("should_show", new Dictionary<string, object>()
{
{"position", "adv"},
});
//走之前的展示adv逻辑
StartCoroutine(ShowADV(type, failCallback, source, wait));
}
最后选择合适的时机加载Native 现在代码里只有初始化和关闭native才会加载
加入混淆
-keep class com.android.unityad.maxnativelibrary.** {*;}