问题
Help please. I have created a script to add a resume funtion for users by seeing an reward base ad from Admob in unity. It works fine when user is online. but performance is too slow (not playable) when user device is in offline mode.
When the script is disable game works fine in both online and offline. The problem maybe in RequestRewardBasedVideo() funtion.
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;
}
}
回答1:
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
this.RequestRewardBasedVideo();
}
When user device is offline, rewarded ad will fail to load almost every sec. So, you will send a lot of requests. That may cause FPS drops.
If i were you, i would start a coroutine for requesting a new rewarded video and wait at least 30 sec. to request a new rewarded ad.
来源:https://stackoverflow.com/questions/55947850/how-to-fix-admob-offline-performance-problem