iOS平台添加Google Admob -2/2(Unity3D開發之八)

來源:互聯網
上載者:User

iOS平台添加Google Admob -2/2(Unity3D開發之八)

 

在上一篇文章中主要是編寫了iOS Admob的介面實現。那麼現在我們要做的事就是在unity中調用iOS Admob並展示。

一、實現Unity中對外介面,內部負責調用iOS Admob介面。
LHiOSAdmob.cs

using UnityEngine;using System.Collections;using System.Runtime.InteropServices;using System.Linq;using System.Collections.Generic;public enum GADAdSize:int{// iPhone and iPod Touch ad size. Typically 320x50.kGADAdSizeBanner = 1,// Taller version of kGADAdSizeBanner. Typically 320x100.kGADAdSizeLargeBanner,// Medium Rectangle size for the iPad (especially in a UISplitView's left pane). Typically 300x250.kGADAdSizeMediumRectangle,// Full Banner size for the iPad (especially in a UIPopoverController or in// UIModalPresentationFormSheet). Typically 468x60.kGADAdSizeFullBanner,// Leaderboard size for the iPad. Typically 728x90.kGADAdSizeLeaderboard,// Skyscraper size for the iPad. Mediation only. AdMob/Google does not offer this size. Typically// 120x600.kGADAdSizeSkyscraper,// An ad size that spans the full width of the application in portrait orientation. The height is// typically 50 pixels on an iPhone/iPod UI, and 90 pixels tall on an iPad UI.kGADAdSizeSmartBannerPortrait,// An ad size that spans the full width of the application in landscape orientation. The height is// typically 32 pixels on an iPhone/iPod UI, and 90 pixels tall on an iPad UI.kGADAdSizeSmartBannerLandscape}public class LHiOSAdmob : MonoBehaviour {[DllImport(__Internal)]private static extern void startRequestAdmob(string admobId, int adSizeId, float pixelX, float pixelY);[DllImport(__Internal)]private static extern void setAdmobHidden(bool isHidden);public static LHiOSAdmob Instance;private GADAdSize adSize;void Awake(){// singletonif (Instance != null){Debug.LogError(Multiple instances of LHiOSAdmob);}Instance = this;}// Init google admob// It will request the admob after five seconds.// It's will auto show admob.public void InitAdmob(string admobId, GADAdSize size, Vector2 pos){#if UNITY_IPHONEint adSizeId = (int)size;startRequestAdmob(admobId, adSizeId, pos.x, pos.y);#elseDebug.Log(Admob only run on iPhone platform);#endif}// Set Admob BannerView is visible or notpublic void SetAdmobVisible(bool isVisible){#if UNITY_IPHONEsetAdmobHidden(!isVisible);#elseDebug.Log(Admob only run on iPhone platform);#endif}// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}}

這是一個單例介面類,內部負責調用iOS Admob介面,並開放對外介面。供其他c#檔案調用。
注意:你可以製作一個Prefab,並包含該指令碼。將該Prefab拖放到情境中。在需要顯示admob的地方調用。

二、測試admob顯示,也就是在你需要的地方。

TestAdmob.cs

using UnityEngine;using System.Collections;public class TestAdmob : MonoBehaviour {// Use this for initializationvoid Start () {LHiOSAdmob.Instance.InitAdmob(your admob id, GADAdSize.kGADAdSizeSmartBannerLandscape, new Vector2(0, 150));}// Update is called once per framevoid Update () {}}

ok, 現在可以Build iOS工程了。

注意:

Build的iOS工程,要添加對應的編譯選項和iOS架構。

1、Other Linker Flags 添加 -ObjC

2、Xcode->Targets->Build Phases->Link Binary With Libraries 添加以下架構:
AdSupport
AudioToolbox
AVFoundation
CoreGraphics
MessageUI
StoreKit
SystemConfiguration


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.