[Unity] AppLovin Max接入Native 广告 Android篇

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
上一篇:Qt多项目管理动态库使用(QMake)


下一篇:MyBatis 框架学习与实践