- Add code to the merge project
- add this c # class to your Unity project
- call functions of this class somewhere from your project
- Call the AD_AdsiOS constructor as early as possible and submit your AdMob ad ID
- Call "ShowInterstitialAd" to display a full-screen ad.
- Call "ShowBannerAdTopRight" to display the advertisement in the upper right corner.
- Call "HideBannerAds" to hide the ad.
#if UNITY_IPHONE using UnityEngine; using System.Collections; using System.Runtime.InteropServices; public class AD_AdsiOS { #if !UNITY_EDITOR [DllImport ("__Internal")] private static extern void _initAds(string p_adMobID); [DllImport ("__Internal")] private static extern void _showInterstitialAd(); [DllImport ("__Internal")] private static extern void _showBannerAdTopRight(); [DllImport ("__Internal")] private static extern void _hideBannerAds(); #endif public AD_AdsiOS(string p_adMobID) { #if UNITY_EDITOR Debug.Log("AD_AdsiOS: will not work in editor."); #else _initAds(p_adMobID); #endif } public void ShowInterstitialAd() { #if UNITY_EDITOR Debug.Log("AD_AdsiOS: ShowInterstitialAd called in editor."); #else _showInterstitialAd(); #endif } public void ShowBannerAdTopRight() { #if UNITY_EDITOR Debug.Log("AD_AdsiOS: ShowBannerAdTopRight called in editor."); #else _showBannerAdTopRight(); #endif } public void HideBannerAds() { #if UNITY_EDITOR Debug.Log("AD_AdsiOS: HideBannerAds called in editor."); #else _hideBannerAds(); #endif } } #endif
Download the AdMob SDK Create a new folder in the root folder of the project called "GoogleAdMobAdsSdkiOS" Copy all the files from the downloaded SDK (but not the Add-ons folder) to the new "GoogleAdMobAdsSdkiOS" Right-click your project ("Unity-iPhone") and click " Add files to Unity iPhone "and select the new" GoogleAdMobAdsSdkiOS "folder in the project root directory. Open the Link Binary With Libraries drop-down list on the Build Phases tab. Add frameworks from the iOS SDK using the + button, which will become visible. Add SystemConfiguration, StoreKit, MessageUI, AVFoundation, and AdSupport for both purposes (check the troubleshooting in steps 1 and 2). Now you need to add -ObjC to other Linker flags! PROJECT! (not goals)
STEP 2 DONE: D
- Add code (source: https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals )
add the following two files to the Classes folder of your project and import them
MyAdMobAdsImpl.h
#ifndef __Unity_iPhone__MyAdMobAdsImpl__ #define __Unity_iPhone__MyAdMobAdsImpl__ void InitMyAdMob(UIView* p_rootView, UIViewController* p_rootController); void DestroyMyAdMob(); #endif
MyAdMobAdsImpl.mm
#import "GADBannerView.h" #import "GADInterstitial.h" #include "MyAdMobAdsImpl.h" NSString* _adMobID; UIViewController* _adRootViewController; GADBannerView* _adBanner; GADInterstitial *_adInterstitial; float _adBannerRelWidth; float _adBannerRelHeight; extern "C" { void _initAds (const char* p_adMobID) { _adMobID = [NSString stringWithUTF8String:p_adMobID]; _adBanner.adUnitID = _adMobID; _adInterstitial.adUnitID = _adMobID; GADRequest *request = [GADRequest request]; [_adInterstitial loadRequest:request]; } void _showInterstitialAd () { [_adInterstitial presentFromRootViewController:_adRootViewController]; // get next add _adInterstitial = [[GADInterstitial alloc] init]; _adInterstitial.adUnitID = _adMobID; GADRequest *request = [GADRequest request]; [_adInterstitial loadRequest:request]; } void _showBannerAdTopRight () { _adBanner.hidden = NO; // force refresh GADRequest *request = [GADRequest request]; [_adBanner loadRequest:request]; } void _hideBannerAds () { _adBanner.hidden = YES; } float _getAdsRelativeWidth () { return _adBannerRelWidth; } float _getAdsRelativeHeight () { return _adBannerRelHeight; } } void InitMyAdMob(UIView* p_rootView, UIViewController* p_rootController) { _adRootViewController = p_rootController; // Initialize the banner at the bottom of the screen. CGPoint origin = CGPointMake(0.0, 0.0); // Use predefined GADAdSize constants to define the GADBannerView. _adBanner = [[[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner origin:origin] autorelease]; [_adBanner setRootViewController:p_rootController]; [p_rootView addSubview:_adBanner]; // ios frame size is not rotated -> exchange width-height _adBannerRelWidth = kGADAdSizeBanner.size.width / p_rootView.frame.size.height; _adBannerRelHeight = kGADAdSizeBanner.size.height / p_rootView.frame.size.width; _adBanner.center = CGPointMake(_adBanner.center.x+ MAX(p_rootView.frame.size.height-kGADAdSizeBanner.size.width, p_rootView.frame.size.width-kGADAdSizeBanner.size.width), _adBanner.center.y); // Initialize the interstitial ad _adInterstitial = [[GADInterstitial alloc] init]; } void DestroyMyAdMob() { _adBanner.delegate = nil; [_adBanner release]; [_adInterstitial release]; }
These two files implement AdMob logic. Using this code, you can show and hide banner ads in the upper right corner and show full-screen ads. In addition, using getAdsRelative * you can get the banner size in the relative screen space (0.1 for the width means that the banner accepts 0.1 * pixels of Screen.width). These methods are not references in the first step for simplicity, but you can use them if you need to.
- Now the InitMyAdMob and DestroyMyAdMob methods should be called from Unity classes
- Open the file "UI / UnityAppController + ViewHandling.mm"
- Add the following code under the last #include: Code (CSharp):
//// ******************* //// //// *** AdMob START *** //// //// ******************* //// #import "MyAdMobAdsImpl.h" //// ******************* //// //// *** AdMob END *** //// //// ******************* ////
- Add the following code to the "createViewHierarchy" function immediately before "[_window makeKeyAndVisible];" Code (CSharp):
//// ******************* //// //// *** AdMob START *** //// //// ******************* //// InitMyAdMob(_rootView, _rootController); //// ******************* //// //// *** AdMob END *** //// //// ******************* ////
- Add the following code to the beginning of the "releaseViewHierarchy" Code (CSharp) function:
//// ******************* //// //// *** AdMob START *** //// //// ******************* //// DestroyMyAdMob(); //// ******************* //// //// *** AdMob END *** //// //// ******************* ////
STEP 3 DONE: p
- Troubleshooting
- if you get a "library not found for -liPhone-lib", be sure to move it at the top of the "Phase Assembly" โ "Link Binary Files to Libraries" tab
- if you get the Undefined character ... _OBJC_CLASS _ $ _ CTTelephonyNetworkInfo "add the CoreTelephony tab in the phase assembly โ Link the Binary With Libraries drop-down list
- if you get about 20 errors in the NSObjCRuntime.h file or in some similar NSObjC * file: make sure that the "File Type" drop-down list is set to "Objective-C ++ Source" (in the far right column when choosing a file) for files that you add to the project. Thus, it processes files correctly and uses the correct paths and stuff. source: http://forum.thegamecreators.com/?m=forum_view&t=201075&b=41
- In the new version of Xcode with the new version of AdMob, additional libraries must be added (EventKit.framework and EventKitUI.framework). Or you get: "_OBJC_CLASS _ $ _ EKEvent" referenced by: objc-class-ref in libGoogleAdMobAds.a (GADOpener.o) "_OBJC_CLASS _ $ _ EKEventEditViewController" referenced by: objc-class-ref in libGoogleAdMobds. a (GADOpener.o)
http://stackoverflow.com/questions/25950990/undefined -symbols-for-architecture-when-add-admob
source share