在 Unity 中实现插屏广告
要使用 Advertisements API
显示全屏插屏广告,请初始化 SDK,然后使用 Load
函数将广告内容加载到 广告单元,并使用 Show
函数显示广告。
Note: Starting from SDK version 4.4.1, the Unity Ads package is now called the Advertisement Legacy package in the Unity Editor. The Advertisement Legacy package version 4.12 is still functional but will not receive any further updates for new or enhanced features. SDK version 4.12 supports the Apple Privacy Manifest update, with no further updates planned for the package.
在 SDK 版本 3.7.0 及更高版本中,可以使用 IUnityAdsLoadListener
和 IUnityAdsShowListener
回调来实现内容成功或失败加载或显示时的逻辑。
Note: If you call Show
without specifying an Ad Unit ID, the method shows loaded content in the Unity Standard Placement.
using UnityEngine;
using UnityEngine.Advertisements;
public class InterstitialAdExample : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
[SerializeField] string _androidAdUnitId = "Interstitial_Android";
[SerializeField] string _iOsAdUnitId = "Interstitial_iOS";
string _adUnitId;
void Awake()
{
// Get the Ad Unit ID for the current platform:
_adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
? _iOsAdUnitId
: _androidAdUnitId;
}
// Load content to the Ad Unit:
public void LoadAd()
{
// IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
Debug.Log("Loading Ad: " + _adUnitId);
Advertisement.Load(_adUnitId, this);
}
// Show the loaded content in the Ad Unit:
public void ShowAd()
{
// Note that if the ad content wasn't previously loaded, this method will fail
Debug.Log("Showing Ad: " + _adUnitId);
Advertisement.Show(_adUnitId, this);
}
// Implement Load Listener and Show Listener interface methods:
public void OnUnityAdsAdLoaded(string adUnitId)
{
// Optionally execute code if the Ad Unit successfully loads content.
}
public void OnUnityAdsFailedToLoad(string _adUnitId, UnityAdsLoadError error, string message)
{
Debug.Log($"Error loading Ad Unit: {_adUnitId} - {error.ToString()} - {message}");
// Optionally execute code if the Ad Unit fails to load, such as attempting to try again.
}
public void OnUnityAdsShowFailure(string _adUnitId, UnityAdsShowError error, string message)
{
Debug.Log($"Error showing Ad Unit {_adUnitId}: {error.ToString()} - {message}");
// Optionally execute code if the Ad Unit fails to show, such as loading another ad.
}
public void OnUnityAdsShowStart(string _adUnitId) { }
public void OnUnityAdsShowClick(string _adUnitId) { }
public void OnUnityAdsShowComplete(string _adUnitId, UnityAdsShowCompletionState showCompletionState) { }
}
下一步:要改进您的实现,请参阅 在 Unity 中实现奖励广告 文档。