Google AdMob与UnityADs在游戏中进行广告接入

C#

浏览数:263

2019-8-30

框架视图

效果图




关键代码

GoogleAdManager

using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;//掺入广告命名空间

public class GoogleAdManager : MonoBehaviour
{

    public string adUnitId = "ca-app-pub-5711132426115648/1023732213";

    // Use this for initialization

    void Start()
    {
        //横幅广告
        //掺入广告
        BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);//参数:广告id,广告尺寸(横幅),位置;
        //构建广告
        AdRequest request = new AdRequest.Builder().Build();
        //加载广告
        bannerView.LoadAd(request);
        //bannerView.Show ();
    }
}

UnityAd

using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;

public class UnityAd : MonoBehaviour
{

    void Start()
    {
        Advertisement.Initialize("1321938", true);
    }
    public void ShowAd()
    {
        print(Advertisement.IsReady());
        if (Advertisement.IsReady())
        {
            Advertisement.Show();
        }
    }

    void Update()
    {
        if (Advertisement.IsReady() && !Advertisement.isShowing)
        {
            ShowAd();
        }
     
    }

    public void ShowRewardedAd()
    {
        if (Advertisement.IsReady("rewardedVideo"))
        {
            var options = new ShowOptions { resultCallback = HandleShowResult };
            Advertisement.Show("rewardedVideo", options);
        }
    }

    private void HandleShowResult(ShowResult result)
    {
        switch (result)
        {
            case ShowResult.Finished:
                Debug.Log("The ad was successfully shown.");
                //  
                // YOUR CODE TO REWARD THE GAMER  
                // Give coins etc.  
                break;
            case ShowResult.Skipped:
                Debug.Log("The ad was skipped before reaching the end.");
                break;
            case ShowResult.Failed:
                Debug.LogError("The ad failed to be shown.");
                break;
        }
    }  
}

作者:TonyWan_AR