下载和导入MAX Unity插件:
官方SDK链接
在这里插入图片描述
2.初始化MAX SDK:
MaxSdkCallbacks.OnSdkInitializedEvent += (MaxSdkBase.SdkConfiguration sdkConfiguration) => {
// AppLovin SDK is initialized, start loading ads
};
MaxSdk.SetSdkKey("YOUR_SDK_KEY_HERE");
MaxSdk.SetUserId("USER_ID");
MaxSdk.InitializeSdk();
3.配置广告源
选择所需要的广告源。点install。ExternalDependencyManager会帮你根据选择的广告源下载对应的SDK,可能需要挂vpn
4.在Unity应用中加载广告
激励视频有一下参考接口
//加载激励视频
MaxSdk.LoadRewardedAd(adUnitId);
//判断是是否已经加载激励视频
MaxSdk.IsRewardedAdReady(adUnitId)
//播放激励视频
MaxSdk.ShowRewardedAd(adUnitId);
//添加奖励下发回调
MaxSdkCallbacks.Rewarded.OnAdReceivedRewardEvent += OnRewardedAdReceivedRewardEvent;
其他类型的广告接口可以看侧边栏的这里选择
文章来源:https://www.toymoban.com/news/detail-476643.html
给一份接入后的参考代码:文章来源地址https://www.toymoban.com/news/detail-476643.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AppLovinMax;
using System;
/// <summary>
/// Max广告聚合中间类
/// </summary>
public class MaxAdsUtil : AdsSDKBase
{
/// <summary>
/// Max SDK key
/// </summary>
string maxKey = SDKConstant.Max_sdk_key;
/// <summary>
/// 激励视频id
/// </summary>
string IdReward = SDKConstant.Max_reward_ads_key;
/// <summary>
/// 插屏视频id
/// </summary>
string IdInterstitial = SDKConstant.Max_interstitial_ads_key;
/// <summary>
/// 横幅视频id
/// </summary>
string IdBannar = SDKConstant.Max_bannar_ads_key;
/// <summary>
/// 初始化
/// </summary>
public override void Init()
{
//base.Init();
if (string.IsNullOrEmpty(maxKey))
{
Loger.Error("MAX SDK KEY is empty");
return;
}
if (isInit)
{
Loger.Info("MAX SDK is Inited");
}
//起动回调
MaxSdkCallbacks.OnSdkInitializedEvent += (MaxSdkBase.SdkConfiguration sdkConfiguration) =>
{
Loger.Info("MAX SDK Init!!!");
InitializeRewardedAds();
isInit = true;
};
string deviceId = SystemInfo.deviceUniqueIdentifier;
MaxSdk.SetUserId(deviceId);
MaxSdk.SetSdkKey(maxKey);
MaxSdk.InitializeSdk();
}
public void ShowDebugView()
{
MaxSdk.ShowMediationDebugger();
}
#region ------------------ 激励视频 ------------------
/// <summary>
/// 激励激励已经准备
/// </summary>
/// <returns></returns>
public override bool IsRewardReady()
{
if (string.IsNullOrEmpty(IdReward)) return false;
return MaxSdk.IsRewardedAdReady(IdReward);
}
/// <summary>
/// 播放激励
/// </summary>
/// <param name="callBack"></param>
public override void PlayRewardAds(Action<bool> callBack)
{
if (!IsRewardReady()) return;
onPlayReward = callBack;
MaxSdk.ShowRewardedAd(IdReward);
}
/// <summary>
/// 激励视频重试请求次数
/// </summary>
int retryAttemptReward = 0;
/// <summary>
/// 激励回调
/// </summary>
Action<bool> onPlayReward;
/// <summary>
/// 初始化激励视频
/// </summary>
public void InitializeRewardedAds()
{
// Attach callback
MaxSdkCallbacks.Rewarded.OnAdLoadedEvent += OnRewardedAdLoadedEvent;
MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnRewardedAdLoadFailedEvent;
MaxSdkCallbacks.Rewarded.OnAdHiddenEvent += OnRewardedAdHiddenEvent;
MaxSdkCallbacks.Rewarded.OnAdDisplayFailedEvent += OnRewardedAdFailedToDisplayEvent;
MaxSdkCallbacks.Rewarded.OnAdReceivedRewardEvent += OnRewardedAdReceivedRewardEvent;
if (string.IsNullOrEmpty(IdReward))
{
Loger.Error("MAX SDK RewardId is empty");
}
// Load the first rewarded ad
LoadRewardedAd();
}
/// <summary>
/// 加载广告
/// </summary>
private void LoadRewardedAd()
{
if (string.IsNullOrEmpty(IdReward)) return;
MaxSdk.LoadRewardedAd(IdReward);
}
/// <summary>
/// 广告加载成功
/// </summary>
/// <param name="adUnitId"></param>
/// <param name="adInfo"></param>
private void OnRewardedAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
{
retryAttemptReward = 0;
Loger.Log($"Max SDK rewardAD ready : id:{adInfo.AdUnitIdentifier}, channel:{adInfo.NetworkName}");
}
/// <summary>
/// 广告加载失败
/// </summary>
/// <param name="adUnitId"></param>
/// <param name="errorInfo"></param>
private void OnRewardedAdLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo)
{
retryAttemptReward++;
float retryDelay = Math.Clamp(2 * retryAttemptReward, 0, 64);
Loger.Info($"Max SDK rewardAD fail :{errorInfo.Code} {errorInfo.Message}");
TimerManager.runTimeOnce(retryDelay, LoadRewardedAd);
}
/// <summary>
/// 广告播放失败
/// </summary>
/// <param name="adUnitId"></param>
/// <param name="errorInfo"></param>
/// <param name="adInfo"></param>
private void OnRewardedAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo)
{
LoadRewardedAd();
onPlayReward = null;
}
/// <summary>
/// 广告关闭
/// </summary>
/// <param name="adUnitId"></param>
/// <param name="adInfo"></param>
private void OnRewardedAdHiddenEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
{
LoadRewardedAd();
onPlayReward = null;
}
/// <summary>
/// 发放奖励
/// </summary>
/// <param name="adUnitId"></param>
/// <param name="reward"></param>
/// <param name="adInfo"></param>
private void OnRewardedAdReceivedRewardEvent(string adUnitId, MaxSdk.Reward reward, MaxSdkBase.AdInfo adInfo)
{
onPlayReward?.Invoke(true);
onPlayReward = null;
Loger.Log($"Max SDK rewardAD Revice : id:{adInfo.AdUnitIdentifier}, channel:{adInfo.NetworkName}");
}
#endregion
#region ------------------ 插屏视频 ------------------
/// <summary>
/// 激励激励已经准备
/// </summary>
/// <returns></returns>
public override bool IsInterstitialReady()
{
if (string.IsNullOrEmpty(IdInterstitial)) return false;
return MaxSdk.IsInterstitialReady(IdInterstitial);
}
/// <summary>
/// 播放激励
/// </summary>
/// <param name="callBack"></param>
public override void PlayInterstitialAds(Action<bool> callBack)
{
if (!IsInterstitialReady()) return;
onPlayInterstitial = callBack;
MaxSdk.ShowInterstitial(IdInterstitial);
}
/// <summary>
/// 插屏视频重试请求次数
/// </summary>
int retryAttemptInterstitial = 0;
/// <summary>
/// 插屏回调
/// </summary>
Action<bool> onPlayInterstitial;
/// <summary>
/// 初始化插屏
/// </summary>
public void InitializeInterstitialAds()
{
// Attach callback
MaxSdkCallbacks.Interstitial.OnAdLoadedEvent += OnInterstitialLoadedEvent;
MaxSdkCallbacks.Interstitial.OnAdLoadFailedEvent += OnInterstitialLoadFailedEvent;
MaxSdkCallbacks.Interstitial.OnAdHiddenEvent += OnInterstitialHiddenEvent;
MaxSdkCallbacks.Interstitial.OnAdDisplayFailedEvent += OnInterstitialAdFailedToDisplayEvent;
if (string.IsNullOrEmpty(IdInterstitial))
{
Loger.Error("MAX SDK InterstitiaId is empty");
}
// Load the first interstitial
LoadInterstitial();
}
/// <summary>
/// 加载插屏广告
/// </summary>
private void LoadInterstitial()
{
if (string.IsNullOrEmpty(IdInterstitial)) return;
MaxSdk.LoadInterstitial(IdInterstitial);
}
/// <summary>
/// 插屏广告加载成功
/// </summary>
/// <param name="adUnitId"></param>
/// <param name="adInfo"></param>
private void OnInterstitialLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
{
retryAttemptInterstitial = 0;
}
/// <summary>
/// 插屏广告加载失败
/// </summary>
/// <param name="adUnitId"></param>
/// <param name="errorInfo"></param>
private void OnInterstitialLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo)
{
retryAttemptInterstitial++;
float retryDelay = Math.Clamp(2 * retryAttemptInterstitial, 0, 64);
TimerManager.runTimeOnce(retryDelay, LoadInterstitial);
}
/// <summary>
/// 插屏广告显示失败
/// </summary>
/// <param name="adUnitId"></param>
/// <param name="errorInfo"></param>
/// <param name="adInfo"></param>
private void OnInterstitialAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo)
{
LoadInterstitial();
onPlayInterstitial = null;
}
/// <summary>
/// 插屏广告显示关闭
/// </summary>
/// <param name="adUnitId"></param>
/// <param name="adInfo"></param>
private void OnInterstitialHiddenEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
{
LoadInterstitial();
onPlayInterstitial?.Invoke(true);
onPlayInterstitial = null;
}
#endregion
#region ------------------ Bannar视频 ------------------
bool isBannarReady = false;
public override void ShowBannarAds()
{
if (IsBannarReady())
MaxSdk.ShowBanner(IdBannar);
}
public override void HideBannarAds()
{
MaxSdk.HideBanner(IdBannar);
}
/// <summary>
/// 设置广告位置
/// </summary>
/// <param name="pos"></param>
public override void SetBannarPos(BannarPos pos = BannarPos.BottomCenter)
{
MaxSdkBase.BannerPosition enmuPos;
switch (pos)
{
case BannarPos.TopCenter:
enmuPos = MaxSdkBase.BannerPosition.TopCenter;break;
case BannarPos.BottomCenter:
enmuPos = MaxSdkBase.BannerPosition.BottomCenter;break;
default:
enmuPos = MaxSdkBase.BannerPosition.BottomCenter; break;
}
MaxSdk.UpdateBannerPosition(IdBannar, enmuPos);
}
public override bool IsBannarReady()
{
return isBannarReady;
}
public void InitializeBannerAds()
{
// Banners are automatically sized to 320×50 on phones and 728×90 on tablets
// You may call the utility method MaxSdkUtils.isTablet() to help with view sizing adjustments
MaxSdk.CreateBanner(IdBannar, MaxSdkBase.BannerPosition.BottomCenter);
// Set background or background color for banners to be fully functional
MaxSdk.SetBannerBackgroundColor(IdBannar, Color.white);
MaxSdkCallbacks.Banner.OnAdLoadedEvent += OnBannerAdLoadedEvent;
MaxSdkCallbacks.Banner.OnAdLoadFailedEvent += OnBannerAdLoadFailedEvent;
MaxSdk.LoadBanner(IdBannar);
}
private void OnBannerAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
{
isBannarReady = true;
}
private void OnBannerAdLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo)
{
isBannarReady = false;
MaxSdk.LoadBanner(IdBannar);
}
#endregion
}
到了这里,关于【Unity】接入Max广告聚合SDK的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!