2023-12-26 03:40:48 +00:00
namespace Guru
{
using UnityEngine ;
using System ;
public partial class GuruSDK
{
/// <summary>
/// 启动广告服务
/// </summary>
public static void StartAds ( )
{
if ( InitConfig . UseCustomConsent )
{
Debug . Log ( $"{Tag} --- Call <color=orange>StartAdsWithCustomConsent</color> when you use custom consent, and pass the result (boolean) to the method." ) ;
}
else
{
// 默认的启动顺序是先启动Consent后, 根据用户回调的结果来启动广告
Instance . StartConsentFlow ( ) ;
}
}
/// <summary>
/// 使用自定义的Consent, 获取用户授权后, 调用此方法
/// </summary>
/// <param name="userAllow"></param>
2024-01-30 02:23:49 +00:00
/// <param name="guideType">Consent 引导的类型, 如果使用了 MAX 的 consent 请填写 max </param>
public static void StartAdsWithCustomConsent ( bool userAllow = true , string guideType = "custom" )
2023-12-26 03:40:48 +00:00
{
2024-01-30 02:23:49 +00:00
#if UNITY_IOS
_attType = guideType ;
InitAttStatus ( ) ;
# endif
2023-12-26 03:40:48 +00:00
if ( userAllow )
{
2024-01-30 02:23:49 +00:00
#if UNITY_IOS
CheckAttStatus ( ) ;
# else
2023-12-26 03:40:48 +00:00
StartAdService ( ) ;
2024-01-30 02:23:49 +00:00
# endif
2023-12-26 03:40:48 +00:00
}
else
{
Debug . Log ( $"{Tag} --- User refuse to provide ads Id, Ads Service will be cancelled" ) ;
}
}
#region Guru Consent
/// <summary>
/// 启动Consent流程
/// </summary>
private void StartConsentFlow ( )
{
LogI ( $"StartConsentFlow" ) ;
GuruConsent . StartConsent ( OnConsentOver ) ;
}
private void OnConsentOver ( int code )
{
2024-01-30 02:23:49 +00:00
#if UNITY_IOS
InitAttStatus ( ) ;
# endif
2023-12-26 03:40:48 +00:00
Callbacks . ConsentFlow . _onConsentResult ? . Invoke ( code ) ;
switch ( code )
{
case GuruConsent . StatusCode . OBTAINED :
case GuruConsent . StatusCode . NOT_AVAILABLE :
// 已获取授权, 或者地区不可用
#if UNITY_IOS
2024-01-30 02:23:49 +00:00
CheckAttStatus ( ) ;
2023-12-26 03:40:48 +00:00
# endif
break ;
}
2024-02-01 09:53:45 +00:00
// 无论状态如何, 都在回调内启动广告初始化
StartAdService ( ) ;
2023-12-26 03:40:48 +00:00
}
2024-01-30 02:23:49 +00:00
# endregion
#region IOS ATT 广告授权流程
2023-12-26 03:40:48 +00:00
#if UNITY_IOS
2024-01-30 02:23:49 +00:00
private static string _initialAttStatus ;
private static String _attType = "admob" ;
/// <summary>
/// 显示系统的 ATT 弹窗
2023-12-26 03:40:48 +00:00
/// </summary>
2024-01-30 02:23:49 +00:00
public static void RequestAttDialog ( )
2023-12-26 03:40:48 +00:00
{
2024-01-30 02:23:49 +00:00
LogI ( $"RequestATTDialog" ) ;
ATTManager . RequestATTDailog ( ReportAttStatus ) ;
2023-12-26 03:40:48 +00:00
}
2024-01-30 02:23:49 +00:00
/// <summary>
/// 初始化 ATT 状态
/// </summary>
public static void InitAttStatus ( )
{
_initialAttStatus = ATTManager . GetStatus ( ) ;
SetUserProperty ( Analytics . ParameterATTStatus , _initialAttStatus ) ; // 上报一个初始的状态
}
/// <summary>
/// iOS 平台检查 ATT 状态
/// </summary>
private static void CheckAttStatus ( ) = > ATTManager . CheckStatus ( ReportAttStatus ) ;
private static void ReportAttStatus ( string status )
{
LogI ( $"{Tag} --- Get Att status: {status}" ) ;
SetUserProperty ( Analytics . ParameterATTStatus , status ) ; // 当前的状态
if ( ! string . IsNullOrEmpty ( _initialAttStatus )
& & status ! = ATTManager . ATT_STATUS_NOT_DETERMINED
& & _initialAttStatus ! = status )
{
// 上报点位:
Analytics . AttResult ( status , _attType ) ;
}
2023-12-26 03:40:48 +00:00
2024-01-30 02:23:49 +00:00
switch ( status )
{
case ATTManager . ATT_STATUS_NOT_DETERMINED :
// ATT 状态未知, 请求弹窗
RequestAttDialog ( ) ;
break ;
case ATTManager . ATT_STATUS_RESTRICTED :
case ATTManager . ATT_STATUS_DENIED :
2024-02-01 09:53:45 +00:00
// ATT 状态受限, 或者被拒绝
2024-01-30 02:23:49 +00:00
break ;
case ATTManager . ATT_STATUS_AUTHORIZED :
2024-02-01 09:53:45 +00:00
// ATT 状态已授权
2024-01-30 02:23:49 +00:00
break ;
}
}
# endif
#endregion
2023-12-26 03:40:48 +00:00
#region Ad Services
private static bool _initAdsCompleted = false ;
/// <summary>
/// 启动广告服务
/// </summary>
public static void StartAdService ( )
{
LogI ( $"StartAdService" ) ;
ADService . Instance . StartService ( OnAdsInitComplete ,
InitConfig . AutoLoadWhenAdsReady , IsDebugMode ) ;
//--------- Callbacks -----------
2024-01-17 09:51:08 +00:00
ADService . OnBannerLoaded = OnBannerLoaded ;
2023-12-26 03:40:48 +00:00
ADService . OnInterstitialLoaded = OnInterstitialLoaded ;
ADService . OnInterstitialFailed = OnInterstitialFailed ;
ADService . OnRewardLoaded = OnRewardLoaded ;
ADService . OnRewardFailed = OnRewardFailed ;
}
2024-01-17 09:51:08 +00:00
private static void OnBannerLoaded ( )
= > Callbacks . Ads . _onBannerADLoaded ? . Invoke ( ) ;
2023-12-26 03:40:48 +00:00
private static void OnInterstitialLoaded ( )
= > Callbacks . Ads . _onInterstitialADLoaded ? . Invoke ( ) ;
private static void OnInterstitialFailed ( )
= > Callbacks . Ads . _onInterstitialADFailed ? . Invoke ( ) ;
private static void OnRewardLoaded ( )
= > Callbacks . Ads . _onRewardedADLoaded ? . Invoke ( ) ;
private static void OnRewardFailed ( )
= > Callbacks . Ads . _onRewardADFailed ? . Invoke ( ) ;
private static void OnAdsInitComplete ( )
{
_initAdsCompleted = true ;
Callbacks . Ads . _onAdsInitComplete ? . Invoke ( ) ;
}
private static bool CheckAdsReady ( )
{
if ( ! _initAdsCompleted )
{
LogE ( "Ads is not ready. Call <GuruSDk.StartAdService> first." ) ;
return false ;
}
return true ;
}
/// <summary>
/// 显示Banner广告
/// </summary>
/// <param name="placement"></param>
public static void ShowBanner ( string placement = "" )
{
if ( ! CheckAdsReady ( ) ) return ;
ADService . Instance . ShowBanner ( placement ) ;
}
/// <summary>
/// 隐藏Banner广告
/// </summary>
public static void HideBanner ( )
{
if ( ! CheckAdsReady ( ) ) return ;
ADService . Instance . HideBanner ( ) ;
}
public static void LoadInterstitialAd ( )
{
if ( ! CheckAdsReady ( ) ) return ;
ADService . Instance . RequestInterstitialAD ( ) ;
}
2023-12-28 07:35:35 +00:00
public static bool IsInterstitialAdReady = > ADService . Instance . IsInterstitialADReady ( ) ;
2023-12-26 03:40:48 +00:00
/// <summary>
/// 显示插屏广告
/// </summary>
/// <param name="placement"></param>
/// <param name="onDismissed"></param>
public static void ShowInterstitialAd ( string placement = "" , Action onDismissed = null )
{
if ( ! CheckAdsReady ( ) ) return ;
if ( ! ADService . Instance . IsInterstitialADReady ( ) )
{
LogE ( "Interstitial is not ready. Call <GuruSDk.ShowInterstitialAd> again." ) ;
LoadInterstitialAd ( ) ;
return ;
}
ADService . Instance . ShowInterstitialAD ( placement , onDismissed ) ;
}
public static void LoadRewardAd ( )
{
if ( ! CheckAdsReady ( ) ) return ;
ADService . Instance . RequestRewardedAD ( ) ;
}
2023-12-28 07:35:35 +00:00
public static bool IsRewardAdReady = > ADService . Instance . IsRewardedADReady ( ) ;
2023-12-26 03:40:48 +00:00
/// <summary>
/// 显示激励视频广告
/// </summary>
/// <param name="placement"></param>
/// <param name="onRewarded"></param>
/// <param name="onFailed"></param>
public static void ShowRewardAd ( string placement = "" , Action onRewarded = null , Action < string > onFailed = null )
{
if ( ! CheckAdsReady ( ) ) return ;
if ( ! ADService . Instance . IsRewardedADReady ( ) )
{
LogE ( "RewardAd is not ready. Call <GuruSDk.LoadRewardAd> again." ) ;
LoadRewardAd ( ) ;
return ;
}
ADService . Instance . ShowRewardedAD ( placement , onRewarded , onFailed ) ;
}
# endregion
#region MaxServices
/// <summary>
/// 显示Max调试菜单
/// </summary>
public static void ShowMaxDebugPanel ( )
{
#if UNITY_EDITOR
LogI ( $"Can not show Max Debug Panel in Editor, skipped." ) ;
return ;
# endif
if ( ! ADService . Instance . IsInitialized )
{
LogI ( $"ADService is not initialized, call <GuruSDK.StartAds> first." ) ;
return ;
}
ADService . Instance . ShowMaxDebugPanel ( ) ;
}
# endregion
}
}