在 Unity 中实现激励广告
奖励玩家观看广告可以提高用户参与度,从而带来更高的收入。例如,游戏可能会奖励玩家游戏内货币、消耗品、额外生命或经验倍增器。有关如何有效设计激励广告的更多信息,请参阅 获利策略 文档。
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.
要奖励玩家完成视频广告,请使用 ShowResult
结果实现回调方法以检查用户是否完成了广告,是否应予以奖励。
激励广告按钮
使用一个按钮提示玩家选择观看广告是激励视频广告的常见实现方式。使用以下步骤中的示例代码创建一个激励广告按钮。只要有广告内容可用,按钮就会在按下时显示广告。
要在 Unity 编辑器中配置按钮
- 选择 Game Object > UI > Button,将按钮添加到您的场景中。
- 选择您添加到场景中的按钮,然后使用检查器(添加组件 > 新建脚本)向其添加脚本组件。将脚本命名为
RewardedAdsButton
以与类名匹配。 - 打开脚本并添加以下代码
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
[SerializeField] Button _showAdButton;
[SerializeField] string _androidAdUnitId = "Rewarded_Android";
[SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
string _adUnitId = null; // This will remain null for unsupported platforms
void Awake()
{
// Get the Ad Unit ID for the current platform:
#if UNITY_IOS
_adUnitId = _iOSAdUnitId;
#elif UNITY_ANDROID
_adUnitId = _androidAdUnitId;
#endif
// Disable the button until the ad is ready to show:
_showAdButton.interactable = false;
}
// Call this public method when you want to get an ad ready to show.
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);
}
// If the ad successfully loads, add a listener to the button and enable it:
public void OnUnityAdsAdLoaded(string adUnitId)
{
Debug.Log("Ad Loaded: " + adUnitId);
if (adUnitId.Equals(_adUnitId))
{
// Configure the button to call the ShowAd() method when clicked:
_showAdButton.onClick.AddListener(ShowAd);
// Enable the button for users to click:
_showAdButton.interactable = true;
}
}
// Implement a method to execute when the user clicks the button:
public void ShowAd()
{
// Disable the button:
_showAdButton.interactable = false;
// Then show the ad:
Advertisement.Show(_adUnitId, this);
}
// Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
{
if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
{
Debug.Log("Unity Ads Rewarded Ad Completed");
// Grant a reward.
}
}
// Implement Load and Show Listener error callbacks:
public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
{
Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
// Use the error details to determine whether to try to load another ad.
}
public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
{
Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
// Use the error details to determine whether to try to load another ad.
}
public void OnUnityAdsShowStart(string adUnitId) { }
public void OnUnityAdsShowClick(string adUnitId) { }
void OnDestroy()
{
// Clean up the button listeners:
_showAdButton.onClick.RemoveAllListeners();
}
}
下一步:请参阅 在 Unity 中实现横幅广告。