Here's a demo project I wrote in Unity4.3. In this demo, user can choose either use ADBannerView(Unity built-in class) or iADBannerView (the class I wrote that calls native ios code) to Create, Destroy, Show and Hide iAd in Unity scene.
Demo Scene
The project demonstrates how to build a simple iAd plugin for iOS.
- In DemoScene, user can select:
1. service type (Unity iAd or Native iAd)
2. banner type (banner or MediumRect)
3. layout type (top, center or bottom)
on GUI and then create the iAd. (figure 1)
Once iAd is created, user can Show/Hide iAd by clicking the GUI button.(figure 2)
- In DemoScene, user can select:
1. service type (Unity iAd or Native iAd)
2. banner type (banner or MediumRect)
3. layout type (top, center or bottom)
on GUI and then create the iAd. (figure 1)
Once iAd is created, user can Show/Hide iAd by clicking the GUI button.(figure 2)
Source
- iADBannerView.cs is the class that calls the native objective-c
- using UnityEngine;
- using System.Collections;
- using System.Runtime.InteropServices;
- public class iADBannerView {
- #if UNITY_IPHONE
- /* Interface to native implementation */
- [DllImport ("__Internal")]
- private static extern void _StartAdBanner (int iType, int iLayout);
- [DllImport ("__Internal")]
- private static extern string _StopAdBanner ();
- [DllImport ("__Internal")]
- private static extern string _ShowAdBanner ();
- [DllImport ("__Internal")]
- private static extern string _HideAdBanner ();
- #endif
- public delegate void AdBannerHandler();
- public static event AdBannerHandler onBannerWasClicked, onBannerWasLoaded;
- public static void TriggerOnClicked() { if(onBannerWasClicked!=null) onBannerWasClicked();}
- public static void TriggerOnLoaded() { if(onBannerWasLoaded!=null) onBannerWasLoaded();}
- public enum Type{
- Banner,
- MediumRect
- }
- public enum Layout{
- Top,
- Center,
- Bottom,
- }
- public bool loaded{get; private set;}
- public bool visible{
- get{return _visible;}
- set{
- if(value) ShowAdBanner();
- else HideAdBanner();
- }
- }
- private bool _visible;
- private Type type;
- private Layout layout;
- public iADBannerView(Type t, Layout l){
- type = t;
- layout = l;
- visible = false;
- loaded = false;
- onBannerWasLoaded += onLoaded;
- StartAdBanner((int)type,(int)layout);
- }
- ~iADBannerView(){
- StopAdBanner();
- }
- private void onLoaded(){
- loaded = true;
- }
- #region Unity to Objective-C
- public void StartAdBanner(int iType, int iLayout)
- {
- #if UNITY_IPHONE
- if (Application.platform != RuntimePlatform.OSXEditor){
- _StartAdBanner(iType,iLayout);
- Debug.Log("BannerView:StartAdBanner");
- }
- #endif
- }
- public void StopAdBanner()
- {
- #if UNITY_IPHONE
- if (Application.platform != RuntimePlatform.OSXEditor){
- _StopAdBanner();
- Debug.Log("BannerView:StopAdBanner");
- }
- #endif
- }
- public void ShowAdBanner()
- {
- #if UNITY_IPHONE
- if (Application.platform != RuntimePlatform.OSXEditor){
- if(!_visible){
- _visible = true;
- _ShowAdBanner();
- Debug.Log("BannerView:ShowAdBanner");
- }
- }
- #endif
- }
- public void HideAdBanner()
- {
- #if UNITY_IPHONE
- if (Application.platform != RuntimePlatform.OSXEditor){
- if(_visible){
- _visible = false;
- _HideAdBanner();
- Debug.Log("BannerView:HideAdBanner");
- }
- }
- #endif
- }
- #endregion
- }
- iADReceiver.cs is the class that get called by the objective-c
- using UnityEngine;
- using System.Collections;
- /// <summary>
- /// Use iADReceiver to get the events from native code
- /// </summary>
- public class iADReceiver : MonoBehaviour {
- public void OnLoaded(string msg){
- iADBannerView.TriggerOnLoaded();
- }
- public void OnClicked(string msg){
- iADBannerView.TriggerOnClicked();
- }
- }
- iOS/BannerViewController.mm
- iOS/BannerViewController.h are native objective-c code for iAd.
- iOS/BannerViewController.h are native objective-c code for iAd.
- #import "BannerViewController.h"
- #import "iPhone_View.h"
- @implementation BannerViewController
- @synthesize adBanner;
- @synthesize type; //0:Banner | 1:MediumRect;
- @synthesize layout; //0:Top | 1:Center | 2:Bottom
- - (id)initWith_Type:(int)iType and_Layout:(int)iLayout
- {
- NSLog(@"initWithTypeLayout");
- self = [super init];
- if(self){
- self.type = iType;
- if(iType==1 && UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad){
- NSLog(@"Only iPad can support ADAdTypeMediumRectangle");
- self.type = 0;
- }
- self.layout = iLayout;
- }
- return self;
- }
- - (void)viewDidLoad
- {
- NSLog(@"viewDidLoad");
- [super viewDidLoad];
- #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
- adBanner = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
- #else
- if(type==0){
- adBanner = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
- }
- else if(type==1){
- adBanner = [[ADBannerView alloc] initWithAdType:ADAdTypeMediumRectangle];
- }
- #endif
- adBanner.delegate = self;
- adBanner.hidden = YES;
- self.view = adBanner;
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- }
- #pragma mark iAd Delegate Methods
- // Method is called when the iAd is loaded.
- - (void)bannerViewDidLoadAd:(ADBannerView *)banner
- {
- NSLog(@"bannerViewDidLoadAd");
- [self setup];
- }
- // Method is called when the iAd fails to load.
- - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
- {
- NSLog(@"didFailToReceiveAdWithError");
- }
- //Method is called When the bounds change for a view controller’s view.
- - (void)viewDidLayoutSubviews
- {
- [self setup];
- }
- - (void)setup
- {
- CGRect contentFrame = UnityGetGLViewController().view.bounds;
- CGRect bannerFrame = CGRectZero;
- //Setup Banner size
- #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
- if (contentFrame.size.width < contentFrame.size.height){
- adBanner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
- }
- else{
- adBanner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
- }
- bannerFrame = adBanner.frame;
- #else
- bannerFrame.size = [adBanner sizeThatFits:contentFrame.size];
- #endif
- if (adBanner.bannerLoaded){
- NSLog(@"adBanner.bannerLoaded");
- switch (layout) {
- case 0:
- bannerFrame.origin.y = 0;
- break;
- case 1:
- bannerFrame.origin.y = contentFrame.size.height/2 - bannerFrame.size.height/2;
- break;
- case 2:
- bannerFrame.origin.y = contentFrame.size.height - bannerFrame.size.height;
- break;
- default:
- bannerFrame.origin.y = 0;
- break;
- }
- bannerFrame.origin.x = contentFrame.size.width/2-bannerFrame.size.width/2;
- UnitySendMessage("iAdPrefab", "OnLoaded", "");
- }
- else{
- NSLog(@"adBanner.bannerNotLoaded");
- bannerFrame.origin.y = contentFrame.size.height;
- }
- adBanner.frame = bannerFrame;
- }
- - (void)show
- {
- if (adBanner.bannerLoaded){
- NSLog(@"show adBanner");
- adBanner.hidden = NO;
- }
- }
- - (void)hide
- {
- if (adBanner.bannerLoaded){
- NSLog(@"hide adBanner");
- adBanner.hidden = YES;
- }
- }
- @end
- static BannerViewController *bannerViewController = nil;
- // When native code plugin is implemented in .mm / .cpp file, then functions
- // should be surrounded with extern "C" block to conform C function naming rules
- extern "C" {
- void _StartAdBanner(int iType, int iLayout) {
- if(bannerViewController == NULL) {
- bannerViewController = [[BannerViewController alloc] initWith_Type:iType and_Layout:iLayout];
- [UnityGetGLViewController().view addSubview:bannerViewController.view];
- }
- }
- void _StopAdBanner() {
- if(bannerViewController != NULL) {
- [bannerViewController release];
- }
- }
- void _ShowAdBanner(){
- if(bannerViewController != NULL) {
- [bannerViewController show];
- }
- }
- void _HideAdBanner(){
- if(bannerViewController != NULL) {
- [bannerViewController hide];
- }
- }
- }
Reference:
Building Plugins for iOS
http://docs.unity3d.com/Documentation/Manual/PluginsForIOS.html
ADBannerView Class Reference
https://developer.apple.com/library/ios/documentation/userexperience/Reference/ADBannerView_Ref/Reference/Reference.html
iAd Unity Plugin
http://www.twistednoodle.com/iad_unity_plugin
How to get the view controller(uiViewController)
http://answers.unity3d.com/questions/40494/who-can-help-me-how-to-get-the-view-controlleruivi.html
Building Plugins for iOS
http://docs.unity3d.com/Documentation/Manual/PluginsForIOS.html
ADBannerView Class Reference
https://developer.apple.com/library/ios/documentation/userexperience/Reference/ADBannerView_Ref/Reference/Reference.html
iAd Unity Plugin
http://www.twistednoodle.com/iad_unity_plugin
How to get the view controller(uiViewController)
http://answers.unity3d.com/questions/40494/who-can-help-me-how-to-get-the-view-controlleruivi.html