如何解决“Admob 离线性能问题”?
How to fix " Admob offline performance problem"?
请帮忙。我创建了一个脚本,通过统一查看来自 Admob 的奖励基础广告,为用户添加简历功能。当用户在线时它工作正常。但当用户设备处于离线模式时性能太慢(无法播放)。
当脚本被禁用时,游戏在线和离线都能正常工作。
问题可能出在 RequestRewardBasedVideo() 函数中。
using System;
using UnityEngine;
using GoogleMobileAds.Api;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class RewardAdResume : MonoBehaviour
{
private string app_id = "....................";
private RewardBasedVideoAd rewardBasedVideo;
public rewdResume RewdResume;
public bool adClosed;
public Text txt2;
public GameObject ops;
public GameObject rw1;
public GameObject rw2;
public GameObject rw3;
public bool opsld;
public GameObject opsLdImg;
// Start is called before the first frame update
void Start()
{
//on publish enable
//MobileAds.Initialize(app_id);
adClosed = false;
opsld = false;
this.RequestRewardBasedVideo();
}
void Update()
{
if (adClosed)
{
if (rewdResume.resumes)
{
txt2.text = adClosed + " Rewarded " + rewdResume.resumes;
RewdResume.resume();
rewdResume.resumes = false;
}
else
{
roverCore.adclosed = true;
}
adClosed = false;
}
if (opsld) {
opsLdImg.GetComponent<Image>().fillAmount += Time.deltaTime * 0.2f;
if (opsLdImg.GetComponent<Image>().fillAmount == 1) {
roverCore.adclosed = true;
ops.SetActive(false);
rw1.SetActive(true);
rw2.SetActive(true);
rw3.SetActive(true);
opsld = false;
}
}
}
private void RequestRewardBasedVideo()
{
string adUnitId = "ca-app-pub-3940256099942544/5224354917";
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
// Called when an ad request has successfully loaded.
rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
// Called when an ad request failed to load.
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
// Called when an ad is shown.
rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
// Called when the ad starts to play.
rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
// Called when the user should be rewarded for watching a video.
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
// Called when the ad is closed.
rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
// Called when the ad click caused the user to leave the application.
rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
// Create an empty ad request.
AdRequest adRequest = new AdRequest.Builder().AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build();
// Load the rewarded video ad with the request.
this.rewardBasedVideo.LoadAd(adRequest, adUnitId);
}
public void UserOptToWatchAd()
{
if (rewardBasedVideo.IsLoaded())
{
roverCore.showChanceTimeout = false;
rewardBasedVideo.Show();
}
else {
ops.SetActive(true);
rw1.SetActive(false);
rw2.SetActive(false);
rw3.SetActive(false);
opsld = true;
//roverCore.adclosed = true;
}
}
#region rewd event
public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
this.RequestRewardBasedVideo();
}
public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
adClosed = true;
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
rewdResume.resumes = true;
}
public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
#endregion
private void OnDisable()
{
// Called when an ad request has successfully loaded.
rewardBasedVideo.OnAdLoaded -= HandleRewardBasedVideoLoaded;
// Called when an ad request failed to load.
rewardBasedVideo.OnAdFailedToLoad -= HandleRewardBasedVideoFailedToLoad;
// Called when an ad is shown.
rewardBasedVideo.OnAdOpening -= HandleRewardBasedVideoOpened;
// Called when the ad starts to play.
rewardBasedVideo.OnAdStarted -= HandleRewardBasedVideoStarted;
// Called when the user should be rewarded for watching a video.
rewardBasedVideo.OnAdRewarded -= HandleRewardBasedVideoRewarded;
// Called when the ad is closed.
rewardBasedVideo.OnAdClosed -= HandleRewardBasedVideoClosed;
// Called when the ad click caused the user to leave the application.
rewardBasedVideo.OnAdLeavingApplication -= HandleRewardBasedVideoLeftApplication;
}
}
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
this.RequestRewardBasedVideo();
}
当用户设备处于离线状态时,激励广告几乎每秒都无法加载。所以,你会发送很多请求。这可能会导致 FPS 下降。
如果我是你,我会启动一个协程来请求一个新的奖励视频并等待至少 30 秒。请求新的激励广告。
请帮忙。我创建了一个脚本,通过统一查看来自 Admob 的奖励基础广告,为用户添加简历功能。当用户在线时它工作正常。但当用户设备处于离线模式时性能太慢(无法播放)。
当脚本被禁用时,游戏在线和离线都能正常工作。 问题可能出在 RequestRewardBasedVideo() 函数中。
using System;
using UnityEngine;
using GoogleMobileAds.Api;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class RewardAdResume : MonoBehaviour
{
private string app_id = "....................";
private RewardBasedVideoAd rewardBasedVideo;
public rewdResume RewdResume;
public bool adClosed;
public Text txt2;
public GameObject ops;
public GameObject rw1;
public GameObject rw2;
public GameObject rw3;
public bool opsld;
public GameObject opsLdImg;
// Start is called before the first frame update
void Start()
{
//on publish enable
//MobileAds.Initialize(app_id);
adClosed = false;
opsld = false;
this.RequestRewardBasedVideo();
}
void Update()
{
if (adClosed)
{
if (rewdResume.resumes)
{
txt2.text = adClosed + " Rewarded " + rewdResume.resumes;
RewdResume.resume();
rewdResume.resumes = false;
}
else
{
roverCore.adclosed = true;
}
adClosed = false;
}
if (opsld) {
opsLdImg.GetComponent<Image>().fillAmount += Time.deltaTime * 0.2f;
if (opsLdImg.GetComponent<Image>().fillAmount == 1) {
roverCore.adclosed = true;
ops.SetActive(false);
rw1.SetActive(true);
rw2.SetActive(true);
rw3.SetActive(true);
opsld = false;
}
}
}
private void RequestRewardBasedVideo()
{
string adUnitId = "ca-app-pub-3940256099942544/5224354917";
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
// Called when an ad request has successfully loaded.
rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
// Called when an ad request failed to load.
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
// Called when an ad is shown.
rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
// Called when the ad starts to play.
rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
// Called when the user should be rewarded for watching a video.
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
// Called when the ad is closed.
rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
// Called when the ad click caused the user to leave the application.
rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
// Create an empty ad request.
AdRequest adRequest = new AdRequest.Builder().AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build();
// Load the rewarded video ad with the request.
this.rewardBasedVideo.LoadAd(adRequest, adUnitId);
}
public void UserOptToWatchAd()
{
if (rewardBasedVideo.IsLoaded())
{
roverCore.showChanceTimeout = false;
rewardBasedVideo.Show();
}
else {
ops.SetActive(true);
rw1.SetActive(false);
rw2.SetActive(false);
rw3.SetActive(false);
opsld = true;
//roverCore.adclosed = true;
}
}
#region rewd event
public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
this.RequestRewardBasedVideo();
}
public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
adClosed = true;
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
rewdResume.resumes = true;
}
public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
#endregion
private void OnDisable()
{
// Called when an ad request has successfully loaded.
rewardBasedVideo.OnAdLoaded -= HandleRewardBasedVideoLoaded;
// Called when an ad request failed to load.
rewardBasedVideo.OnAdFailedToLoad -= HandleRewardBasedVideoFailedToLoad;
// Called when an ad is shown.
rewardBasedVideo.OnAdOpening -= HandleRewardBasedVideoOpened;
// Called when the ad starts to play.
rewardBasedVideo.OnAdStarted -= HandleRewardBasedVideoStarted;
// Called when the user should be rewarded for watching a video.
rewardBasedVideo.OnAdRewarded -= HandleRewardBasedVideoRewarded;
// Called when the ad is closed.
rewardBasedVideo.OnAdClosed -= HandleRewardBasedVideoClosed;
// Called when the ad click caused the user to leave the application.
rewardBasedVideo.OnAdLeavingApplication -= HandleRewardBasedVideoLeftApplication;
}
}
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
this.RequestRewardBasedVideo();
}
当用户设备处于离线状态时,激励广告几乎每秒都无法加载。所以,你会发送很多请求。这可能会导致 FPS 下降。
如果我是你,我会启动一个协程来请求一个新的奖励视频并等待至少 30 秒。请求新的激励广告。