AdMob利用しよう
AdMOBをUNITYに取り入れる
1.「Unity用AdMob Plugin」DLしましょう
https://github.com/googleads/googleads-mobile-unity/releases
「GoogleMobileAds.unitypackage」というパッケージファイルをDLし、
Unityプロジェクトにインポートしましょう。
[import]をクリックして、インポートをしましょう
2 [GoogleMobileAds.framework]DLしましょう
https://developers.google.com/admob/ios/download
DLしたframeworkをunity project-Plugins-iOSに入れる
GoogleMovileAds.frameworkを選択し、inspectorが表示され、
Platform settingsーFramework dependenciesの項目にチェックを入れる
- AdSupport
- AudioToolbox
- AVFoundation
- CoreGraphics
- CoreTelephony
- EventKit
- EventKitUI
- MessageUI
- StoreKit
- SystemConfiguration
追加されたGOOGLEMOBILEADSの中にEDITORーGoogleMobileAdsDependencies.xml
ファイルをクリックして
bitcodeEnabled="true" に変更する。
これで、UNITYからビルドして、frameworkが設定完了。
3. 広告スクリプトを作りましょう
using
UnityEngine;
using
System.Collections;
using
GoogleMobileAds.Api;
public
class
AdMobManager : MonoBehaviour {
public
string
Android_Banner;
public
string
Android_Interstitial;
public
string
ios_Banner;
public
string
ios_Interstitial;
private
InterstitialAd interstitial;
private
AdRequest request;
bool
is_close_interstitial =
false
;
// Use this for initialization
void
Awake () {
// 起動時にインタースティシャル広告をロードしておく
RequestInterstitial ();
// バナー広告を表示
RequestBanner ();
}
// Update is called once per frame
void
Update () {
}
public
void
RequestBanner()
{
#if UNITY_ANDROID
string
adUnitId = Android_Banner;
#elif UNITY_IPHONE
string
adUnitId = ios_Banner;
#else
string
adUnitId =
"unexpected_platform"
;
#endif
// Create a 320x50 banner at the top of the screen.
BannerView bannerView =
new
BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
// Create an empty ad request.
AdRequest request =
new
AdRequest.Builder().Build();
// Load the banner with the request.
bannerView.LoadAd(request);
}
public
void
RequestInterstitial()
{
#if UNITY_ANDROID
string
adUnitId = Android_Interstitial;
#elif UNITY_IPHONE
string
adUnitId = ios_Interstitial;
#else
string
adUnitId =
"unexpected_platform"
;
#endif
if
(is_close_interstitial ==
true
) {
interstitial.Destroy ();
}
// Initialize an InterstitialAd.
interstitial =
new
InterstitialAd (adUnitId);
// Create an empty ad request.
request =
new
AdRequest.Builder ().Build ();
// Load the interstitial with the request.
interstitial.LoadAd (request);
interstitial.OnAdClosed += HandleAdClosed;
interstitial.OnAdFailedToLoad += HandleAdReLoad;
is_close_interstitial =
false
;
}
// インタースティシャル広告を閉じた時に走る
void
HandleAdClosed(
object
sender, System.EventArgs e)
{
is_close_interstitial =
true
;
RequestInterstitial();
}
// 広告のロードに失敗したときに走る
void
HandleAdReLoad(
object
sender, System.EventArgs e)
{
is_close_interstitial =
true
;
StartCoroutine(_waitConnect());
}
// 次のロードまで30秒待つ
IEnumerator _waitConnect()
{
while
(
true
)
{
yield
return
new
WaitForSeconds(30.0f);
// ネットに接続できるときだけリロード
if
(Application.internetReachability != NetworkReachability.NotReachable)
{
RequestInterstitial();
break
;
}
}
}
public
void
interstitialShow()
{
if
(interstitial.IsLoaded())
{
interstitial.Show();
}
}
}
4.BUILDして、エラ〜発生
vi ~/.bash_profile
最後 source ~/.bash_profile
UNITY 再度BUILDして、エラーなくなりました。
5.TERMINAL で pod install --repo-update
6. XcodeでBUILDしてみましょう