如何在 Unity 应用程序中创建多个奖励视频?
How create multiple reward video's in Unity application?
最近几天我试图在我的 Unity 应用程序中实现奖励视频 (admob)。我想要人们可以观看多个奖励视频,并提供不同类型的奖励。我觉得我很接近(也许根本不是),因为我几乎可以正常工作。第一次点击测试广告时,它会显示广告、我获得奖励以及我获得奖励的消息。如果我然后 load/watch 第二个广告,它有效,但奖励不是应有的。我得到两个奖励。所以,我先看一个 100 个硬币的广告,我得到了 100 个硬币,效果非常好。然后,我看了 500 金币的广告,但我得到了 600 金币,而且我收到的消息都是 100 和 500 金币,虽然我只 'earned' 500 金币。看起来 HandleRewardBasedVideoRewardedAdMob 都存在错误,但我已经尝试了所有我能想到的方法,但我没有在互联网上找到任何类似的东西。我用小的奖励 100 个金币,用大的奖励 500 个金币。
我希望有人能帮助我,因为它让我发疯。感谢您的宝贵时间!
using UnityEngine;
using GoogleMobileAds.Api;
using System;
public class AdsManager : MonoBehaviour {
#region AdMob
[Header("Admob")]
private string adMobAppID = "";
private string videoAdMobIdsmall = "ca-app-pub-3940256099942544/5224354917";
private string videoAdMobIdbig = "ca-app-pub-3940256099942544/5224354917";
private RewardBasedVideoAd rewardBasedAdMobVideosmall;
private RewardBasedVideoAd rewardBasedAdMobVideobig;
AdRequest AdMobVideoRequestsmall;
AdRequest AdMobVideoRequestbig;
#endregion
[Space(15)]
public decimal moneyToAddsmall;
public decimal moneyToAddbig;
static AdsManager instance;
public static AdsManager Instance
{
get
{
if(instance == null)
instance = GameObject.FindObjectOfType(typeof(AdsManager)) as AdsManager;
return instance;
}
}
void Awake ()
{
gameObject.name = this.GetType().Name;
DontDestroyOnLoad(gameObject);
InitializeAds();
}
public void ShowVideoRewardsmall() {
moneyToAddsmall = 100;
if(rewardBasedAdMobVideosmall.IsLoaded())
{
AdMobShowVideosmall();
}
}
public void ShowVideoRewardbig() {
moneyToAddbig = 500;
if(rewardBasedAdMobVideobig.IsLoaded())
{
AdMobShowVideobig();
}
}
private void RequestRewardedVideosmall()
{
// Called when an ad request has successfully loaded.
rewardBasedAdMobVideosmall.OnAdLoaded += HandleRewardBasedVideoLoadedAdMobsmall;
// Called when an ad request failed to load.
rewardBasedAdMobVideosmall.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoadAdMobsmall;
// Called when an ad is shown.
rewardBasedAdMobVideosmall.OnAdOpening += HandleRewardBasedVideoOpenedAdMobsmall;
// Called when the ad starts to play.
rewardBasedAdMobVideosmall.OnAdStarted += HandleRewardBasedVideoStartedAdMobsmall;
// Called when the user should be rewarded for watching a video.
rewardBasedAdMobVideosmall.OnAdRewarded += HandleRewardBasedVideoRewardedAdMobsmall;
// Called when the ad is closed.
rewardBasedAdMobVideosmall.OnAdClosed += HandleRewardBasedVideoClosedAdMobsmall;
// Called when the ad click caused the user to leave the application.
rewardBasedAdMobVideosmall.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplicationAdMobsmall;
// Create an empty ad request.
AdMobVideoRequestsmall = new AdRequest.Builder().Build();
// Load the rewarded video ad with the request.
this.rewardBasedAdMobVideosmall.LoadAd(AdMobVideoRequestsmall, videoAdMobIdsmall);
}
public void HandleRewardBasedVideoLoadedAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoadAdMobsmall(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
}
public void HandleRewardBasedVideoOpenedAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStartedAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosedAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
this.rewardBasedAdMobVideosmall.LoadAd(AdMobVideoRequestsmall, videoAdMobIdsmall);
}
public void HandleRewardBasedVideoRewardedAdMobsmall(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
Statistics._instance.AddMoney(moneyToAddsmall);
Toast.instance.ShowMessage("Congratulations with your 100 coins!", 4);
MonoBehaviour.print("HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " + type);
}
public void HandleRewardBasedVideoLeftApplicationAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
private void RequestRewardedVideobig()
{
// Called when an ad request has successfully loaded.
rewardBasedAdMobVideobig.OnAdLoaded += HandleRewardBasedVideoLoadedAdMobbig;
// Called when an ad request failed to load.
rewardBasedAdMobVideobig.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoadAdMobbig;
// Called when an ad is shown.
rewardBasedAdMobVideobig.OnAdOpening += HandleRewardBasedVideoOpenedAdMobbig;
// Called when the ad starts to play.
rewardBasedAdMobVideobig.OnAdStarted += HandleRewardBasedVideoStartedAdMobbig;
// Called when the user should be rewarded for watching a video.
rewardBasedAdMobVideobig.OnAdRewarded += HandleRewardBasedVideoRewardedAdMobbig;
// Called when the ad is closed.
rewardBasedAdMobVideobig.OnAdClosed += HandleRewardBasedVideoClosedAdMobbig;
// Called when the ad click caused the user to leave the application.
rewardBasedAdMobVideobig.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplicationAdMobbig;
// Create an empty ad request.
AdMobVideoRequestbig = new AdRequest.Builder().Build();
// Load the rewarded video ad with the request.
this.rewardBasedAdMobVideobig.LoadAd(AdMobVideoRequestbig, videoAdMobIdbig);
}
public void HandleRewardBasedVideoLoadedAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoadAdMobbig(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
}
public void HandleRewardBasedVideoOpenedAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStartedAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosedAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
this.rewardBasedAdMobVideosmall.LoadAd(AdMobVideoRequestbig, videoAdMobIdsmall);
}
public void HandleRewardBasedVideoRewardedAdMobbig(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
Statistics._instance.AddMoney(moneyToAddbig);
Toast.instance.ShowMessage("Congratulations with your 500 coins!", 4);
MonoBehaviour.print("HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " + type);
}
public void HandleRewardBasedVideoLeftApplicationAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
void InitializeAds()
{
MobileAds.Initialize(adMobAppID);
this.rewardBasedAdMobVideosmall = RewardBasedVideoAd.Instance;
this.RequestRewardedVideosmall();
this.rewardBasedAdMobVideobig = RewardBasedVideoAd.Instance;
this.RequestRewardedVideobig();
}
void AdMobShowVideosmall()
{
rewardBasedAdMobVideosmall.Show();
}
void AdMobShowVideobig()
{
rewardBasedAdMobVideobig.Show();
}
bool isVideoAvaiable()
{
#if !UNITY_EDITOR
if(rewardBasedAdMobVideosmall.IsLoaded())
{
return true;
}
#endif
return false;
}
bool isVideoAvaiablebig()
{
#if !UNITY_EDITOR
if(rewardBasedAdMobVideobig.IsLoaded())
{
return true;
}
#endif
return false;
}
}
有了标准的统一广告库,这一切就容易多了,为什么不使用那个呢
这是带有奖励广告的统一广告:
https://unityads.unity3d.com/help/unity/integration-guide-unity#rewarded-video-ads
RewardBasedVideoAd 是一个单例对象(不同于 BannerView 和 InterstitialAd)。在您的 InitializeAds 方法中,您将同一对象分配给 RequestRewardedVideosmall 和 rewardBasedAdMobVideobig。您一次不能请求多个 RewardBasedVideoAd。
您可以阅读更多相关信息here
Because RewardedBasedVideoAd is a singleton, requests to load an ad
should be made using a shared instance.
最近几天我试图在我的 Unity 应用程序中实现奖励视频 (admob)。我想要人们可以观看多个奖励视频,并提供不同类型的奖励。我觉得我很接近(也许根本不是),因为我几乎可以正常工作。第一次点击测试广告时,它会显示广告、我获得奖励以及我获得奖励的消息。如果我然后 load/watch 第二个广告,它有效,但奖励不是应有的。我得到两个奖励。所以,我先看一个 100 个硬币的广告,我得到了 100 个硬币,效果非常好。然后,我看了 500 金币的广告,但我得到了 600 金币,而且我收到的消息都是 100 和 500 金币,虽然我只 'earned' 500 金币。看起来 HandleRewardBasedVideoRewardedAdMob 都存在错误,但我已经尝试了所有我能想到的方法,但我没有在互联网上找到任何类似的东西。我用小的奖励 100 个金币,用大的奖励 500 个金币。 我希望有人能帮助我,因为它让我发疯。感谢您的宝贵时间!
using UnityEngine;
using GoogleMobileAds.Api;
using System;
public class AdsManager : MonoBehaviour {
#region AdMob
[Header("Admob")]
private string adMobAppID = "";
private string videoAdMobIdsmall = "ca-app-pub-3940256099942544/5224354917";
private string videoAdMobIdbig = "ca-app-pub-3940256099942544/5224354917";
private RewardBasedVideoAd rewardBasedAdMobVideosmall;
private RewardBasedVideoAd rewardBasedAdMobVideobig;
AdRequest AdMobVideoRequestsmall;
AdRequest AdMobVideoRequestbig;
#endregion
[Space(15)]
public decimal moneyToAddsmall;
public decimal moneyToAddbig;
static AdsManager instance;
public static AdsManager Instance
{
get
{
if(instance == null)
instance = GameObject.FindObjectOfType(typeof(AdsManager)) as AdsManager;
return instance;
}
}
void Awake ()
{
gameObject.name = this.GetType().Name;
DontDestroyOnLoad(gameObject);
InitializeAds();
}
public void ShowVideoRewardsmall() {
moneyToAddsmall = 100;
if(rewardBasedAdMobVideosmall.IsLoaded())
{
AdMobShowVideosmall();
}
}
public void ShowVideoRewardbig() {
moneyToAddbig = 500;
if(rewardBasedAdMobVideobig.IsLoaded())
{
AdMobShowVideobig();
}
}
private void RequestRewardedVideosmall()
{
// Called when an ad request has successfully loaded.
rewardBasedAdMobVideosmall.OnAdLoaded += HandleRewardBasedVideoLoadedAdMobsmall;
// Called when an ad request failed to load.
rewardBasedAdMobVideosmall.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoadAdMobsmall;
// Called when an ad is shown.
rewardBasedAdMobVideosmall.OnAdOpening += HandleRewardBasedVideoOpenedAdMobsmall;
// Called when the ad starts to play.
rewardBasedAdMobVideosmall.OnAdStarted += HandleRewardBasedVideoStartedAdMobsmall;
// Called when the user should be rewarded for watching a video.
rewardBasedAdMobVideosmall.OnAdRewarded += HandleRewardBasedVideoRewardedAdMobsmall;
// Called when the ad is closed.
rewardBasedAdMobVideosmall.OnAdClosed += HandleRewardBasedVideoClosedAdMobsmall;
// Called when the ad click caused the user to leave the application.
rewardBasedAdMobVideosmall.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplicationAdMobsmall;
// Create an empty ad request.
AdMobVideoRequestsmall = new AdRequest.Builder().Build();
// Load the rewarded video ad with the request.
this.rewardBasedAdMobVideosmall.LoadAd(AdMobVideoRequestsmall, videoAdMobIdsmall);
}
public void HandleRewardBasedVideoLoadedAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoadAdMobsmall(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
}
public void HandleRewardBasedVideoOpenedAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStartedAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosedAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
this.rewardBasedAdMobVideosmall.LoadAd(AdMobVideoRequestsmall, videoAdMobIdsmall);
}
public void HandleRewardBasedVideoRewardedAdMobsmall(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
Statistics._instance.AddMoney(moneyToAddsmall);
Toast.instance.ShowMessage("Congratulations with your 100 coins!", 4);
MonoBehaviour.print("HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " + type);
}
public void HandleRewardBasedVideoLeftApplicationAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
private void RequestRewardedVideobig()
{
// Called when an ad request has successfully loaded.
rewardBasedAdMobVideobig.OnAdLoaded += HandleRewardBasedVideoLoadedAdMobbig;
// Called when an ad request failed to load.
rewardBasedAdMobVideobig.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoadAdMobbig;
// Called when an ad is shown.
rewardBasedAdMobVideobig.OnAdOpening += HandleRewardBasedVideoOpenedAdMobbig;
// Called when the ad starts to play.
rewardBasedAdMobVideobig.OnAdStarted += HandleRewardBasedVideoStartedAdMobbig;
// Called when the user should be rewarded for watching a video.
rewardBasedAdMobVideobig.OnAdRewarded += HandleRewardBasedVideoRewardedAdMobbig;
// Called when the ad is closed.
rewardBasedAdMobVideobig.OnAdClosed += HandleRewardBasedVideoClosedAdMobbig;
// Called when the ad click caused the user to leave the application.
rewardBasedAdMobVideobig.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplicationAdMobbig;
// Create an empty ad request.
AdMobVideoRequestbig = new AdRequest.Builder().Build();
// Load the rewarded video ad with the request.
this.rewardBasedAdMobVideobig.LoadAd(AdMobVideoRequestbig, videoAdMobIdbig);
}
public void HandleRewardBasedVideoLoadedAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoadAdMobbig(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
}
public void HandleRewardBasedVideoOpenedAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStartedAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosedAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
this.rewardBasedAdMobVideosmall.LoadAd(AdMobVideoRequestbig, videoAdMobIdsmall);
}
public void HandleRewardBasedVideoRewardedAdMobbig(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
Statistics._instance.AddMoney(moneyToAddbig);
Toast.instance.ShowMessage("Congratulations with your 500 coins!", 4);
MonoBehaviour.print("HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " + type);
}
public void HandleRewardBasedVideoLeftApplicationAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
void InitializeAds()
{
MobileAds.Initialize(adMobAppID);
this.rewardBasedAdMobVideosmall = RewardBasedVideoAd.Instance;
this.RequestRewardedVideosmall();
this.rewardBasedAdMobVideobig = RewardBasedVideoAd.Instance;
this.RequestRewardedVideobig();
}
void AdMobShowVideosmall()
{
rewardBasedAdMobVideosmall.Show();
}
void AdMobShowVideobig()
{
rewardBasedAdMobVideobig.Show();
}
bool isVideoAvaiable()
{
#if !UNITY_EDITOR
if(rewardBasedAdMobVideosmall.IsLoaded())
{
return true;
}
#endif
return false;
}
bool isVideoAvaiablebig()
{
#if !UNITY_EDITOR
if(rewardBasedAdMobVideobig.IsLoaded())
{
return true;
}
#endif
return false;
}
}
有了标准的统一广告库,这一切就容易多了,为什么不使用那个呢
这是带有奖励广告的统一广告: https://unityads.unity3d.com/help/unity/integration-guide-unity#rewarded-video-ads
RewardBasedVideoAd 是一个单例对象(不同于 BannerView 和 InterstitialAd)。在您的 InitializeAds 方法中,您将同一对象分配给 RequestRewardedVideosmall 和 rewardBasedAdMobVideobig。您一次不能请求多个 RewardBasedVideoAd。
您可以阅读更多相关信息here
Because RewardedBasedVideoAd is a singleton, requests to load an ad should be made using a shared instance.