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();
}
}
在上面的代码中我已经尝试将adob奖励视频广告实施到我的游戏中,这是一个从ui按钮按下的方法。当调用该方法时,广告没有出现在我的手机上(它适用于演示脚本和横幅等工作。我已成功导入所有SDK和其他文件)。我在这里做错了什么,我该如何妥善处理。另外,我如何检查广告何时完成,以便我可以奖励用户?
答案 0 :(得分:1)
您需要使用RewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
订阅OnAdRewarded广告事件。
只需从RequestRewardBasedVideo()
功能调用Start
即可对此进行测试。以下代码来自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();
}
}
//视频广告完成后调用此功能您可以使用金额变量奖励玩家。
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);
}
答案 1 :(得分:0)
我使用此插件https://github.com/unity-plugins/Unity-Admob 和这样的代码。
加载视频
Admob.Instance().loadRewardedVideo("ca-app-pub-3940256099942544/1712485313");
在游戏结束时显示,在显示视频之前检查视频是否准备就绪:
if (Admob.Instance().isRewardedVideoReady()) {
Admob.Instance().showRewardedVideo();
}
处理广告事件
Admob.Instance().videoEventHandler += onInterstitialEvent;
void onInterstitialEvent(string eventName, string msg)
{
Debug.Log("handler onAdmobEvent---" + eventName + " " + msg);
if (eventName == AdmobEvent.onAdLoaded)
{
Admob.Instance().showRewardedVideo();
}
}