2023年10月25日 星期三

【筆記】- Unity 如何加入 AdMob Ads 橫幅廣告 - For iOS - Xcode

 參考:https://developers.google.com/admob/unity/quick-start?hl=zh-tw#ios


實作環境:macOS 13.6、Xcode 15.0.1、Unity Editor 2023.3.30f1、Cocoa 1.13.0.


必要條件


(1) 安裝 CocoaPods!(Xcode 請記得先安裝)

curl -L https://get.rvm.io | bash -s stable

sudo gem install cocoapods


參考:https://developers.google.com/admob/unity/quick-start?hl=zh-tw

參考:https://developers.google.com/admob/unity/banner?hl=zh-tw#ios

參考:https://developers.google.com/admob/ios/quick-start?hl=zh-tw#manually_using_the_sdk_download

參考:https://developers.google.com/admob/ios/download?hl=zh-tw

建議:https://mac.install.guide/ruby/13.html

建議:https://www.ruby-lang.org/en/downloads/

其他:https://developers.google.com/admob/unity/mediation/unity?hl=zh-tw


(2) 如同 Unity Android 版本加入 Admob Ads 的作法,然後切換到 iOS Build 環境.



(3) 設定 iOS - Player Settings 相關參數



(4) 點選「Build」輸出 Xcode專案





(5) 檢查 Xcoed 專案目錄下有沒生成 CocoaPods 所需相關檔案?尤其是名為 XXX.xcworkspace



(6) Xcode 開啟 XXX.xcworkspace 檔,而非 Xcode 內定的 XXX.xcodeproj



(7) 點選 Build 就行囉!


*若是沒看到 XXX.xcodeproj 檔...


(1) 試著在 Podfile 所在目錄下 pod install 命令(第一次執行會很久...)

會出現如下訊息,若沒成功就得檢視訊息回應是啥了(我遇到的是 pod 版本太低「1.9.1」, 幾經折騰,後來更新為 1.13.0 才過關!):





2023年10月18日 星期三

【筆記】- Unity 如何加入 AdMob Ads 橫幅廣告 - For Android

參考: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) 結束,收工!^_^