iOS平台添加Google Admob -2/2(Unity3D开发之八)
来源:互联网 发布:畅唐网络待遇 编辑:程序博客网 时间:2024/06/07 04:45
猴子原创,欢迎转载。转载请注明: 转载自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(){// 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
1 0
- iOS平台添加Google Admob -2/2(Unity3D开发之八)
- iOS平台添加Google Admob -1/2(Unity3D开发之七)
- Unity3d iOS 如何添加Admob
- ios添加admob 错误NSDecimalNumber gad_negativeOne - 2
- iOS开发用swift添加admob广告
- android开发步步为营之55:google广告平台admob接入总结
- Android开发步步为营之55:google广告平台admob接入总结
- 如何添加AdMob广告到现有的Unity3D ios游戏
- 如何添加AdMob广告到现有的Unity3D ios游戏
- 如何添加AdMob广告到现有的Unity3D ios游戏
- 2DSprite添加Light照射(Unity3D开发之十六)
- Unity3D Android游戏添加google广告的方法——使用AdMob
- 基于Unity3D的Android游戏添加google广告的方法——使用AdMob
- 基于Unity3D的Android游戏添加google广告的方法——使用AdMob
- iOS开发用swift添加admob广告,iosswiftadmob广告
- Google AdMob Ads iOS Fundamentals
- Google AdMob Ads iOS Fundamentals
- Remove Google Play Games libraries on iOS (Unity3D开发之二十一)
- 理解 Linux 条件变量
- UILabel自适应高度和自动换行
- MapReduce体系结构及各种算法(1)
- 算法-Java中如何把两个数组合并为一个
- C++ Constructors FAQ
- iOS平台添加Google Admob -2/2(Unity3D开发之八)
- UVA - 152 Tree's a Crowd
- 【笔试面试——24】链表04——反转链表
- IOS中Json解析的四种方法
- sql回滚语句
- 薄膜电容器的特性
- 什么是响应式Web设计?怎样进行?
- H264关键帧和丢帧策略研究
- PHP大小写:函数名和类名不区分,变量名区分