Unity + Admob 奖励广告:事件未触发
Unity + Admob rewarded ads : event not firing
我受困于 admob 奖励广告,我不知道如何让活动正常进行。问题是我的问答游戏会在每个问题时重新加载场景,即使我阻止广告被破坏,事件也根本不会触发。广告展示完美。我尝试了很多东西,但我一定在某个地方犯了一个错误……有人有想法吗?
非常感谢!
using System;
using System.Collections;
using UnityEngine;
using GoogleMobileAds.Api;
public class RewardedScriptRow : MonoBehaviour
{
private RewardBasedVideoAd rewardBasedVideo;
public AudioClip GiftSound;
// Use this for initialization
void Start()
{
RequestInterstitial();
Debug.Log("Load at start");
}
public void LaunchAd() //Called from another script
{
StartCoroutine("Load");
}
private void RequestInterstitial()
{
string adUnitId = "";
#if UNITY_ANDROID
adUnitId = "ca-app-pub-00000/00000000";
#elif UNITY_IOS
adUnitId = "ca-app-pub-0000000000000";
#else
adUnitId = "unexpected_platform";
#endif
// Get singleton reward based video ad reference.
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
this.rewardBasedVideo.LoadAd(request, adUnitId);
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoClosed;
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
IEnumerator Load()
{
while (!rewardBasedVideo.IsLoaded())
yield return new WaitForEndOfFrame();
yield return new WaitForSeconds(0.0f);
rewardBasedVideo.Show();
yield break;
}
//EVENT
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
RequestInterstitial();
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
RequestInterstitial();
}
}
编辑 1:
using System;
using System.Collections;
using UnityEngine;
using GoogleMobileAds.Api;
public class RewardedScriptRow : MonoBehaviour
{
private RewardBasedVideoAd rewardBasedVideo;
public AudioClip GiftSound;
public static RewardedScriptRow Instance;
// Use this for initialization
void Start()
{
Instance = this;
DontDestroyOnLoad(this);
RequestRewardBasedVideo();
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoClosed;
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
//Called after 10 questions
public void LaunchAd()
{
StartCoroutine("Load");
}
private void RequestRewardBasedVideo()
{
string adUnitId = "";
#if UNITY_ANDROID
adUnitId = "ca-app-pub-0000000/0000000000";
#elif UNITY_IOS
adUnitId = "ca-app-pub-00000/00000000";
#else
adUnitId = "unexpected_platform";
#endif
// Get singleton reward based video ad reference.
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
this.rewardBasedVideo.LoadAd(request, adUnitId);
}
//EVENT
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
RequestRewardBasedVideo();
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
RequestRewardBasedVideo();
}
IEnumerator Load()
{
while (!rewardBasedVideo.IsLoaded())
yield return new WaitForEndOfFrame();
yield return new WaitForSeconds(0.0f);
rewardBasedVideo.Show();
yield break;
}
}
这就是游戏与场景的配合方式:
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
首先:
在您的事件中调用 RequestInterstial
方法似乎非常不明智/不合逻辑。因为这样做,您正在为您已经订阅的相同事件创建多个订阅!这可能会导致非常不希望的/不需要的行为以及导致 Whosebug exceptions
我不清楚为什么您甚至会在事件触发时调用 RequestInterstial
。在我看来,您可能希望在第一个视频放映后加载一个新视频。重构您的方法,以便您不添加订阅事件。
将订阅事件和初始化代码移至您的 Start 或 Awake 方法。
此外,您请求的不是插页式视频,而是基于奖励的视频。我建议重命名以保持代码的逻辑性。
Public static RewardedScriptRow Instance;
void Start()
{
Instance = this;
DontDestroyOnLoad(this);
RequestRewardBasedVideo();
// Get singleton reward based video ad reference.
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoClosed;
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
private void RequestRewardBasedVideo()
{
#if UNITY_ANDROID
string appId = "ca-app-pub-3940256099942544~3347511713";
#elif UNITY_IPHONE
string appId = "ca-app-pub-3940256099942544~1458002511";
#else
string appId = "unexpected_platform";
#endif
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded video ad with the request.
this.rewardBasedVideo.LoadAd(request, adUnitId);
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
RequestRewardBasedVideo();
}
除此之外,它应该可以工作。如果您仍然没有得到理想的结果,请尝试在调试时设置断点和/或在订阅的方法中使用 Debug.Log()
来查看发生了什么。
编辑:另外,如果是重载场景导致的,可以尝试添加DontDestroyOnLoad(this);
防止"AdObject"被破坏。我建议在您的第一个场景中创建此脚本并将其从所有其他场景中删除(以防止重复)。
然后您甚至可以应用 singleton pattern,这样您就可以轻松地从其他 类.
中访问脚本
示例:
StartCoroutine(RewardedScriptRow.Instance.LaunchAd());
我受困于 admob 奖励广告,我不知道如何让活动正常进行。问题是我的问答游戏会在每个问题时重新加载场景,即使我阻止广告被破坏,事件也根本不会触发。广告展示完美。我尝试了很多东西,但我一定在某个地方犯了一个错误……有人有想法吗?
非常感谢!
using System;
using System.Collections;
using UnityEngine;
using GoogleMobileAds.Api;
public class RewardedScriptRow : MonoBehaviour
{
private RewardBasedVideoAd rewardBasedVideo;
public AudioClip GiftSound;
// Use this for initialization
void Start()
{
RequestInterstitial();
Debug.Log("Load at start");
}
public void LaunchAd() //Called from another script
{
StartCoroutine("Load");
}
private void RequestInterstitial()
{
string adUnitId = "";
#if UNITY_ANDROID
adUnitId = "ca-app-pub-00000/00000000";
#elif UNITY_IOS
adUnitId = "ca-app-pub-0000000000000";
#else
adUnitId = "unexpected_platform";
#endif
// Get singleton reward based video ad reference.
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
this.rewardBasedVideo.LoadAd(request, adUnitId);
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoClosed;
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
IEnumerator Load()
{
while (!rewardBasedVideo.IsLoaded())
yield return new WaitForEndOfFrame();
yield return new WaitForSeconds(0.0f);
rewardBasedVideo.Show();
yield break;
}
//EVENT
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
RequestInterstitial();
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
RequestInterstitial();
}
}
编辑 1:
using System;
using System.Collections;
using UnityEngine;
using GoogleMobileAds.Api;
public class RewardedScriptRow : MonoBehaviour
{
private RewardBasedVideoAd rewardBasedVideo;
public AudioClip GiftSound;
public static RewardedScriptRow Instance;
// Use this for initialization
void Start()
{
Instance = this;
DontDestroyOnLoad(this);
RequestRewardBasedVideo();
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoClosed;
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
//Called after 10 questions
public void LaunchAd()
{
StartCoroutine("Load");
}
private void RequestRewardBasedVideo()
{
string adUnitId = "";
#if UNITY_ANDROID
adUnitId = "ca-app-pub-0000000/0000000000";
#elif UNITY_IOS
adUnitId = "ca-app-pub-00000/00000000";
#else
adUnitId = "unexpected_platform";
#endif
// Get singleton reward based video ad reference.
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
this.rewardBasedVideo.LoadAd(request, adUnitId);
}
//EVENT
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
RequestRewardBasedVideo();
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
RequestRewardBasedVideo();
}
IEnumerator Load()
{
while (!rewardBasedVideo.IsLoaded())
yield return new WaitForEndOfFrame();
yield return new WaitForSeconds(0.0f);
rewardBasedVideo.Show();
yield break;
}
}
这就是游戏与场景的配合方式:
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
首先:
在您的事件中调用 RequestInterstial
方法似乎非常不明智/不合逻辑。因为这样做,您正在为您已经订阅的相同事件创建多个订阅!这可能会导致非常不希望的/不需要的行为以及导致 Whosebug exceptions
我不清楚为什么您甚至会在事件触发时调用 RequestInterstial
。在我看来,您可能希望在第一个视频放映后加载一个新视频。重构您的方法,以便您不添加订阅事件。
将订阅事件和初始化代码移至您的 Start 或 Awake 方法。
此外,您请求的不是插页式视频,而是基于奖励的视频。我建议重命名以保持代码的逻辑性。
Public static RewardedScriptRow Instance;
void Start()
{
Instance = this;
DontDestroyOnLoad(this);
RequestRewardBasedVideo();
// Get singleton reward based video ad reference.
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoClosed;
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
private void RequestRewardBasedVideo()
{
#if UNITY_ANDROID
string appId = "ca-app-pub-3940256099942544~3347511713";
#elif UNITY_IPHONE
string appId = "ca-app-pub-3940256099942544~1458002511";
#else
string appId = "unexpected_platform";
#endif
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded video ad with the request.
this.rewardBasedVideo.LoadAd(request, adUnitId);
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
RequestRewardBasedVideo();
}
除此之外,它应该可以工作。如果您仍然没有得到理想的结果,请尝试在调试时设置断点和/或在订阅的方法中使用 Debug.Log()
来查看发生了什么。
编辑:另外,如果是重载场景导致的,可以尝试添加DontDestroyOnLoad(this);
防止"AdObject"被破坏。我建议在您的第一个场景中创建此脚本并将其从所有其他场景中删除(以防止重复)。
然后您甚至可以应用 singleton pattern,这样您就可以轻松地从其他 类.
中访问脚本示例:
StartCoroutine(RewardedScriptRow.Instance.LaunchAd());