1. 下載插件
2. import GoogleMobileAdsPlugin.unitypackage
(即下載下來的插件)
3. 建立一腳本 ShowGoogleAds.cs 掛在一物件上,即可成功顯示廣告
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
public class ShowGoogleAds : MonoBehaviour {
private BannerView bannerView;
// Use this for
initialization
void Start () {
// google ad
#if UNITY_ANDROID
string appId = "xx-xxx-xxx-xxxxxxxxxxxxxxxx~xxxxxxxxxx";
#elif UNITY_IPHONE
string appId = " xx-xxx-xxx-xxxxxxxxxxxxxxxx~xxxxxxxxxx ";
#else
string appId =
"unexpected_platform";
#endif
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(appId);
this.RequestBanner();
}
// Update is called once per frame
void Update () {
}
private void RequestBanner()
{
// 這是測試id
//ca-app-pub-3940256099942544/6300978111
#if UNITY_ANDROID
string adUnitId = "xx-xxx-xxx-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = " xx-xxx-xxx-xxxxxxxxxxxxxxxx/xxxxxxxxxx ";
#else
string adUnitId =
"unexpected_platform";
#endif
// Create a banner at the top of the
screen.
bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
// Create an empty ad request.
AdRequest request = new
AdRequest.Builder().Build();
// Load the banner with the request.
bannerView.LoadAd(request);
}
}