gpt4 book ai didi

android - 我如何将 admob 奖励广告实现为 unity

转载 作者:行者123 更新时间:2023-11-30 01:19:53 24 4
gpt4 key购买 nike

using UnityEngine;
using System.Collections;
using GoogleMobileAds;
using GoogleMobileAds.Api;
using UnityEngine.Advertisements;

public class GameAdvertising : MonoBehaviour {

public RewardBasedVideoAd rewardBasedVideo;
bool hasPlayed;

void Start () {
rewardBasedVideo = RewardBasedVideoAd.Instance;
}

public void playAd()
{
AdRequest rewardrequest = new AdRequest.Builder().Build();
rewardBasedVideo.LoadAd(rewardrequest, "ca-app-pub-5920324855307233/4458481507");
rewardBasedVideo.Show();
}
}

在我上面的代码中,我已经尝试在我的游戏中实现 admob 奖励视频广告,该方法是通过按下 ui 按钮调用的方法。调用该方法时,广告没有出现在我的手机上(它适用于演示脚本和横幅等。我已成功导入所有 SDK 和其他文件)。我在这里做错了什么以及如何正确放置它。另外,我如何检查广​​告何时结束以便奖励用户?

最佳答案

您需要使用 RewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded; 订阅 OnAdRewarded 广告事件。

只需从 Start 函数调用 RequestRewardBasedVideo() 即可对此进行测试。下面这段代码来自here我对其进行了一些修改。

private void RequestRewardBasedVideo()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "INSERT_AD_UNIT_HERE";
#elif UNITY_IPHONE
string adUnitId = "INSERT_AD_UNIT_HERE";
#else
string adUnitId = "unexpected_platform";
#endif

RewardBasedVideoAd rewardBasedVideo = RewardBasedVideoAd.Instance;

AdRequest request = new AdRequest.Builder().Build();
rewardBasedVideo.LoadAd(request, adUnitId);

//Show Ad
showAdd(rewardBasedVideo);
}

private void showAdd(RewardBasedVideoAd rewardBasedVideo)
{
if (rewardBasedVideo.IsLoaded())
{
//Subscribe to Ad event
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
rewardBasedVideo.Show();
}
}

//当视频广告播放结束时调用此函数您可以在此处使用 amount 变量奖励玩家。

public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
//Reawrd User here
print("User rewarded with: " + amount.ToString() + " " + type);
}

关于android - 我如何将 admob 奖励广告实现为 unity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37263387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com