移动平台广告解决方案--Admob

作者: 隐牧 | 来源:发表于2018-09-05 20:11 被阅读2213次

    本文知乎同步链接:https://zhuanlan.zhihu.com/p/43807044

    一、简介

    AdMob 是移动电话广告市场,可提供用户在移动电话网络上播放广告。2006年AdMob创建于加州。移动网站(Mobile site)可选择加入AdMob,并启用AdMob的广告轮播,广告则在AdMob网站中置放。目前iPhone与Android手机上皆有此功能。2009年11月9日Google宣布以7亿5千万股份购入AdMob,正式投入移动设备上的广告市场。

    Admob覆盖200多个国家,是美国最大的手机广告平台。

    icon

    二、广告类型

    总共分四种。

    分别为:Banner(横幅广告) 、Interstitial(插页广告)、Rewarded Video(激励广告)。

    Banner(横幅广告):最常见的,就是平时看到的屏幕的底部或者顶部有一个横条的广告条。

    Interstitial(插页广告):类似启动页广告,整个屏幕显示一副广告。

    Rewarded Video(激励广告):这个玩国外游戏经常会看到,你看完一个广告视频,游戏会奖励你一些道具或者其他的什么(这个奖励是一个整数,在AdMob中可以设置数量,用户看完视频会得到这个数量的奖励,比如奖励3瓶药水,3条生命等。)。

    广告单元

    三、 实施过程

    unity引擎在接入Admob广告时,过程非常简单:

    1、首先下载googleads-mobile-unity, 链接为:https://github.com/googleads/googleads-mobile-unity

    注意:点击上图红色部分releases跳转的位置可以选择自己需要的SDK版本。下载完插件,把插件里包含的GoogleMobileAds.unitypackage的文件导入你的项目。 导入项目后你的project里面会出现以下文件。

    2、申请广告id

    (1)前往AdMob官网注册账号,使用自己的Google账号即可(已注册的童鞋跳过此步骤);

            链接:https://www.google.com/admob/

    (2)注册完成后,登陆账号,即可进入AdMob管理中心,如下所示:

    在此界面,可点击左侧边栏中的选项进行信息设置,在此不在赘述。

    (3)账号申请完成后,即可为自己的应用申请相应的广告id了,选择左侧边栏中的 “应用” 选项,如下图所示:

    选择后,在右侧将会出现弹出窗体,点击窗体左下角的 “ 添加应用 ” 按钮:

    在弹出界面填写应用发布信息(已发布的应用,可在此直接关联Admob,未发布的应用可在之后进行关联):

    在此篇文章中,制作一个测试案例,本人选择未发布,选择完成后,跳转至如下界面,填写应用信息,并为应用选择相应的发布平台,在此以Android平台为例,选择Android,如下图所示:

    信息填写完成后,选择添加,即可成功创建自己的应用了,如下图所示:

    在此图中可获取自己的应用ID,在之后的广告接入中需要配置,可提前记录下来。接下来可谓自己的应用创建广告单元,选择左下角的创建广告单元按钮,即可进入如下界面:

    在此界面选择相应的广告模式即可进行创建,以横幅为例(其他两种创建方式相同),点击选择按钮,将进入如下界面,在该界面中填写广告单元名称:

    点击高级设置,可进行广告单元类型、刷新频率、有效每千次展示费用低价设置:

    设置完成后,点击左下角的创建广告单元按钮,即可完成广告创建。

    创建完成后,将会获得横幅广告单元id,在此界面底部,可选择 “完成”:终止广告单元创建,亦或选择“再创建一个广告单元”:继续进行广告单元创建。

    本次Test案例,创建两个广告单元:横幅与插页。

    3、代码逻辑,针对于Admob官方SDK,本人编写如下逻辑代码,进行广告接入:

    AdmobLaunch.cs: 用于配置APPID,进行广告单元初始化

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;using GoogleMobileAds.Api;

    public class AdmobLaunch : MonoBehaviour {

    void Awake()

    { #if UNITY_ANDROID

    string appId = "填写自己的AppId"; #elif UNITY_IPHONE

    string appId = "";

    #else

    string appId = "unexpected_platform";

    #endif

    MobileAds.Initialize(appId); DontDestroyOnLoad (this.gameObject);

    } // Use this for initialization

    void Start () { }

    // Update is called once per frame

    void Update () {

    }

    #region GUI Button

    /*

    void OnGUI()

    {

    if (GUI.Button(new Rect(0, 200, 200, 200), "Request _ Interstitial "))

    {

    GoogleMobileAdsDemoScript_Insert.Instance.RequestInterstitial();

    }

    if (GUI.Button(new Rect(300, 200, 200, 200), "Show Ad Interstitial"))

    {

    GoogleMobileAdsDemoScript_Insert.Instance.Show();

    }

    GUI.Box(new Rect(600, 200, 600, 200), "is ready Interstitial : " + GoogleMobileAdsDemoScript_Insert.Instance.IsReady());

    if (GUI.Button(new Rect(0, 600, 200, 200), "Request _ Banner "))

    {

    GoogleMobileAdsDemoScript_Banner.Instance.RequestBanner();

    }

    if (GUI.Button(new Rect(300, 600, 200, 200), "Show Ad Banner"))

    {

    GoogleMobileAdsDemoScript_Banner.Instance.Show();

    }

    if (GUI.Button(new Rect(600, 600, 200, 200), "destory Ad Banner"))

    {

    GoogleMobileAdsDemoScript_Banner.Instance.Destory();

    }

    if (GUI.Button(new Rect(0, 1000, 200, 200), "Request _ RewardedVideo "))

    {

    GoogleMobileAdsDemoScript_Video.Instance.RequestRewardedVideo();

    }

    if (GUI.Button(new Rect(300, 1000, 200, 200), "Show Ad RewardedVideo"))

    {

    GoogleMobileAdsDemoScript_Video.Instance.Show();

    }

    GUI.Box(new Rect(600, 1000, 600, 200), "is ready RewardedVideo : " + GoogleMobileAdsDemoScript_Video.Instance.IsReady());

    }

    */

    #endregion

    }

    GoogleMobileAdsDemoScript_Banner.cs: 横幅广告接入逻辑,可配置广告单元不同状态的回调事件。

    using UnityEngine;

    using System.Collections;

    using GoogleMobileAds.Api;

    using System;

    public class GoogleMobileAdsDemoScript_Banner : MonoBehaviour {

        public BannerView bannerView;

        public static GoogleMobileAdsDemoScript_Banner Instance;

        void Awake()    {        Instance = this;    }    public void Start()    {

    RequestBanner();

        }

          public void RequestBanner()    {

    #if UNITY_ANDROID

    string adUnitId = "填写自己的BannerId"; #elif UNITY_IPHONE

    string adUnitId = ""; #else

    string adUnitId = "unexpected_platform";

    #endif if (bannerView != null)

    bannerView.Destroy (); bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);

    // // Called when an ad request has successfully loaded.

    // bannerView.OnAdLoaded += HandleOnAdLoaded;

    // // Called when an ad request failed to load.

    // bannerView.OnAdFailedToLoad += HandleOnAdFailedToLoad;

    // // Called when an ad is clicked.

    // bannerView.OnAdOpening += HandleOnAdOpened;

    // // Called when the user returned from the app after an ad click.

    // bannerView.OnAdClosed += HandleOnAdClosed;

    // // Called when the ad click caused the user to leave the application.

    // //bannerView.OnAdLeavingApplication += HandleOnAdLeavingApplication;

    AdRequest request = new AdRequest.Builder().Build();

            // Load the banner with the request.        bannerView.LoadAd(request);

        }    public void Show()    {

            bannerView.Show();    }

        public void Destory()    {

            bannerView.Destroy();

        }

        public void HandleOnAdLoaded(object sender, EventArgs args)    {

            MonoBehaviour.print("HandleAdLoaded event received");    }    public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)    {

            MonoBehaviour.print("HandleFailedToReceiveAd event received with message: "

                                + args.Message);

        }

        public void HandleOnAdOpened(object sender, EventArgs args)    {

            MonoBehaviour.print("HandleAdOpened event received");

        }

        public void HandleOnAdClosed(object sender, EventArgs args)    {

            MonoBehaviour.print("HandleAdClosed event received");

        }

        public void HandleOnAdLeftApplication(object sender, EventArgs args)

        {

            MonoBehaviour.print("HandleAdLeftApplication event received");

        }

    }

    GoogleMobileAdsDemoScript_Insert.cs: 插页广告接入逻辑,可配置广告单元不同状态的回调事件。

    using UnityEngine;

    using System.Collections;

    using GoogleMobileAds.Api;

    using System;

    using UnityEngine.SceneManagement;

    public class GoogleMobileAdsDemoScript_Insert : MonoBehaviour {

        private InterstitialAd interstitial;

        public static GoogleMobileAdsDemoScript_Insert Instance;

        void Awake()    {        Instance = this;

        }

        public void Start()    { RequestInterstitial();

        }

        public void RequestInterstitial()    {

    #if UNITY_ANDROID

    string adUnitId = "填写自己的InterstitialId"; #elif UNITY_IPHONE

    string adUnitId = ""; #else

    string adUnitId = "unexpected_platform";

    #endif

    if (interstitial != null)

    interstitial.Destroy (); // Initialize an InterstitialAd.

    interstitial = new InterstitialAd(adUnitId);

    // // Called when an ad request has successfully loaded.

    // interstitial.OnAdLoaded += HandleOnAdLoaded;

    // // Called when an ad request failed to load.

    // interstitial.OnAdFailedToLoad += HandleOnAdFailedToLoad;

    // // Called when an ad is shown.

    // interstitial.OnAdOpening += HandleOnAdOpened;

    // // Called when the ad is closed.

    interstitial.OnAdClosed += HandleOnAdClosed;

    // // Called when the ad click caused the user to leave the application.

    // //interstitial.OnAdLeavingApplication += HandleOnAdLeavingApplication;

    // Create an empty ad request.

    AdRequest request = new AdRequest.Builder().Build();        // Load the interstitial with the request.

            interstitial.LoadAd(request);

        }    public bool IsReady()

        {

            return interstitial.IsLoaded();

        }

        public void Show()

        {

    //        if (IsReady())

    //        {

                interstitial.Show();//        }

    //        else {//            RequestInterstitial();

    //        }

        }

        public void HandleOnAdLoaded(object sender, EventArgs args)

        {

            MonoBehaviour.print("HandleAdLoaded event received");

        }

        public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)

        {

            MonoBehaviour.print("HandleFailedToReceiveAd event received with message: "

                                + args.Message);

    SceneManager.LoadScene( "LaunchScene" );

    ZTBubble.GameUIForm.isclose = true;

        }    public void HandleOnAdOpened(object sender, EventArgs args)

        {

            MonoBehaviour.print("HandleAdOpened event received");

        }    public void HandleOnAdClosed(object sender, EventArgs args)    {

            MonoBehaviour.print("HandleAdClosed event received");

    SceneManager.LoadScene( "LaunchScene" ); ZTBubble.GameUIForm.isclose = true;

        }

        public void HandleOnAdLeftApplication(object sender, EventArgs args)

        {

            MonoBehaviour.print("HandleAdLeftApplication event received");

        }

    }

    GoogleMobileAdsDemoScript_Video.cs: 激励广告接入逻辑,可配置广告单元不同状态的回调事件。

    using UnityEngine;using System.Collections;using GoogleMobileAds.Api;using System;public class GoogleMobileAdsDemoScript_Video : MonoBehaviour {

        private RewardBasedVideoAd rewardBasedVideo;

        // Create an empty ad request.

        AdRequest request = new AdRequest.Builder().Build();

        public static GoogleMobileAdsDemoScript_Video Instance;

        void Awake()

        {        Instance = this;

        }    public void Start()    {        Init();

        }

        void Init()

        {

            // Get singleton reward based video ad reference.

            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;        this.RequestRewardedVideo();    }    public void RequestRewardedVideo()

        {

    #if UNITY_ANDROID

            string adUnitId = "填写自己的RewardedVideoId"; #elif UNITY_IPHONE

                string adUnitId = ""; #else

                string adUnitId = "unexpected_platform"; #endif

            // Load the rewarded video ad with the request.        this.rewardBasedVideo.LoadAd(request, adUnitId);    }    public bool IsReady()

        {

            return rewardBasedVideo.IsLoaded();

        }

        public void Show()

        {        if (IsReady())

            {

                rewardBasedVideo.Show();

            }

            else {

                RequestRewardedVideo();

            }    }    public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)    {        MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");

        }

        public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)

        {

            MonoBehaviour.print(

                "HandleRewardBasedVideoFailedToLoad event received with message: "

                                + args.Message);

        }    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)    {        MonoBehaviour.print("HandleRewardBasedVideoClosed event received");    }    public void HandleRewardBasedVideoRewarded(object sender, Reward args)

        {

            string type = args.Type;

            double amount = args.Amount;

            MonoBehaviour.print(

                "HandleRewardBasedVideoRewarded event received for "

                            + amount.ToString() + " " + type);

        }

        public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)

        {

            MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");

        }

    }

    至此,Admob广告单元接入工作完成,创建一个空物体,将三个脚本进行挂载,运行程序,即可获得相应广告单元初始化输出信息。

    4、发布应用,进行真机测试

    在Unity编辑器下,我们无法进行广告单元测试,在此,可以将应用进行发布,安装到相应平台的移动设备或者模拟器进行测试(建议使用真机测试)。本次程序运行效果如下所示:

    至此,所有流程已结束。

    相关文章

      网友评论

        本文标题:移动平台广告解决方案--Admob

        本文链接:https://www.haomeiwen.com/subject/ygjzwftx.html