【问题标题】:Where should I add the interstitial ad?我应该在哪里添加插页式广告?
【发布时间】:2020-11-17 07:55:35
【问题描述】:

我设法测试了一个简单的应用,并且广告(横幅和插页式)按预期工作(测试的,因为真实的没有,我知道为什么)。
问题是我想将广告集成到我的主应用程序中。我是为横幅做的,它可以工作,但不知道如何处理插页式广告。对于测试应用程序,我在按下按钮时调用了一个函数,但在我的应用程序中,我不希望按下按钮来显示插页式广告,我希望每次玩家死亡时都会发生这种情况。而且我不知道该怎么做。

using System;
using UnityEngine;
using GoogleMobileAds.Api;

public class ads : MonoBehaviour
{
    private BannerView bannerView;
    private InterstitialAd interstitial;
    private void RequestBanner()...
    private void RequestInterstitial()
    {
        string adUnitId = "ca-app-pub-3940256099942544/1033173712";
        this.interstitial = new InterstitialAd(adUnitId);
        AdRequest request = new AdRequest.Builder().Build();
        this.interstitial.LoadAd(request);
    }
    public void GameOverSoShowInterstitial()
    {
        if (this.interstitial.IsLoaded())
        {
            this.interstitial.Show();
        }
    }
    void Start()
    {
        MobileAds.Initialize(initStatus => { });
        this.RequestBanner();
        this.RequestInterstitial();
    }
}

^这是我的广告脚本 但它位于second scene named Menu,但由于插页式应该在玩家死亡时出现,我应该将那个游戏对象移动到other scene that is the actual game,因为在“菜单”部分是不可能死的。而且,为了检查广告何时出现,我应该使用 GameManager,对吗?在游戏场景中也是如此。

public void GameOver()
    {
        gameOver = true;
        gameScore = score.GetComponent<Score>().getScore();
        score.SetActive(false);
        Invoke("ActivateGameOverCanvas", 1);
        pauseBtn.SetActive(false);
    }

^这是来自gamemanager的游戏结束部分,所以我想在这里我应该以某种方式介绍一条会生成插页式广告的指令,但我不知道。我也尝试将“广告”脚本添加到游戏管理器中,因此在游戏管理器执行 GameOver 函数结束时,从广告中调用 GameOverSoShowInterstitial,但它不起作用。


有什么想法吗? :(

【问题讨论】:

    标签: android unity3d ads banner interstitial


    【解决方案1】:

    1.使用来自另一个场景的对象

    在从一个场景到另一个场景中使用脚本/对象时,我们通常将该对象放入MonoBehaviour 内置方法DontDestroyOnLoad(object);。通过使用这种方法,我们可以确保object 在您更改场景时不会被破坏。假设 Menu 您的脚本在 Menu Scene 中,您可以将该对象放入 DontDestroyOnLoad() 方法中,并且它不会在整个游戏中被破坏。

    2。使用 Add 脚本中的方法

    为此,我们有多种选择。就像您可以使用 GameObject.FindObjectOfType()GameObject.FindGameObjectWithTag() 等找到对象一样。但是使用 Gameobject.FindXXX() 方法在性能方面可能会很重。 battre 方法是使用单例模式。在此您基本上在游戏开始时创建一个对象(当您需要该对象时)。并且不会有其他相同类型的对象(这意味着不会有其他对象附加相同的脚本)

    3.看两点在行动 您的广告脚本 您无需更改任何内容,只需添加单例模式即可。

    using System;
    using UnityEngine;
    using GoogleMobileAds.Api;
    
    public class ads : MonoBehaviour
    {
        //This is a static instance of this class so that you can access it from anywhere from you game, Without creating any creating a new Object.(Point #2)
        public static ads instance {
        get;         //These are the getter and setter methods of native C# find the reference below
        private set;
        }
    
        private Awake()
        {
            //Check if the instance is already assigned(if there is already an object with this same script attached)
            if(instance == null)
            {
               instance = this; //If instance is empty the assign this object to the instance
               DoNotDestroyOnLoad(this.gameObject); //This line will stop Unity from destroying the object when you load new scene.(Point #1)
            }
            else
            {
               Destroy(this.gameObject); //since there is already an object of with script destroy this object so that there will be no conflicts between multiple instances of the same class.
            }
        }
    
        void Start()
        {
            MobileAds.Initialize(initStatus => { });
            this.RequestBanner();
            this.RequestInterstitial();
        }
        public void ShowInterstitialAd()
        {
            ... Show your Ad
        }
    }
    

    游戏管理器脚本

    public void PlayerDie() //assuming that you have a method for when your Player dies
    {
        ...
        ads.instance.ShowInterstitialAd(); //Call the ShowInterstitialAd() method because the object will not be destroyed when you load another scene.
    }
    

    我基本上已经完成了。我希望这可以帮助你。以下是一些可能有助于进一步理解这一点的参考资料。

    Singleton pattern

    Getter and setter methods of native C#

    【讨论】:

    • 公共静态广告实例 { get { if (instance == null) { instance = GameObject.FindGameObjectWithTag("gameovertag"); } 返回实例; } 设置 { } }
    • 我按照你说的做了,这就是我发现 getter 应该写的方式(我用 gameover 画布将标签 gameovertag 添加到对象),但是 setter 呢?
    • 你不应该在 Get 或 set 方法中做,我还给你发了一个参考视频来了解 Singleton 模式。请观看视频,因为他将能够解释你击球手然后我。
    • 以及使用get的原因;并设置;方法。是我们可以将get; 方法设置为public,而set; 方法可以设置为privet,这样无论如何我们都无法从任何地方更改实例。
    • 非常感谢,您是大神。我已经设法显示横幅和插页式广告,但即使横幅不断显示,插页式测试广告也只会出现一次,当我第一次死的时候。我是否遗漏了什么或者这就是它应该工作的方式?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多