我如何将 admob 奖励广告实施为 unity
How do I implement admob rewarded ads into unity
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 按钮调用的方法。调用该方法时,广告没有出现在我的 phone 上(它适用于演示脚本和横幅等。我已成功导入所有 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();
}
}
//当视频广告播放完毕后调用此函数您可以在这里给玩家奖励金额变量。
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);
}
我在用这个插件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();
}
}
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 按钮调用的方法。调用该方法时,广告没有出现在我的 phone 上(它适用于演示脚本和横幅等。我已成功导入所有 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();
}
}
//当视频广告播放完毕后调用此函数您可以在这里给玩家奖励金额变量。
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);
}
我在用这个插件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();
}
}