【问题标题】:How to show admob interstitial and reward ads in Unity game?如何在 Unity 游戏中显示 admob 插页式和奖励广告?
【发布时间】:2017-07-17 09:38:42
【问题描述】:

谁能给我 c# 脚本在游戏结束或暂停时显示插页式广告。我已经尝试过官方文档中的代码,但它没有工作,所以如果有人知道怎么做,请给我脚本。

【问题讨论】:

    标签: unity3d mobile admob ads


    【解决方案1】:

    未经测试但应该可以工作:

    编辑:编辑的部分实际上来自谷歌的教程。 偶尔去google一下,你会学到很多东西的。

    using System;
    using UnityEngine;
    using GoogleMobileAds.Api;
    
    public class AdMobOnPause : MonoBehaviour
    {
        public string AndroidInterstitialID = "INSERT_ANDROID_INTERSTITIAL_AD_UNIT_ID_HERE";
    public string IOSInterstitialID = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
    
    
    private static AdMobOnPause instance;
    
    private InterstitialAd interstitial;
    // ADD THIS
    private RewardBasedVideoAd rewardBasedVideo;
    
    private AdRequest request;
    void Awake()
    {
        if (AdMobOnPause.instance != null)
        {
            Destroy(gameObject);    //just allow one adscontroller on scene over gameplay, even when you restart this level
        }
    }
    void Start()
    {
        DontDestroyOnLoad(gameObject);
        instance = this;
        RequestInterstitial();
        //Request Video!
        RequestRewardBasedVideo();
    }
    
    //ADD THESE TWO METHODS
    void OnEnable() {
    rewardBasedVideo.OnAdRewarded += HandleRewardedSuccessful;
    }
    
    void OnDisable() {
    rewardBasedVideo.OnAdRewarded -= HandleRewardedSuccessful;
    }
    
    private void RequestInterstitial()
    {
    #if UNITY_ANDROID
        string adUnitId = AndroidInterstitialID;
    #elif UNITY_IPHONE
        string adUnitId = IOSInterstitialID;
    #else
        string adUnitId = "unexpected_platform";
    #endif
    
        // Initialize an InterstitialAd.
        interstitial = new InterstitialAd(adUnitId);
        interstitial.OnAdClosed += HandleEventHandler;
        // Create an empty ad request.
        request = new AdRequest.Builder().Build();
        // Load the interstitial with the request.
        interstitial.LoadAd(request);
    }
    
    void HandleEventHandler(object sender, System.EventArgs e)
    {
        interstitial.Destroy();
        interstitial.LoadAd(request);
    }
    
    public void Pause()
    {
        //your pause code here or,
        //if(Time.timeScale < 1)
        //    Time.timeScale = 1;
        //else
        //    Time.timeScale = 0;
        if (interstitial.IsLoaded())
        {
            interstitial.Show();
        }
    }
    //From here on is the Rewarded video code.
    private void RequestRewardBasedVideo()
    {
    #if UNITY_ANDROID
        string adUnitId = "INSERT_AD_UNIT_HERE";
    #elif UNITY_IPHONE
        string adUnitId = "INSERT_AD_UNIT_HERE";
    #else
        string adUnitId = "unexpected_platform";
    #endif
    
        rewardBasedVideo = RewardBasedVideoAd.Instance;
    
        AdRequest request = new AdRequest.Builder().Build();
        rewardBasedVideo.LoadAd(request, adUnitId);
    }
    
    public void GameOver()
    {
        if (rewardBasedVideo.IsLoaded())
        {
            rewardBasedVideo.Show();
        }
    }
    
    public void HandleRewardedSuccessful() {
    // Do Something when rewarded video is successfully watched
    }
    

    }

    【讨论】:

    • 谢谢,它可以工作,但我还需要用于基于奖励的视频广告的 c# 脚本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 2022-06-21
    相关资源
    最近更新 更多