2026-04-22 09:52:55 +08:00
using System ;
2026-04-22 11:13:16 +08:00
using System.Collections ;
2026-04-22 09:52:55 +08:00
using System.Collections.Generic ;
using System.Globalization ;
using AppsFlyerSDK ;
using DG.Tweening ;
2026-04-22 11:13:16 +08:00
using Newtonsoft.Json ;
using SGModule.NetKit ;
2026-05-20 14:29:11 +08:00
using SRKZBz0SDK ;
2026-04-22 09:52:55 +08:00
// using AppsFlyerSDK;
2026-04-22 11:13:16 +08:00
// using AppsFlyerSDK;
2026-04-22 09:52:55 +08:00
using UnityEngine ;
using UnityEngine.Events ;
2026-04-27 11:20:13 +08:00
namespace RedHotRoast
2026-04-22 09:52:55 +08:00
{
public class MaxADKit
{
public static string SDKKey =
2026-04-28 18:42:00 +08:00
"LHx_tPFslZpTTiIPABmS24T7Ev1QEVOUVOzirpkxLUuTwJTVZGCKAk9L3Tm3FwuM5LxHK3q1EIbAemJB5sNpX2" ;
2026-04-22 09:52:55 +08:00
2026-04-28 18:42:00 +08:00
public static string interstitialADUnitID = "8971dfc98065c2c0" ;
2026-04-22 11:13:16 +08:00
2026-04-22 09:52:55 +08:00
2026-04-28 18:42:00 +08:00
public static string rewardedADUnitID = "13c3601021e6a8b9" ;
2026-04-22 09:52:55 +08:00
2026-04-22 11:13:16 +08:00
private const float RevenueThreshold = 0 ;
2026-04-22 09:52:55 +08:00
public static void Init ()
{
2026-05-20 14:29:11 +08:00
#if ! UNITY_EDITOR
// 注册 ab事件,0或1,0为自然量版本,1为激励版本
SRKZBz0SDK_Utility . Instance . RegistIosParam (( i =>
2026-04-22 09:52:55 +08:00
{
2026-05-20 14:29:11 +08:00
SuperApplication . Instance . attribution = i == 0 ? "organic" : "non_organic" ;
Debug . Log ( $"ios ab param : {i} attribution=== {SuperApplication.Instance.attribution}" );
NetworkDispatcher . Instance . Dispatch ( NetworkMsg . Login );
}));
2026-04-22 09:52:55 +08:00
2026-05-20 14:29:11 +08:00
void GameConfig ( bool result , string config )
{
Debug . Log ( $"************* game config result : {result}, config : {config}" );
}
// SDK初始化方法
SRKZBz0SDK_Utility . Instance . Init ( null , null , GameConfig );
#endif
// MaxSdkCallbacks.OnSdkInitializedEvent += sdkConfiguration =>
// {
// 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();
2026-04-22 09:52:55 +08:00
// MaxSdk.ShowMediationDebugger();
2026-04-22 11:13:16 +08:00
// #if !UNITY_EDITOR
// MBridgeSDKManager.initialize("368295", "fc0155e8f6e8bda23b06d22414379609");
// #endif
}
2026-04-22 09:52:55 +08:00
2026-04-22 11:13:16 +08:00
public static void SetUserID ( string userID )
{
user_id = userID ;
MaxSdk . SetUserId ( user_id );
2026-04-22 09:52:55 +08:00
}
#region 插屏广告相关
public static UnityAction < bool > onInterstitialAdCompleted = null ;
2026-05-20 14:29:11 +08:00
public static bool CheckInterstitialReady ()
{
return SRKZBz0SDK_Utility . Instance . IsInterReady ();
}
2026-04-22 09:52:55 +08:00
public static void ShowInterstitial ( string placement = "DefaultInterstitial" ,
UnityAction < bool > onCompleted = null )
{
2026-05-20 14:29:11 +08:00
if ( CheckInterstitialReady ())
2026-04-22 09:52:55 +08:00
{
2026-05-20 14:29:11 +08:00
Debug . Log ( $"广告已经准备好,播放" );
SRKZBz0SDK_Utility . Instance . ShowInter ( placement , () =>
{
onCompleted ?. Invoke ( true );
});
2026-04-22 09:52:55 +08:00
}
else
{
2026-05-20 14:29:11 +08:00
Debug . Log ( $"广告未准备好,不播放" );
2026-04-22 09:52:55 +08:00
onCompleted ?. Invoke ( false );
}
2026-05-20 14:29:11 +08:00
// if (MaxSdk.IsInterstitialReady(interstitialADUnitID))
// {
// // Debug.Log($"广告已经准备好,播放");
// MaxSdk.ShowInterstitial(interstitialADUnitID, placement);
// onInterstitialAdCompleted = onCompleted;
//
// }
// else
// {
// // Debug.Log($"广告未准备好,不播放");
//
// onCompleted?.Invoke(false);
// }
2026-04-22 09:52:55 +08:00
}
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 );
2026-04-22 11:13:16 +08:00
BIManager . Instance . TrackAdEvent ( BIEvent . AD_REQUEST , "Interstitial" );
2026-04-22 09:52:55 +08:00
}
private static void OnInterstitialLoadedEvent ( string adUnitId , MaxSdkBase . AdInfo adInfo )
{
retryAttemptInterstitial = 0 ;
2026-04-22 11:13:16 +08:00
// NetworkKit.BuriedPoint(BuriedPointEvent.video_type, BuriedPointEvent.Interstitial_videos_fill_number, 1);
BIManager . Instance . TrackAdEvent ( BIEvent . AD_INVENTORY , "Interstitial" , adInfo . Placement , adInfo . NetworkName );
2026-04-22 09:52:55 +08:00
}
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 )
{
2026-04-22 11:13:16 +08:00
BIManager . Instance . TrackAdEvent ( BIEvent . AD_IMP , "Interstitial" , adInfo . Placement , adInfo . NetworkName ,
adInfo . Revenue , _placement );
2026-04-22 09:52:55 +08:00
}
private static void OnInterstitialAdFailedToDisplayEvent ( string adUnitId , MaxSdkBase . ErrorInfo errorInfo ,
MaxSdkBase . AdInfo adInfo )
{
LoadInterstitial ();
}
private static void OnInterstitialClickedEvent ( string adUnitId , MaxSdkBase . AdInfo adInfo )
{
2026-04-22 11:13:16 +08:00
BIManager . Instance . TrackAdEvent ( BIEvent . AD_CLICK , "Interstitial" , adInfo . Placement , adInfo . NetworkName );
2026-04-22 09:52:55 +08:00
}
private static void OnInterstitialHiddenEvent ( string adUnitId , MaxSdkBase . AdInfo adInfo )
{
LoadInterstitial ();
onInterstitialAdCompleted ?. Invoke ( true );
onInterstitialAdCompleted = null ;
2026-04-22 11:13:16 +08:00
#if ! GAME_RELEASE
GameHelper . AdOverRevenueEvent ( 2 , 0.1f );
#endif
2026-04-22 09:52:55 +08:00
}
#endregion
2026-04-22 11:13:16 +08:00
#region 激励视频广告相关
2026-04-22 09:52:55 +08:00
2026-04-22 11:13:16 +08:00
public static UnityAction < bool > onVideoAdCompleted = null ;
private static string _placement = "" ;
private static Coroutine _waitForVideoAd = null ;
2026-05-20 14:29:11 +08:00
public static bool CheckRewardedReady ()
{
return SRKZBz0SDK_Utility . Instance . IsVideoReady ();
}
2026-04-22 11:13:16 +08:00
public static void ShowVideo ( string placement = "DefaultVideo" , UnityAction < bool > onCompleted = null )
2026-04-22 09:52:55 +08:00
{
2026-04-22 11:13:16 +08:00
onVideoAdCompleted = onCompleted ;
_placement = placement ;
#if UNITY_EDITOR
onVideoAdCompleted ?. Invoke ( true );
#else
TrackKit . SendEvent ( VideoBehaviorTrack . Event , VideoBehaviorTrack . Property . watch_ad_people );
TrackKit . SendEvent ( VideoBehaviorTrack . Event , VideoBehaviorTrack . Property . Rewarded_videos_trigger_number );
2026-04-22 09:52:55 +08:00
2026-05-20 14:29:11 +08:00
if ( CheckRewardedReady ())
2026-04-22 11:13:16 +08:00
{
2026-05-20 14:29:11 +08:00
SRKZBz0SDK_Utility . Instance . ShowRewardVideo ( _placement , b =>
{
onVideoAdCompleted ?. Invoke ( b );
}, ()=>
{
Debug . Log ( $"激励广告关闭" );
if ( GameHelper . IsGiftSwitch () && ! SaveData . GetSaveObject (). is_get_removead && ( UnityEngine . Random . Range ( 0 , 100 ) < GameHelper . GetCommonModel (). rewardinsertion ))
{
TrackKit . SendEvent ( ADEventTrack . Event , ADEventTrack . Property . afterRewardAdShow );
GameHelper . ShowInterstitial ( "AfterReward" );
}
});
2026-04-22 09:52:55 +08:00
}
else
{
2026-05-20 14:29:11 +08:00
onVideoAdCompleted ?. Invoke ( false );
2026-04-22 09:52:55 +08:00
}
2026-05-20 14:29:11 +08:00
// if (MaxSdk.IsRewardedAdReady(rewardedADUnitID))
// {
// MaxSdk.ShowRewardedAd(rewardedADUnitID, _placement);
// }
// else
// {
// if (_waitForVideoAd != null)
// {
// CrazyAsyKit.StopCoroutine(_waitForVideoAd);
// }
// _waitForVideoAd = CrazyAsyKit.StartCoroutine(WaitForVideoAd());
// }
2026-04-22 11:13:16 +08:00
#endif
2026-04-22 09:52:55 +08:00
}
2026-04-22 11:13:16 +08:00
private static IEnumerator WaitForVideoAd ()
2026-04-22 09:52:55 +08:00
{
2026-04-22 11:13:16 +08:00
UICtrlDispatcher . Instance . Dispatch ( UICtrlMsg . PayloadingUI_Open );
var count = 0 ;
var result = false ;
while ( count < 6 )
{
if ( MaxSdk . IsRewardedAdReady ( rewardedADUnitID ))
{
result = true ;
MaxSdk . ShowRewardedAd ( rewardedADUnitID , _placement );
break ;
}
2026-04-22 09:52:55 +08:00
2026-04-22 11:13:16 +08:00
count ++;
yield return new WaitForSeconds ( 0.5f );
2026-04-22 09:52:55 +08:00
}
2026-04-22 11:13:16 +08:00
if (! result )
2026-04-22 09:52:55 +08:00
{
2026-04-22 11:13:16 +08:00
onVideoAdCompleted ?. Invoke ( result );
2026-04-22 09:52:55 +08:00
}
2026-04-22 11:13:16 +08:00
UICtrlDispatcher . Instance . Dispatch ( UICtrlMsg . PayloadingUI_Close );
_waitForVideoAd = null ;
2026-04-22 09:52:55 +08:00
}
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 );
2026-04-22 11:13:16 +08:00
BIManager . Instance . TrackAdEvent ( BIEvent . AD_REQUEST , "RewardedVideo" );
2026-04-22 09:52:55 +08:00
}
private static void OnInterstitialAdRevenueEvent ( string adUnitId , MaxSdkBase . AdInfo adInfo )
{
2026-04-22 11:13:16 +08:00
DOVirtual . DelayedCall ( 0.1f , () =>
{
OnAdRevenuePaidEvent ( adUnitId , adInfo , 2 );
});
2026-04-22 09:52:55 +08:00
}
private static void OnRewardedAdRevenueEvent ( string adUnitId , MaxSdkBase . AdInfo adInfo )
{
2026-04-22 11:13:16 +08:00
DOVirtual . DelayedCall ( 0.1f , () =>
{
OnAdRevenuePaidEvent ( adUnitId , adInfo , 1 );
});
2026-04-22 09:52:55 +08:00
}
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
2026-04-22 11:13:16 +08:00
//广告收益上传(用户收益,每次上传)
double revenue = adInfo . Revenue ;
if ( revenue > 0 )
2026-04-22 09:52:55 +08:00
{
2026-04-22 11:13:16 +08:00
GameHelper . AdOverRevenueEvent ( adType , ( float ) revenue );
2026-04-22 09:52:55 +08:00
}
2026-04-22 11:13:16 +08:00
Debug . Log ( $"[OnAdRevenuePaidEvent] revenue: {revenue} \n adInfo.Revenue: {JsonConvert.SerializeObject(adInfo)}" );
2026-04-22 09:52:55 +08:00
2026-04-22 11:13:16 +08:00
Dictionary < string , string > adCallbackInfo = new Dictionary < string , string >();
if ( revenue > RevenueThreshold )
{
2026-04-22 09:52:55 +08:00
adCallbackInfo . Add ( "af_revenue" , revenue . ToString ( CultureInfo . InvariantCulture ));
AppsFlyer . sendEvent ( "af_ad_revenue" , adCallbackInfo );
PlayerPrefs . SetString ( $"adInfoRevenue_{user_id}" , "0" );
2026-04-22 11:13:16 +08:00
}
2026-04-22 09:52:55 +08:00
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 ());
}
}
2026-04-22 11:13:16 +08:00
/*
ATTRIBUTION_PLATFORM_APPSFLYER = "AppsFlyer";
ATTRIBUTION_PLATFORM_ADJUST = "Adjust";
ATTRIBUTION_PLATFORM_TENJIN = "Tenjin";
ATTRIBUTION_PLATFORM_SINGULAR = "Singular";
ATTRIBUTION_PLATFORM_KOCHAVA = "Kochava";
ATTRIBUTION_PLATFORM_BRANCH = "Branch";
ATTRIBUTION_PLATFORM_REYUN = "Reyun";
ATTRIBUTION_PLATFORM_SOLAR_ENGINE = "SolarEngine";
...
...
*/
//这里需要改成自己的归因平台名称,这里以Adjust为例,"userid"替换为归因平台UID
// MBridgeRevenueParamsEntity mBridgeRevenueParamsEntity = new MBridgeRevenueParamsEntity(MBridgeRevenueParamsEntity.ATTRIBUTION_PLATFORM_ADJUST, PlayerPrefs.GetString("adjust_adid", "default"));
///MaxSdkBase.AdInfo类型的adInfo
// mBridgeRevenueParamsEntity.SetMaxAdInfo(adInfo);
// MBridgeRevenueManager.Track(mBridgeRevenueParamsEntity);
2026-04-22 09:52:55 +08:00
}
private static void OnRewardedAdLoadedEvent ( string adUnitId , MaxSdkBase . AdInfo adInfo )
{
retryAttempt = 0 ;
2026-04-22 11:13:16 +08:00
TrackKit . SendEvent ( ADEventTrack . Event , $"{ADEventTrack.Property.ad_load_code}Succeed" );
BIManager . Instance . TrackAdEvent ( BIEvent . AD_INVENTORY , "RewardedVideo" , adInfo . Placement , adInfo . NetworkName );
2026-04-22 09:52:55 +08:00
}
private static void OnRewardedAdLoadFailedEvent ( string adUnitId , MaxSdkBase . ErrorInfo errorInfo )
{
retryAttempt ++;
2026-04-22 11:13:16 +08:00
double retryDelay = Math . Pow ( 2 , Math . Min ( 3 , retryAttempt ));
2026-04-22 09:52:55 +08:00
2026-04-22 11:13:16 +08:00
TrackKit . SendEvent ( ADEventTrack . Event , $"{ADEventTrack.Property.ad_load_code}{errorInfo.Code}" );
2026-04-22 09:52:55 +08:00
CrazyAsyKit . StartAction ( "LoadRewardedAd" , LoadRewardedAd , ( float ) retryDelay );
}
private static void OnRewardedAdDisplayedEvent ( string adUnitId , MaxSdkBase . AdInfo adInfo )
{
2026-04-22 11:13:16 +08:00
TrackKit . SendEvent ( ADEventTrack . Event , $"{ADEventTrack.Property.ad_show_code}Succeed" );
BIManager . Instance . TrackAdEvent ( BIEvent . AD_IMP , "RewardedVideo" , adInfo . Placement , adInfo . NetworkName , adInfo . Revenue , _placement );
2026-04-22 09:52:55 +08:00
}
private static void OnRewardedAdFailedToDisplayEvent ( string adUnitId , MaxSdkBase . ErrorInfo errorInfo ,
MaxSdkBase . AdInfo adInfo )
{
2026-04-22 11:13:16 +08:00
TrackKit . SendEvent ( ADEventTrack . Event , $"{ADEventTrack.Property.ad_show_code}{errorInfo.Code}" );
2026-04-22 09:52:55 +08:00
LoadRewardedAd ();
}
private static void OnRewardedAdClickedEvent ( string adUnitId , MaxSdkBase . AdInfo adInfo )
{
2026-04-22 11:13:16 +08:00
BIManager . Instance . TrackAdEvent ( BIEvent . AD_CLICK , "RewardedVideo" , adInfo . Placement , adInfo . NetworkName );
2026-04-22 09:52:55 +08:00
}
private static void OnRewardedAdHiddenEvent ( string adUnitId , MaxSdkBase . AdInfo adInfo )
{
LoadRewardedAd ();
2026-04-22 11:13:16 +08:00
if ( _hasReceivedReward && onVideoAdCompleted != null )
2026-04-22 09:52:55 +08:00
{
onVideoAdCompleted ( true );
}
onVideoAdCompleted = null ;
2026-04-22 11:13:16 +08:00
_hasReceivedReward = false ;
2026-04-22 09:52:55 +08:00
2026-04-22 11:13:16 +08:00
if ( GameHelper . IsGiftSwitch () && ! SaveData . GetSaveObject (). is_get_removead && ( UnityEngine . Random . Range ( 0 , 100 ) < GameHelper . GetCommonModel (). rewardinsertion ))
2026-04-22 09:52:55 +08:00
{
2026-04-22 11:13:16 +08:00
TrackKit . SendEvent ( ADEventTrack . Event , ADEventTrack . Property . afterRewardAdShow );
2026-04-22 09:52:55 +08:00
GameHelper . ShowInterstitial ( "AfterReward" );
}
2026-04-22 11:13:16 +08:00
#if ! GAME_RELEASE
GameHelper . AdOverRevenueEvent ( 1 , 0.1f );
#endif
2026-04-22 09:52:55 +08:00
}
2026-04-22 11:13:16 +08:00
private static bool _hasReceivedReward = false ;
2026-04-22 09:52:55 +08:00
private static void OnRewardedAdReceivedRewardEvent ( string adUnitId , MaxSdk . Reward reward ,
MaxSdkBase . AdInfo adInfo )
{
2026-04-22 11:13:16 +08:00
_hasReceivedReward = true ;
2026-04-22 09:52:55 +08:00
}
private static void OnRewardedAdRevenuePaidEvent ( string adUnitId , MaxSdkBase . AdInfo adInfo )
{
}
#endregion
}
}