声明UnityADS命名空间,UnityEngine.Advertisement在脚本的标题中(请参见广告文档):
- using UnityEngine.Advertisements;
- 调用Show()以显示广告的功能:Advertisement.show(S)()
奖励参与广告的玩家会增加用户参与,从而带来更高的收入。例如,游戏可能会奖励具有游戏货币、消耗品、更多生命或经验的玩家。
若要奖励完成视频广告的玩家,请使用HandleShowResult以下示例中的回调方法。一定要检查result等于ShowResult.Finished以验证用户未跳过AD。
在脚本中,添加回调方法。
调用时将方法作为参数传递Show()。
呼叫(C)Show()使用"rewardedVideo"放置以使视频不可跳。
void ShowRewardedVideo ()
{
ShowOptions options # new ShowOptions();
options.resultCallback # HandleShowResult;
Advertisement.Show("rewardedVideo", options);
}
void HandleShowResult (ShowResult result)
{
if(result == ShowResult.Finished) {
Debug.Log("Video completed - Offer a reward to the player");
// Reward your player here.
}else if(result == ShowResult.Skipped) {
Debug.LogWarning("Video was skipped - Do NOT reward the player");
}else if(result == ShowResult.Failed) {
Debug.LogError("Video failed to show");
}
}
示例奖励广告按钮代码
- 使用以下代码创建“奖励广告”按钮。只要广告可用,“广告”按钮就会显示广告。
- 选择(Select)游戏对象"用户界面(UI)"按钮将按钮添加到您的场景(Scene)。
- 选择要添加到场景中的按钮,然后使用checker(在checker中,选择添加组件"新建脚本。)
- 打开脚本并添加以下代码:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
//---------- ONLY NECESSARY FOR ASSET PACKAGE INTEGRATION: ----------//
#ifUNITY_IOS
private string gameId = "1486551";
#elifUNITY_ANDROID
private string gameId = "1486550";
#endif
//-------------------------------------------------------------------//
[RequireComponent([Shìpín guǎnggào](Button))]
public class UnityAdsButton : MonoBehaviour
{
Button m_Button;
public string placementId = "rewardedVideo";
void Start ()
{
m_Button = GetComponent<Button>();
if (m_Button) m_Button.onClick.AddListener(ShowAd);
//---------- ONLY NECESSARY FOR ASSET PACKAGE INTEGRATION: ----------//
if (Advertisement.isSupported) {
Advertisement.Initialize (gameId, true);
}
//-------------------------------------------------------------------//
}
void Update ()
{
if (m_Button) m_Button.interactable = Advertisement.IsReady(placementId);
}
void ShowAd ()
{
ShowOptions options = new ShowOptions();
options.resultCallback = HandleShowResult;
Advertisement.Show(placementId, options);
}
void HandleShowResult (ShowResult result)
{
if(result == ShowResult.Finished) {
Debug.Log("Video completed - Offer a reward to the player");
}else if(result == ShowResult.Skipped) {
Debug.LogWarning("Video was skipped - Do NOT reward the player");
}else if(result == ShowResult.Failed) {
Debug.LogError("Video failed to show");
}
}
}