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

news2024/12/15 15:25:50

把下载下来的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.** {*;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2259996.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【前端 Uniapp】使用Vant打造Uniapp项目(避坑版)

一、基本介绍 Uniapp 是基于 Vue.js 的开发框架&#xff0c;通过一套代码可以同时发布到多个平台的应用框架。而 Vant 是针对移动端 Vue.js 的组件库。通过这样的组合&#xff0c;我们可以快速构建出一个跨平台的移动应用。Vant 已经支持多种小程序和 H5 平台&#xff0c;也对…

【记录】Django解决与VUE跨域问题

1 梗概 这里记录Django与VUE的跨域问题解决方法&#xff0c;主要修改内容是在 Django 中。当然其他的前端项目 Django 也可以这样处理。 2 安装辅助包 pip install django-cors-headers3 配置 settings.py INSTALLED_APPS [ # ... corsheaders, # ... ] 为了响应…

【AI知识】激活函数介绍(sigmoid Tanh Relu)+ 梯度爆炸 / 消失及解决办法

激活函数&#xff1a; 使用激活函数的原因&#xff1a; 神经网络中每一层的输入输出都是一个线性求和的过程&#xff0c;下一层的输出只是承接了上一层输入函数的线性变换&#xff0c;如果没有激活函数&#xff0c;无论构造的神经网络多么复杂&#xff0c;有多少层&#xff0c;…

驱动开发-入门【1】

1.内核下载地址 Linux内核源码的官方网站为https://www.kernel.org/&#xff0c;可以在该网站下载最新的Linux内核源码。进入该网站之后如下图所示&#xff1a; 从上图可以看到多个版本的内核分支&#xff0c;分别为主线版本&#xff08;mainline&#xff09;、稳定版本&#…

3D 生成重建038-DiffGS训练一个3DGS编码器来简化训练

3D 生成重建038-DiffGS训练一个3DGS编码器来简化训练 文章目录 0 论文工作1 论文方法2 实验结果 0 论文工作 三维高斯溅射&#xff08;3DGS&#xff09;在渲染速度和保真度方面表现出了令人信服的性能&#xff0c;但由于其离散性和非结构性&#xff0c;高斯溅射的生成仍然是一…

【渗透测试一】信息收集

信息收集简介 定义 信息收集是渗透测试和网络安全评估等活动的初始关键阶段。它是指通过各种合法手段&#xff08;如网络扫描、查询公开数据库、社会工程学等&#xff09;&#xff0c;收集与目标系统&#xff08;包括网络、主机、应用程序、组织等&#xff09;相关的信息&…

小迪笔记第五十一天-前后台功能点文件下载文件读取文件删除目录遍历目录穿越

前言 前后台功能点 就是因为权限的不同而造成的 功能的不同 一般这个文件的 下载 文件的读取 一般出现在前台 而人家的删除和遍历 目录的穿越出现在 后台 以这个海洋cms 为例进行 演示 &#xff08;源码在后台&#xff09; 后台管理地址&#xff1a;http://192.168…

「Mac玩转仓颉内测版50」小学奥数篇13 - 动态规划入门

本篇将通过 Python 和 Cangjie 双语介绍动态规划的基本概念&#xff0c;并解决一个经典问题&#xff1a;斐波那契数列。学生将学习如何使用动态规划优化递归计算&#xff0c;并掌握编程中的重要算法思想。 关键词 小学奥数Python Cangjie动态规划斐波那契数列 一、题目描述 …

phidata - 具有记忆、知识、工具和推理能力的多模态代理

Phidata 是一个用于构建多模态代理的框架&#xff0c;使用 phidata 可以&#xff1a;使用内存、知识、工具和推理构建多模式代理。建立可以协同工作解决问题的代理团队。使用漂亮的 Agent UI 与您的代理聊天。 16200 Stars 2200 Forks 28 Issues 82 贡献者 MPL-2.0 License Pyt…

第六届全球校园人工智能算法精英大赛-算法巅峰专项赛(系列文章)-- 开篇

前言 “全球校园人工智能算法精英大赛”是江苏省人工智能学会举办的面向全球具有正式学籍的全日制高等院校及以上在校学生举办的算法竞赛。其中的算法巅峰专项赛是新赛道&#xff0c;2024年是其第一届比赛。 翻阅过所有赛道的题目&#xff0c;题目出的真心可以&#xff0c;很具…

柚坛工具箱Uotan Toolbox适配鸿蒙,刷机体验再升级

想要探索智能设备的无限可能&#xff1f;Uotan Toolbox&#xff08;柚坛工具箱&#xff09;将是您的得力助手。这款采用C#语言打造的创新型开源工具箱&#xff0c;以其独特的设计理念和全面的功能支持&#xff0c;正在改变着用户与移动设备互动的方式。 作为一款面向专业用户的…

‘Close Project‘ is not available while IDEA is updating indexes的解决

XXX is not available while IDEA is updating indexes IDEA 1.Remove from Recent Projects 2.重新 Open工程即可

[笔记] 编译LetMeowIn(C++汇编联编程序)过程

文章目录 前言过程下载源码vs2017 创建空项目 引入编译文件改项目依赖属性改汇编编译属性该项目还需注意编译运行 总结 前言 编译LetMeowin 项目发现是个混编项目&#xff0c;c调用汇编的程序&#xff0c;需要配置一下&#xff0c;特此记录一下 过程 下载源码 首先下载源码…

Linux系统操作03|chmod、vim

上文&#xff1a; Linux系统操作02|基本命令-CSDN博客 目录 六、chmod&#xff1a;给文件设置权限 1、字母法 2、数字法&#xff08;用的最多&#xff09; 七、vim&#xff1a;代码编写和文本编辑 1、启动和退出 1️⃣启动 2️⃣退出 2、vim基本操作 六、chmod&#x…

SpringCloud微服务实战系列:01让SpringCloud项目在你机器上运行起来

目录 项目选型 项目安装-本地运行起来 软件安装&#xff1a; 项目启动&#xff1a; 总结&答疑 项目选型 软件开发&#xff0c;基本上都不会从0开始&#xff0c;一般都是在其他项目或者组件的基础上进行整合优化迭代&#xff0c;站在巨人肩膀上才能看得更远&#xff0c…

Python鼠标轨迹算法(游戏防检测)

一.简介 鼠标轨迹算法是一种模拟人类鼠标操作的程序&#xff0c;它能够模拟出自然而真实的鼠标移动路径。 鼠标轨迹算法的底层实现采用C/C语言&#xff0c;原因在于C/C提供了高性能的执行能力和直接访问操作系统底层资源的能力。 鼠标轨迹算法具有以下优势&#xff1a; 模拟…

npm error Error: Command failed: F:\360Downloads\Software\nodejs\node.exe

前言&#xff1a; 电脑环境&#xff1a;win7 node版本&#xff1a;18.20.0 npm版本&#xff1a;10.9.2 情景再现&#xff1a;电脑上是存在的vuevite的项目且可以正常运行。想着摸鱼的时间复习一下ts语法&#xff0c;所以想创建一个demo。按照 开始 | Vite 官方中文文档 官网创建…

软件工程 设计的复杂性

复杂性代表事件或事物的状态&#xff0c;它们具有多个相互关联的链接和高度复杂的结构。在软件编程中&#xff0c;随着软件设计的实现&#xff0c;元素的数量以及它们之间的相互联系逐渐变得庞大&#xff0c;一下子变得难以理解。 如果不使用复杂性指标和度量&#xff0c;软件…

大屏开源项目go-view二次开发3----象形柱图控件(C#)

环境搭建参考&#xff1a; 大屏开源项目go-view二次开发1----环境搭建(C#)-CSDN博客 要做的象形柱图控件最终效果如下图&#xff1a; 其实这个控件我前面的文章也介绍过&#xff0c;不过是用wpf做的&#xff0c;链接如下&#xff1a; wpf利用Microsoft.Web.WebView2显示html…

ORB-SLAM3源码学习:G2oTypes.cc: void EdgeInertial::computeError 计算预积分残差

前言 这部分函数涉及了g2o的内容以及IMU相关的推导内容&#xff0c;需要你先去进行这部分的学习。 1.函数声明 void EdgeInertial::computeError() 2.函数定义 涉及到的IMU的公式&#xff1a; {// TODO Maybe Reintegrate inertial measurments when difference between …