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

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

上一篇文章中主要是编写了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. yum:在Red Hat和Fedora中使用
  2. TreeView使用
  3. Nodejs编码转化问题
  4. 读书笔记_Effective_C++_条款四十五:运用成员函数模板接受所有兼容类型
  5. 华为S9306简单实用配置合集
  6. [转]powerDesigner生成excel版本的数据库文件
  7. Double与BigDecimal 比较
  8. 快速解决js开发下拉框中blur与click冲突
  9. view-xpath
  10. windows 中的类似于sudo的命令(在cmd中以另一个用户的身份运行命令)
  11. Akka-Cluster(0)- 分布式应用开发的一些想法
  12. 【题解】BZOJ 1901: Zju2112 Dynamic Rankings
  13. 手工利用Chrome浏览器“Javascript控制台”
  14. Java菜鸟学习笔记(23)--继承篇(二):继承与组合
  15. 1003 我要通过!| PAT (Basic Level) Practice
  16. C#高级编程9-第9章 字符串和正则表达式
  17. (使用lua++)Lua脚本和C++交互(三)
  18. 【ZZ】国外大型网站使用到编程语言 | 菜鸟教程
  19. 一款基于jQuery和HTML5全屏焦点图
  20. 持续集成:TeamCity 的安装和使用

热门文章

  1. css控制file控件透明 漂浮
  2. Hibernate的条件查询的几种方式
  3. C语言 递归 汉诺塔问题 最大公约数问题
  4. ios中的safari转换时间戳问题
  5. Python小代码_13_生成两个参数的最小公倍数和最大公因数
  6. 2018年4月更新70多个公司dnc招聘职位
  7. python:浅析python 中__name__ = '__main__' 的作用(转载)
  8. Docker环境 ELK 快速部署
  9. Java9相关资料(JShell简易教程等)
  10. SQL Server AlwaysON从入门到进阶(2)——存储