码迷,mamicode.com
首页 > 移动开发 > 详细

iOS平台添加Google Admob -2/2(Unity3D开发之八)

时间:2014-08-07 15:54:10      阅读:418      评论:0      收藏:0      [点我收藏+]

标签:unity   ios   google   admob   

猴子原创,欢迎转载。转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢!

原文地址: http://www.cocos2dev.com/?p=572

bubuko.com,布布扣

上一篇文章中主要是编写了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


iOS平台添加Google Admob -2/2(Unity3D开发之八),布布扣,bubuko.com

iOS平台添加Google Admob -2/2(Unity3D开发之八)

标签:unity   ios   google   admob   

原文地址:http://blog.csdn.net/cocos2der/article/details/38417709

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!