如何修复Admob离线性能问题"? [英] How to fix " Admob offline performance problem"?

查看:138
本文介绍了如何修复Admob离线性能问题"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助.我创建了一个脚本,可以通过统一查看Admob的奖励基础广告来为用户添加恢复功能.用户在线时工作正常.但是当用户设备处于离线模式时,性能太慢(无法播放).

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.

禁用脚本后,在线和离线游戏都可以正常运行. 问题可能出在RequestRewardBasedVideo()函数中.

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;
    }
}

推荐答案

rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;

public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
 {
     this.RequestRewardBasedVideo();
 }

当用户设备离线时,奖励广告几乎每秒都无法加载.因此,您将发送很多请求.这可能会导致FPS下降.

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.

如果我是您,我将启动协程请求新的奖励视频,并等待至少30秒.请求一个新的奖励广告.

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.

这篇关于如何修复Admob离线性能问题"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆