參考:https://developers.google.com/admob/unity/banner?hl=zh-tw
話不多說,實作一番:
(1) 下載 GoogleMobileAds.unitypackage:到這個網頁 https://github.com/googleads/googleads-mobile-unity 裡的右下角 Release 連結裡,點選「Google Mobile Ads Unity Plugin v8.5.2」(本例是 v8.5.2 版)https://github.com/googleads/googleads-mobile-unity/releases/tag/v8.5.2
(2) 在 Google Mobile Ads Unity Plugin v8.5.2 網頁最下方的 Assets 裡,可找到 GoogleMobileAds-v8.5.2.unitypackage 檔案,直接點選就可以下載了.
(3) 在 Unity 裡載入 GoogleMobileAds.unitypackage,在 Unity 頂部選單裡點選「Assets」「Import Package」「Custom Package…」,並 Open 選擇的 GoogleMobileAds.unitypackage 檔案.
(4) 載入 GoogleMobileAds.unitypackage 檔案時,會出現一堆有的沒的選項,就給他「Import」「Yes」「Enable」下去就是了...
(5) 設定應用程式 ID,在 Unity 頂部選單裡,點選「Assets」「Google Mobile Ads」「Settings…」,並填入來自 AdMob Ads 的應用程式 ID.
(6) 先測試 Admob Ads,記得在 Build Settings 中先勾選「Development Build」,以後正式發布再取消勾選.
(7) 在 Unity Project 中建立一個空的 GameObject,命名為 BannerViewer.及新建一個 C# script,命名為 GoogleMobileAdsDemoScript.cs,(cs 程式內容於文末) 並將該 cs 檔拉入 BannerViewer 裡.
(8) 點選執行,測試成功!
(9) GoogleMobileAdsDemoScript.cs 程式內容參考:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
LoadAd();
});
}
// ------------------------------------------------------------------------
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
private string _adUnitId = "ca-app-pub-3940256099942544/6300978111";
#elif UNITY_IPHONE
private string _adUnitId = "ca-app-pub-3940256099942544/2934735716";
#else
private string _adUnitId = "unused";
#endif
BannerView _bannerView;
// <summary>
// Creates a 320x50 banner view at top of the screen.
// </summary>
public void CreateBannerView()
{
Debug.Log("Creating banner view");
// If we already have a banner, destroy the old one.
if (_bannerView != null)
{
//DestroyAd();
DestroyBannerView();
}
// Create a 320x50 banner at top of the screen
_bannerView = new BannerView(_adUnitId, AdSize.Banner, AdPosition.Top);
}
// <summary>
// Creates the banner view and loads a banner ad.
// </summary>
public void LoadAd()
{
// create an instance of a banner view first.
if(_bannerView == null)
{
CreateBannerView();
}
// create our request used to load the ad.
var adRequest = new AdRequest();
// send the request to load the ad.
Debug.Log("Loading banner ad.");
_bannerView.LoadAd(adRequest);
}
// <summary>
// listen to events the banner view may raise.
// </summary>
private void ListenToAdEvents()
{
// Raised when an ad is loaded into the banner view.
_bannerView.OnBannerAdLoaded += () =>
{
Debug.Log("Banner view loaded an ad with response : "
+ _bannerView.GetResponseInfo());
};
// Raised when an ad fails to load into the banner view.
_bannerView.OnBannerAdLoadFailed += (LoadAdError error) =>
{
Debug.LogError("Banner view failed to load an ad with error : "
+ error);
};
// Raised when the ad is estimated to have earned money.
_bannerView.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(string.Format("Banner view paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
// Raised when an impression is recorded for an ad.
_bannerView.OnAdImpressionRecorded += () =>
{
Debug.Log("Banner view recorded an impression.");
};
// Raised when a click is recorded for an ad.
_bannerView.OnAdClicked += () =>
{
Debug.Log("Banner view was clicked.");
};
// Raised when an ad opened full screen content.
_bannerView.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Banner view full screen content opened.");
};
// Raised when the ad closed full screen content.
_bannerView.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Banner view full screen content closed.");
};
}
// <summary>
// Destroys the banner view.
// </summary>
public void DestroyBannerView()
{
if (_bannerView != null)
{
Debug.Log("Destroying banner view.");
_bannerView.Destroy();
_bannerView = null;
}
}
}
(Z) 結束,收工!^_^