Unity本身也是支持广告的,它是一个广告系统,同时也提供广告中介服务,可以接入其他广告平台。
Unity中添加广告的方式有两种,一种是Advertise Legacy,一种是LevelPlay。前者是Unity自己的广告系统,后者是直接收购后整合进来的。
这两种方式现在都支持,但前者已经不更新了。
所以开发者尽量使用LevelPlay,它在接入其他广告平台时也很方便。
功能/特性 | Advertisement Legacy | Ads Mediation |
支持广告网络 | 仅支持 Unity Ads | 支持多个广告网络 |
集成复杂度 | 简单(少量代码,Unity Ads 默认支持) | 较复杂(需额外配置调解规则和安装广告 SDK) |
广告填充率 | 依赖 Unity Ads 的覆盖范围和填充率 | 利用调解优化填充率 |
广告收益 | 收益依赖 Unity Ads | 综合多个广告网络的收益,通常更高 |
适用项目规模 | 小型项目 | 中型到大型项目 |
灵活性 | 较低 | 高,支持自定义调解策略 |
本文主要介绍如何集成传统的 Advertisement Legacy,至于Ads Mediation将在另外一篇中单独介绍。
在集成之前要做:
- 将项目连接到Service平台
- 安装Advertisement Legacy包
- 在cloud平台添加广告单元
(一)集成插屏广告
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;public class AdDisplay : MonoBehaviour
{public string myGameIdAndroid = "YOUR_GAME_ID_HERE";public string myGameIdIOS = "YOUR_GAME_ID_HERE";public string adUnitIdAndroid = "Interstitial_Android";public string adUnitIdIOS = "Interstitial_iOS";public string myAdUnitId;public bool adStarted;private bool testMode = true;// Start is called before the first frame updatevoid Start(){#if UNITY_IOSAdvertisement.Initialize(myGameIdIOS, testMode);myAdUnitId = adUnitIdIOS;#elseAdvertisement.Initialize(myGameIdAndroid, testMode);myAdUnitId = adUnitIdAndroid;#endif}// Update is called once per framevoid Update(){if (Advertisement.isInitialized && !adStarted){Advertisement.Load(myAdUnitId);Advertisement.Show(myAdUnitId);adStarted = true;}}
(二)集成奖励广告
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;public class RewardedAdDisplay : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
{public string myGameIdAndroid = "YOUR_GAME_ID_HERE";public string myGameIdIOS = "YOUR_GAME_ID_HERE";public string adUnitIdAndroid = "Rewarded_Android";public string adUnitIdIOS = "Rewarded_iOS";public string myAdUnitId;public string myAdStatus = "";public bool adStarted;public bool adCompleted;private bool testMode = true;// Start is called before the first frame updatevoid Start(){#if UNITY_IOSAdvertisement.Initialize(myGameIdIOS, testMode, this);myAdUnitId = adUnitIdIOS;#elseAdvertisement.Initialize(myGameIdAndroid, testMode, this);myAdUnitId = adUnitIdAndroid;#endif}// Update is called once per framevoid Update(){}public void OnInitializationComplete(){Debug.Log("Unity Ads initialization complete.");Advertisement.Load(myAdUnitId,this);}public void OnInitializationFailed(UnityAdsInitializationError error, string message){myAdStatus = message;Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");}public void OnUnityAdsAdLoaded(string adUnitId){Debug.Log("Ad Loaded: " + adUnitId);if (!adStarted){Advertisement.Show(myAdUnitId,this);}}public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message){myAdStatus = message;Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");}public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message){myAdStatus = message;Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");}public void OnUnityAdsShowStart(string adUnitId){adStarted = true;Debug.Log("Ad Started: " + adUnitId);}public void OnUnityAdsShowClick(string adUnitId){Debug.Log("Ad Clicked: " + adUnitId);}public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState){adCompleted = showCompletionState == UnityAdsShowCompletionState.COMPLETED;Debug.Log("Ad Completed: " + adUnitId);}}
(三)集成横幅广告
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;public class BannerAdExample : MonoBehaviour
{// For the purpose of this example, these buttons are for functionality testing:[SerializeField] Button _loadBannerButton;[SerializeField] Button _showBannerButton;[SerializeField] Button _hideBannerButton;[SerializeField] BannerPosition _bannerPosition = BannerPosition.BOTTOM_CENTER;[SerializeField] string _androidAdUnitId = "Banner_Android";[SerializeField] string _iOSAdUnitId = "Banner_iOS";string _adUnitId = null; // This will remain null for unsupported platforms.void Start(){// Get the Ad Unit ID for the current platform:
#if UNITY_IOS_adUnitId = _iOSAdUnitId;
#elif UNITY_ANDROID_adUnitId = _androidAdUnitId;
#endif// Disable the button until an ad is ready to show:_showBannerButton.interactable = false;_hideBannerButton.interactable = false;// Set the banner position:Advertisement.Banner.SetPosition(_bannerPosition);// Configure the Load Banner button to call the LoadBanner() method when clicked:_loadBannerButton.onClick.AddListener(LoadBanner);_loadBannerButton.interactable = true;}// Implement a method to call when the Load Banner button is clicked:public void LoadBanner(){// Set up options to notify the SDK of load events:BannerLoadOptions options = new BannerLoadOptions{loadCallback = OnBannerLoaded,errorCallback = OnBannerError};// Load the Ad Unit with banner content:Advertisement.Banner.Load(_adUnitId, options);}// Implement code to execute when the loadCallback event triggers:void OnBannerLoaded(){Debug.Log("Banner loaded");// Configure the Show Banner button to call the ShowBannerAd() method when clicked:_showBannerButton.onClick.AddListener(ShowBannerAd);// Configure the Hide Banner button to call the HideBannerAd() method when clicked:_hideBannerButton.onClick.AddListener(HideBannerAd);// Enable both buttons:_showBannerButton.interactable = true;_hideBannerButton.interactable = true; }// Implement code to execute when the load errorCallback event triggers:void OnBannerError(string message){Debug.Log($"Banner Error: {message}");// Optionally execute additional code, such as attempting to load another ad.}// Implement a method to call when the Show Banner button is clicked:void ShowBannerAd(){// Set up options to notify the SDK of show events:BannerOptions options = new BannerOptions{clickCallback = OnBannerClicked,hideCallback = OnBannerHidden,showCallback = OnBannerShown};// Show the loaded Banner Ad Unit:Advertisement.Banner.Show(_adUnitId, options);}// Implement a method to call when the Hide Banner button is clicked:void HideBannerAd(){// Hide the banner:Advertisement.Banner.Hide();}void OnBannerClicked() { }void OnBannerShown() { }void OnBannerHidden() { }void OnDestroy(){// Clean up the listeners:_loadBannerButton.onClick.RemoveAllListeners();_showBannerButton.onClick.RemoveAllListeners();_hideBannerButton.onClick.RemoveAllListeners();}
}
参考:
1. Unity Ad文档: https://docs.unity.com/ads/en-us/manual/UnityAdsHome
2. https://learn.unity.com/tutorial/getting-started-with-unity-monetization
3. https://learn.unity.com/tutorial/unity-monetization-rewarded-ads