饂飩コーディング

iOSアプリやら、Unityやら、Cocos2dやらごにょごにょ書いております

Unity→Xcodeでadmob実装してみる。とりあえず表示するだけバージョン

サイトが引っ越しました。→https://scombu.com

約1秒後に自動的にリダイレクトします。切り替わらない場合はリンクをクリックしてください。

久しぶりにadmob実装してみます。今回はUnityでbuildしてXcodeで実装してみます。
Pluginは使わないので簡単だけどただ表示するだけです。

Unityのバージョンによってもどのファイルに実装すればいいのか変わってくるみたい
今回はUnity4.5.1こいつでいきます。

https://developers.google.com/mobile-ads-sdk/download?hl=ja
GoogleのDeveloperサイトからiOS用のSDKをダウンロードします


Unityで簡単なプロジェクトを作って、iOS用にBuildします
ここではiOSBuildという名前でBuildしていわゆるXcode用のソースをUnityから吐き出します。
f:id:appdeappuappu:20140630214925p:plain


ここからはXcodeで作業します。projectファイルをダブルクリックして
https://developers.google.com/mobile-ads-sdk/docs/?hl=ja#ios
の手順にしたがってadmobファイルとFrameworkを追加していきます。

まずは、add-ons以外をxcodeに取り込みます。
f:id:appdeappuappu:20140630215738p:plain

以下のフレームワークを取り込みます
AdSupport
AudioToolbox
AVFoundation
CoreGraphics
MessageUI
StoreKit
SystemConfiguration
CoreTelephony
f:id:appdeappuappu:20140630220317p:plain

GoogleのDocumentにはなぜか漏れているんだけど忘れずにCoreTelephony.frameworkも
追加します。自分はこれ入れてなくてエラーの文字読んでたら必要なんじゃない?って気づきました
f:id:appdeappuappu:20140630220252p:plain

次は、Build Settings のLinkingのOther Linker Flagに"-ObjC"を追加

とりあえずココで一度デバッグ実行してみるとエラーがでてくることがあります
f:id:appdeappuappu:20140630221138p:plain
これはLibrariesへのSearch Pathが正しく設定されていないっぽいのが原因なので
Librarie Search Path を設定しなおします。
↓こんな感じ
f:id:appdeappuappu:20140630222022p:plain
libiPhone-libのディレクトリをFinderで調べてLibrary Search Path の所に追加して
もう一つlibGoogleAdMobAdsのディレクトリも追加してあげればOKでしょう

ここまでデバッグ実行して警告がでなければOK


ここからコーディングします

UnityAppController.mmを探してこれを変更していきます
基本はGoogleの導入手順書にそって変更していきますが全く同じではないです
UnityAppController+ViewHandling.mmに
#import "GADBannerView.h"を追加

さらに-(void)createViewHierarchyメソッドを書き換えます

- (void)createViewHierarchy
{
/////////////////
    GADBannerView* _adBanner;
/////////////////
    
	AddViewControllerAllDefaultImpl([UnityDefaultViewController class]);

	NSAssert(_unityView != nil, @"_unityView should be inited at this point");
	NSAssert(_window != nil, @"_window should be inited at this point");

	[self createViewHierarchyImpl];
	NSAssert(_rootView != nil, @"createViewHierarchyImpl must assign _rootView");
	NSAssert(_rootController != nil, @"createViewHierarchyImpl must assign _rootController");

	_rootView.contentScaleFactor = [UIScreen mainScreen].scale;
	_rootView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

	_rootController.wantsFullScreenLayout = TRUE;
	_rootController.view = _rootView;
	if([_rootController isKindOfClass: [UnityViewControllerBase class]])
		[(UnityViewControllerBase*)_rootController assignUnityView:_unityView];

	//[_window makeKeyAndVisible];
	[UIView setAnimationsEnabled:NO];

	// TODO: extract it?

	ShowSplashScreen(_window);

	NSNumber* style = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"Unity_LoadingActivityIndicatorStyle"];
	ShowActivityIndicator([SplashScreen Instance], style ? [style intValue] : -1 );
    
    
    
/////////////////
    CGPoint origin = CGPointMake(0.0, 0.0);
    _adBanner = [[[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner origin:origin] autorelease];
    _adBanner.adUnitID = @"あなたのPublisher ID";
    [_adBanner setRootViewController:_rootController];
    [_mainDisplay->view addSubview:_adBanner];
    
   
     GADRequest *request = [GADRequest request];
    request.testDevices = [NSArray arrayWithObjects:
                           GAD_SIMULATOR_ID,
                           @"テスト用端末のUDID",
                           nil];

    [_adBanner loadRequest:request];
    
    //[_mainDisplay->window makeKeyAndVisible];
    [_window makeKeyAndVisible];
/////////////////
    
}


広告の位置設定はadBanner.centerをCGPointMakeか何かで設定すればOKかとおもわれます

こんな感じになります

https://www.youtube.com/watch?v=j1PgFzo88hA&feature=youtu.be