本文知乎同步鏈接:https://zhuanlan.zhihu.com/p/43807044
一沥割、簡(jiǎn)介
AdMob 是移動(dòng)電話廣告市場(chǎng)饶辙,可提供用戶在移動(dòng)電話網(wǎng)絡(luò)上播放廣告。2006年AdMob創(chuàng)建于加州弟断。移動(dòng)網(wǎng)站(Mobile site)可選擇加入AdMob樊卓,并啟用AdMob的廣告輪播,廣告則在AdMob網(wǎng)站中置放短蜕。目前iPhone與Android手機(jī)上皆有此功能氢架。2009年11月9日Google宣布以7億5千萬(wàn)股份購(gòu)入AdMob,正式投入移動(dòng)設(shè)備上的廣告市場(chǎng)朋魔。
Admob覆蓋200多個(gè)國(guó)家岖研,是美國(guó)最大的手機(jī)廣告平臺(tái)。
二、廣告類型
總共分四種孙援。
分別為:Banner(橫幅廣告) 害淤、Interstitial(插頁(yè)廣告)、Rewarded Video(激勵(lì)廣告)拓售。
Banner(橫幅廣告):最常見(jiàn)的窥摄,就是平時(shí)看到的屏幕的底部或者頂部有一個(gè)橫條的廣告條。
Interstitial(插頁(yè)廣告):類似啟動(dòng)頁(yè)廣告础淤,整個(gè)屏幕顯示一副廣告崭放。
Rewarded Video(激勵(lì)廣告):這個(gè)玩國(guó)外游戲經(jīng)常會(huì)看到,你看完一個(gè)廣告視頻鸽凶,游戲會(huì)獎(jiǎng)勵(lì)你一些道具或者其他的什么(這個(gè)獎(jiǎng)勵(lì)是一個(gè)整數(shù)币砂,在AdMob中可以設(shè)置數(shù)量,用戶看完視頻會(huì)得到這個(gè)數(shù)量的獎(jiǎng)勵(lì)玻侥,比如獎(jiǎng)勵(lì)3瓶藥水决摧,3條生命等。)凑兰。
三掌桩、 實(shí)施過(guò)程
unity引擎在接入Admob廣告時(shí),過(guò)程非常簡(jiǎn)單:
1姑食、首先下載googleads-mobile-unity拘鞋, 鏈接為:https://github.com/googleads/googleads-mobile-unity
注意:點(diǎn)擊上圖紅色部分releases跳轉(zhuǎn)的位置可以選擇自己需要的SDK版本。下載完插件矢门,把插件里包含的GoogleMobileAds.unitypackage的文件導(dǎo)入你的項(xiàng)目。 導(dǎo)入項(xiàng)目后你的project里面會(huì)出現(xiàn)以下文件灰蛙。
2祟剔、申請(qǐng)廣告id
(1)前往AdMob官網(wǎng)注冊(cè)賬號(hào),使用自己的Google賬號(hào)即可(已注冊(cè)的童鞋跳過(guò)此步驟)摩梧;
? ? ? ? 鏈接:https://www.google.com/admob/
(2)注冊(cè)完成后物延,登陸賬號(hào),即可進(jìn)入AdMob管理中心仅父,如下所示:
在此界面叛薯,可點(diǎn)擊左側(cè)邊欄中的選項(xiàng)進(jìn)行信息設(shè)置,在此不在贅述笙纤。
(3)賬號(hào)申請(qǐng)完成后耗溜,即可為自己的應(yīng)用申請(qǐng)相應(yīng)的廣告id了,選擇左側(cè)邊欄中的 “應(yīng)用” 選項(xiàng)省容,如下圖所示:
選擇后抖拴,在右側(cè)將會(huì)出現(xiàn)彈出窗體,點(diǎn)擊窗體左下角的 “ 添加應(yīng)用 ” 按鈕:
在彈出界面填寫應(yīng)用發(fā)布信息(已發(fā)布的應(yīng)用,可在此直接關(guān)聯(lián)Admob阿宅,未發(fā)布的應(yīng)用可在之后進(jìn)行關(guān)聯(lián)):
在此篇文章中候衍,制作一個(gè)測(cè)試案例,本人選擇未發(fā)布洒放,選擇完成后蛉鹿,跳轉(zhuǎn)至如下界面,填寫應(yīng)用信息往湿,并為應(yīng)用選擇相應(yīng)的發(fā)布平臺(tái)菲饼,在此以Android平臺(tái)為例,選擇Android埠巨,如下圖所示:
信息填寫完成后双妨,選擇添加,即可成功創(chuàng)建自己的應(yīng)用了蔓腐,如下圖所示:
在此圖中可獲取自己的應(yīng)用ID矩乐,在之后的廣告接入中需要配置,可提前記錄下來(lái)回论。接下來(lái)可謂自己的應(yīng)用創(chuàng)建廣告單元散罕,選擇左下角的創(chuàng)建廣告單元按鈕,即可進(jìn)入如下界面:
在此界面選擇相應(yīng)的廣告模式即可進(jìn)行創(chuàng)建傀蓉,以橫幅為例(其他兩種創(chuàng)建方式相同)欧漱,點(diǎn)擊選擇按鈕,將進(jìn)入如下界面葬燎,在該界面中填寫廣告單元名稱:
點(diǎn)擊高級(jí)設(shè)置误甚,可進(jìn)行廣告單元類型、刷新頻率谱净、有效每千次展示費(fèi)用低價(jià)設(shè)置:
設(shè)置完成后窑邦,點(diǎn)擊左下角的創(chuàng)建廣告單元按鈕,即可完成廣告創(chuàng)建壕探。
創(chuàng)建完成后冈钦,將會(huì)獲得橫幅廣告單元id,在此界面底部李请,可選擇 “完成”:終止廣告單元?jiǎng)?chuàng)建瞧筛,亦或選擇“再創(chuàng)建一個(gè)廣告單元”:繼續(xù)進(jìn)行廣告單元?jiǎng)?chuàng)建。
本次Test案例导盅,創(chuàng)建兩個(gè)廣告單元:橫幅與插頁(yè)较幌。
3、代碼邏輯白翻,針對(duì)于Admob官方SDK绅络,本人編寫如下邏輯代碼,進(jìn)行廣告接入:
AdmobLaunch.cs: 用于配置APPID,進(jìn)行廣告單元初始化
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: 橫幅廣告接入邏輯恩急,可配置廣告單元不同狀態(tài)的回調(diào)事件杉畜。
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: 插頁(yè)廣告接入邏輯,可配置廣告單元不同狀態(tài)的回調(diào)事件衷恭。
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: 激勵(lì)廣告接入邏輯此叠,可配置廣告單元不同狀態(tài)的回調(diào)事件。
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廣告單元接入工作完成灭袁,創(chuàng)建一個(gè)空物體,將三個(gè)腳本進(jìn)行掛載窗看,運(yùn)行程序茸歧,即可獲得相應(yīng)廣告單元初始化輸出信息。
4显沈、發(fā)布應(yīng)用软瞎,進(jìn)行真機(jī)測(cè)試
在Unity編輯器下,我們無(wú)法進(jìn)行廣告單元測(cè)試拉讯,在此涤浇,可以將應(yīng)用進(jìn)行發(fā)布,安裝到相應(yīng)平臺(tái)的移動(dòng)設(shè)備或者模擬器進(jìn)行測(cè)試(建議使用真機(jī)測(cè)試)魔慷。本次程序運(yùn)行效果如下所示:
至此只锭,所有流程已結(jié)束。