你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS平台添加Google Admob -2/2(Unity3D開發之八)

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

編輯:IOS開發綜合

 

/

在上一篇文章中主要是編寫了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()
	{
		// singleton
		if (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_IPHONE
		int adSizeId = (int)size;
		startRequestAdmob(admobId, adSizeId, pos.x, pos.y);
		#else
		Debug.Log(Admob only run on iPhone platform);
		#endif
	}

	// Set Admob BannerView is visible or not
	public void SetAdmobVisible(bool isVisible)
	{
		#if UNITY_IPHONE
		setAdmobHidden(!isVisible);
		#else
		Debug.Log(Admob only run on iPhone platform);
		#endif
	}

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

這是一個單例接口類,內部負責調用iOS Admob接口,並開放對外接口。供其他c#文件調用。
注意:你可以制作一個Prefab,並包含該腳本。將該Prefab拖放到場景中。在需要顯示admob的地方調用。

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

TestAdmob.cs

using UnityEngine;
using System.Collections;

public class TestAdmob : MonoBehaviour {
	// Use this for initialization
	void Start () {
		LHiOSAdmob.Instance.InitAdmob(your admob id, GADAdSize.kGADAdSizeSmartBannerLandscape, new Vector2(0, 150));
	}
	
	// Update is called once per frame
	void 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


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved