2019年6月23日 星期日

Unity 學習筆記 - 放置Admob廣告


關於如何在unity 中加入Admob廣告,在官方網站其實有很詳細的說明,請參照Mobile Ads SDK (Unity)
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);
    }
}


沒有留言:

張貼留言

[Android][App] Pin Poke Challenge 隱私權保護政策

非常歡迎您使用「 Pin Poke Challenge 」,為了讓您能夠安心使用此 App 的各項服務與資訊,特此向您說明 Pin Poke Challenge的隱私權保護政策,以保障您的權益,請您詳閱下列內容: 一、隱私權保護政策的適用範圍 隱私權保護政策內容,包括...