【问题标题】:Where to put Request Interstitial Ad function in code?代码中的请求插页式广告功能放在哪里?
【发布时间】:2019-07-03 23:43:37
【问题描述】:

当我想向他们展示您认为放置此功能的正确位置时,继续投放广告;是 Start() 吗?一旦游戏对象生成,就会调用 Start ,所以会有广告怎么办??

public void RequestInterstitialAd()
{
    interstitalAd = new InterstitialAd(interstitalAdID);
    AdRequest adRequest = new AdRequest.Builder().Build();
    interstitalAd.LoadAd(adRequest);

    interstitalAd.OnAdLoaded += HandleOnAdLoaded;
    interstitalAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
    interstitalAd.OnAdOpening += HandleOnAdOpened;
    interstitalAd.OnAdClosed += HandleOnAdClosed;
    interstitalAd.OnAdLeavingApplication += HandleOnAdLeavingApplication;
}

【问题讨论】:

    标签: unity3d admob


    【解决方案1】:

    是的,在静态 GameObject 的 'Start' 方法中执行。因为它需要初始化一次,而不是多次。

    【讨论】:

    • 它会继续加载广告,即使它被调用一次?
    • 是的,只有一次。它应该是。
    • 感谢您的回答,但我发现每次您需要展示广告时都需要调用它
    • 您应该调用一次广告初始化,但显示或其他任何东西都应该调用正确的代码位置。
    • 我只在开始时执行此操作,并且每 3 次加载场景时调用 DisplayInterstitialAd() 函数,但没有像每次需要显示广告时都需要 loadAd() 函数那样工作
    【解决方案2】:

    每次我需要展示广告时都需要调用它,所以我这样做了:

    public class AdMobManager : MonoBehaviour
    {
        public static AdMobManager instance;//to have the right to call this class functions and variables without the need to put static to each one from another class 
        [SerializeField]
        private string appID,interstitalAdID;
        private InterstitialAd interstitalAd;
        public bool isAdOpened,isAdClosed;
        // Start is called before the first frame update
        void Start()
        {
            instance = this;
            MobileAds.Initialize(appID);
            RequestInterstitialAd();//load an ad from the first time game is opened
        }
    
        // Update is called once per frame
        void Update()
        {
        }
    
        public void RequestInterstitialAd()
        {
            if(interstitalAd != null)//remove the old interstital ad showed before to create a new one 
            {
                interstitalAd.Destroy();
            }
            interstitalAd = new InterstitialAd(interstitalAdID);
            AdRequest adRequest = new AdRequest.Builder().Build();
            interstitalAd.LoadAd(adRequest);
            interstitalAd.OnAdLoaded += HandleOnAdLoaded;
            interstitalAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
            interstitalAd.OnAdOpening += HandleOnAdOpened;
            interstitalAd.OnAdClosed += HandleOnAdClosed;
            interstitalAd.OnAdLeavingApplication += HandleOnAdLeavingApplication;
            Debug.Log("Ads Request Created");//used to test
        }
    
        public void DisplayInterstitialAd()
        {
            if (interstitalAd.IsLoaded())
            {
                interstitalAd.Show();
            }
        }
    
        public void HandleOnAdLoaded(object sender, EventArgs args)
        {
            MonoBehaviour.print("HandleAdLoaded event received");
        }
        public void HandleOnAdFailedToLoad(object sender,AdFailedToLoadEventArgs args)
        {
            MonoBehaviour.print("HandleFailedToReciveAd event received with message: " + args.Message);
        }
        public void HandleOnAdOpened(object sender, EventArgs args)
        {
            MonoBehaviour.print("HandleAdOpened event received");
            isAdOpened = true;
        }
        public void HandleOnAdClosed(object sender,EventArgs args)
        {
            MonoBehaviour.print("HandleAdClosed event received");
            isAdClosed = true;
            isAdOpened = false;//initialise
            RequestInterstitialAd();//everytime we show an ad load another one when the first one is closed
        }
        public void HandleOnAdLeavingApplication(object sender,EventArgs args)
        {
            MonoBehaviour.print("HandleAdLeavingApplication event recived");
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-28
      • 2020-11-17
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      相关资源
      最近更新 更多