using System; using System.Collections.Generic; using System.Globalization; using AppsFlyerSDK; using DG.Tweening; // using AppsFlyerSDK; using UnityEngine; using UnityEngine.Events; namespace FlowerPower { public class MaxADKit { public static string SDKKey = "oXM0CzVDi7P1HstOpKvFMInPMOzpQ9uA6t3x75q5f5wQvsEy9vuiiiM94ZJCJSV7PcZGroSSInQCTGsu04QEiE"; public static string interstitialADUnitID = "12480e9974faffec"; public static string rewardedADUnitID = "487d531ebc72302d"; public static string AppOpenAdUnitId = "df561dae26462676"; private const float RevenueThreshold = 0.1f; public static Dictionary adCallbackInfo; public static void Init() { MaxSdkCallbacks.OnSdkInitializedEvent += sdkConfiguration => { if (PlayerPrefs.GetInt("OpenAD", 1) == 1) InitOpenAds(); else GameDispatcher.Instance.Dispatch(GameMsg.CloseMask); DOVirtual.DelayedCall(7, () => { InitializeRewardedAds(); InitializeInterstitialAds(); }); }; MaxSdk.SetSdkKey(SDKKey); // var loginModel = GameHelper.GetLoginModel(); // user_id = loginModel.uid.ToString(); // MaxSdk.SetUserId(loginModel.uid.ToString()); // MaxSdk.SetIsAgeRestrictedUser(false); MaxSdk.SetHasUserConsent(true); MaxSdk.SetDoNotSell(false); MaxSdk.InitializeSdk(); // MaxSdk.ShowMediationDebugger(); adCallbackInfo = new Dictionary(); } #region 插屏广告相关 public static UnityAction onInterstitialAdCompleted = null; public static void ShowInterstitial(string placement = "DefaultInterstitial", UnityAction onCompleted = null) { if (MaxSdk.IsInterstitialReady(interstitialADUnitID)) { // Debug.Log($"广告已经准备好,播放"); MaxSdk.ShowInterstitial(interstitialADUnitID, placement); onInterstitialAdCompleted = onCompleted; } else { // Debug.Log($"广告未准备好,不播放"); onCompleted?.Invoke(false); } } static int retryAttemptInterstitial; public static void InitializeInterstitialAds() { MaxSdkCallbacks.Interstitial.OnAdLoadedEvent += OnInterstitialLoadedEvent; MaxSdkCallbacks.Interstitial.OnAdLoadFailedEvent += OnInterstitialLoadFailedEvent; MaxSdkCallbacks.Interstitial.OnAdDisplayedEvent += OnInterstitialDisplayedEvent; MaxSdkCallbacks.Interstitial.OnAdClickedEvent += OnInterstitialClickedEvent; MaxSdkCallbacks.Interstitial.OnAdHiddenEvent += OnInterstitialHiddenEvent; MaxSdkCallbacks.Interstitial.OnAdDisplayFailedEvent += OnInterstitialAdFailedToDisplayEvent; MaxSdkCallbacks.Interstitial.OnAdRevenuePaidEvent += OnInterstitialAdRevenueEvent; LoadInterstitial(); } private static void LoadInterstitial() { MaxSdk.LoadInterstitial(interstitialADUnitID); } private static void OnInterstitialLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { retryAttemptInterstitial = 0; NetworkKit.BuriedPoint(BuriedPointEvent.video_type, BuriedPointEvent.Interstitial_videos_fill_number, 1); } private static void OnInterstitialLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo) { retryAttemptInterstitial++; double retryDelay = Math.Pow(2, Math.Min(6, retryAttemptInterstitial)); CrazyAsyKit.StartAction("LoadInterstitial", LoadInterstitial, (float)retryDelay); } private static void OnInterstitialDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { } private static void OnInterstitialAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo) { LoadInterstitial(); } private static void OnInterstitialClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { } private static void OnInterstitialHiddenEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { LoadInterstitial(); onInterstitialAdCompleted?.Invoke(true); onInterstitialAdCompleted = null; } #endregion #region 开屏相关 public static UnityAction onAppOpenAdCompleted; private static void LoadAppOpenAd() { Debug.Log("LOAD APP. AD"); MaxSdk.LoadAppOpenAd(AppOpenAdUnitId); } public static void ShowOpenAdIfReady(UnityAction onCompleted = null) { // #if UNITY_EDITOR // onCompleted?.Invoke(true); // #else onAppOpenAdCompleted = onCompleted; if (!IsLoaded) { MaxSdkCallbacks.AppOpen.OnAdLoadedEvent += ShowOpenAd; } else { // bool isFinished = NetworkManager.isWatchedAD; // onCompleted?.Invoke(isFinished); ShowOpenAd(null, null); } // #endif } public static void ShowOpenAd(string adUnitId, MaxSdkBase.AdInfo adInfo) { MaxSdk.ShowAppOpenAd(AppOpenAdUnitId); } public static void InitOpenAds() { MaxSdkCallbacks.AppOpen.OnAdLoadedEvent += OnAppOpenLoadedEvent; MaxSdkCallbacks.AppOpen.OnAdLoadFailedEvent += OnOpendAdLoadFailedEvent; // MaxSdkCallbacks.AppOpen.OnAdDisplayedEvent += OnInterstitialDisplayedEvent; MaxSdkCallbacks.AppOpen.OnAdClickedEvent += OnOpenClickedEvent; MaxSdkCallbacks.AppOpen.OnAdHiddenEvent += OnOpenAdHiddenEvent; // MaxSdkCallbacks.AppOpen.OnAdDisplayFailedEvent += OnInterstitialAdFailedToDisplayEvent; MaxSdkCallbacks.AppOpen.OnAdRevenuePaidEvent += OnOpenAdRevenueEvent; LoadAppOpenAd(); } private static void OnOpenClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { NetworkKit.BuriedPoint(BuriedPointEvent.open_ad, BuriedPointEvent.open_show, 1); NetworkKit.BuriedPoint(BuriedPointEvent.open_ad, BuriedPointEvent.open_show_people, 1); } private static bool IsLoaded = false; private static void OnAppOpenLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { IsLoaded = true; } private static void OnOpendAdLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo) { // retryAttempt++; // double retryDelay = Math.Pow(2, Math.Min(6, retryAttempt)); // CrazyAsyKit.StartAction("LoadRewardedAd", LoadRewardedAd, (float)retryDelay); GameDispatcher.Instance.Dispatch(GameMsg.CloseMask); InitializeRewardedAds(); InitializeInterstitialAds(); } private static void OnOpenAdHiddenEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { // LoadAppOpenAd(); onAppOpenAdCompleted?.Invoke(true); onAppOpenAdCompleted = null; // if (GameHelper.IsGiftSwitch() && !SaveData.GetSaveObject().is_get_removead && (Random.Range(0, 100) < GameHelper.GetCommonModel().rewardinsertion)) // { // TrackManager.GetInstance.SendTrackEvent(TrackKeys.AfterRewardAdShow); // GameHelper.ShowInterstitial("AfterReward"); // } } #endregion #region 激励视频广告相关 public static UnityAction onVideoAdCompleted = null; public static void ShowVideo(string placement = "DefaultVideo", UnityAction onCompleted = null) { #if UNITY_EDITOR onCompleted?.Invoke(true); #else NetworkKit.BuriedPoint(BuriedPointEvent.video_behavior,BuriedPointEvent.watch_ad_people,1); NetworkKit.BuriedPoint(BuriedPointEvent.video_type,BuriedPointEvent.Rewarded_videos_trigger_number,1); if (MaxSdk.IsRewardedAdReady(rewardedADUnitID)) { MaxSdk.ShowRewardedAd(rewardedADUnitID, placement); onVideoAdCompleted=onCompleted; } else { onCompleted?.Invoke(false); } #endif } static int retryAttempt; public static void InitializeRewardedAds() { MaxSdkCallbacks.Rewarded.OnAdLoadedEvent += OnRewardedAdLoadedEvent; MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnRewardedAdLoadFailedEvent; MaxSdkCallbacks.Rewarded.OnAdDisplayedEvent += OnRewardedAdDisplayedEvent; MaxSdkCallbacks.Rewarded.OnAdClickedEvent += OnRewardedAdClickedEvent; MaxSdkCallbacks.Rewarded.OnAdRevenuePaidEvent += OnRewardedAdRevenuePaidEvent; MaxSdkCallbacks.Rewarded.OnAdHiddenEvent += OnRewardedAdHiddenEvent; MaxSdkCallbacks.Rewarded.OnAdDisplayFailedEvent += OnRewardedAdFailedToDisplayEvent; MaxSdkCallbacks.Rewarded.OnAdReceivedRewardEvent += OnRewardedAdReceivedRewardEvent; MaxSdkCallbacks.Rewarded.OnAdRevenuePaidEvent += OnRewardedAdRevenueEvent; LoadRewardedAd(); } private static void LoadRewardedAd() { MaxSdk.LoadRewardedAd(rewardedADUnitID); } private static void OnInterstitialAdRevenueEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { OnAdRevenuePaidEvent(adUnitId, adInfo, 2); #if !FlowerPowerRelease GameHelper.AdOverRevenueEvent(2, 0.1f); #endif } private static void OnRewardedAdRevenueEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { OnAdRevenuePaidEvent(adUnitId, adInfo, 1); #if !FlowerPowerRelease GameHelper.AdOverRevenueEvent(1, 0.1f); #endif } private static void OnOpenAdRevenueEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { OnAdRevenuePaidEvent(adUnitId, adInfo, 3); #if !FlowerPowerRelease GameHelper.AdOverRevenueEvent(3, 0.1f); #endif } public static string user_id = ""; private static void OnAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo, int adType) { //string countryCode = MaxSdk.GetSdkConfiguration().CountryCode; // "US" for the United States, etc - Note: Do not confuse this with currency code which is "USD" // //string networkName = adInfo.NetworkName; // Display name of the network that showed the ad // // string adUnitIdentifier = adInfo.AdUnitIdentifier; // The MAX Ad Unit ID // //string placement = adInfo.Placement; // The placement this ad's postbacks are tied to // // string networkPlacement = adInfo.NetworkPlacement; // The placement ID from the network that showed the ad string countryCode = "USD"; adCallbackInfo.Clear(); adCallbackInfo.Add("appsflyer_id", AppsFlyer.getAppsFlyerId()); adCallbackInfo.Add("customer_user_id", user_id); adCallbackInfo.Add("af_currency", countryCode); //广告收益上传(扣量)((用户累计收益大于0.1上报50%,50%可配置)) double revenue = Convert.ToDouble(PlayerPrefs.GetString($"adInfoRevenue_{user_id}", "0")); revenue += adInfo.Revenue; if (adInfo.Revenue > 0) { GameHelper.AdOverRevenueEvent(adType, (float)adInfo.Revenue); } // Debug.Log($"revenue: {revenue} \n adInfo.Revenue: {adInfo.Revenue}"); // if (revenue >= RevenueThreshold) // { adCallbackInfo.Add("af_revenue", revenue.ToString(CultureInfo.InvariantCulture)); AppsFlyer.sendEvent("af_ad_revenue", adCallbackInfo); PlayerPrefs.SetString($"adInfoRevenue_{user_id}", "0"); int adrate = ConfigSystem.GetConfig().adrate / 100; double revenueAdrate = revenue * adrate; adCallbackInfo["af_revenue"] = revenueAdrate.ToString(CultureInfo.InvariantCulture); AppsFlyer.sendEvent("Af_new_ad_revenue", adCallbackInfo); // } // else // { // PlayerPrefs.SetString($"adInfoRevenue_{user_id}", revenue.ToString()); // } int highSend; if (!PlayerPrefs.HasKey($"sendHighRevenue_{user_id}")) { highSend = 0; // 如果不存在,则初始化为 0 } else { highSend = PlayerPrefs.GetInt($"sendHighRevenue_{user_id}", 0); // 从 PlayerPrefs 中获取 } // 判断是否需要发送高收入事件 // Debug.Log($"highSend=====: {highSend}"); if (highSend == 0) { float limitNum = GameHelper.GetCommonModel().afSendLimit; var totalNum = Convert.ToDouble(PlayerPrefs.GetString($"adRevenueTotal_{user_id}", "0")); totalNum += adInfo.Revenue; // Debug.Log($"totalNum=====: {totalNum} {limitNum}"); if (totalNum >= limitNum) { GameHelper.sendHighRevenueToAF(); // 发送高收入事件 PlayerPrefs.SetInt($"sendHighRevenue_{user_id}", 1); // 标记已发送 } else { PlayerPrefs.SetString($"adRevenueTotal_{user_id}", totalNum.ToString()); } } } private static void OnRewardedAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { retryAttempt = 0; } private static void OnRewardedAdLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo) { retryAttempt++; double retryDelay = Math.Pow(2, Math.Min(6, retryAttempt)); CrazyAsyKit.StartAction("LoadRewardedAd", LoadRewardedAd, (float)retryDelay); } private static void OnRewardedAdDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { } private static void OnRewardedAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo) { LoadRewardedAd(); } private static void OnRewardedAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { } private static bool hasReceivedReward = false; private static void OnRewardedAdHiddenEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { LoadRewardedAd(); if (hasReceivedReward && onVideoAdCompleted != null) { onVideoAdCompleted(true); } // 重置标志 hasReceivedReward = false; onVideoAdCompleted = null; if (GameHelper.IsGiftSwitch() && !SaveData.GetSaveobject().is_get_removead && (UnityEngine.Random.Range(0, 100) < GameHelper.GetCommonModel().rewardinsertion)) { NetworkKit.BuriedPoint(BuriedPointEvent.Apple_pay_event, BuriedPointEvent.afterRewardAdShow, 1); GameHelper.ShowInterstitial("AfterReward"); } } private static void OnRewardedAdReceivedRewardEvent(string adUnitId, MaxSdk.Reward reward, MaxSdkBase.AdInfo adInfo) { hasReceivedReward = true; } private static void OnRewardedAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) { } #endregion } }