How To Add AdMob Ads Into Your Existing Unity3D iOS (iPhone/iPad/iPod) Game

New tutorial: How To Add AdMob Ads Into Your Existing Unity3D iOS (iPhone/iPad/iPod) Game vol.2 - Unity Engine - Unity Discussions
How To Add AdMob Ads Into Your Existing Unity3D iOS (iPhone/iPad/iPod) Game
(my setup Mac OS 10.8.2/Xcode 4.6/Unity 3.7(latest 3.x)/AdMob 6.3.0)

It is really easy, but took me almost a week to figure it out! I don’t know Object C and never programmed in Xcode. I hope this short tutorial will help some people, because I did not find any good solution on the net.

1. Manage Libraries in Xcode (source: AdMob  |  Google for Developers)

  • Download the AdMob SDK
  • Create a new folder in your project root folder called “GoogleAdMobAdsSdkiOS”
  • Copy all files from the downloaded SDK (but not the “Add-ons” folder) into the new “GoogleAdMobAdsSdkiOS”
  • Right click your project (“Unity-iPhone”) and press “Add-Files to Unity iPhone” and select the new “GoogleAdMobAdsSdkiOS” folder in your project’s root
  • Open the Link Binary With Libraries dropdown under the Build Phases tab. Add the frameworks from the iOS SDK using the + button that becomes visible. Add StoreKit, MessageUI and AdSupport to both targets
  • You now need to add -ObjC to the Other Linker Flags of !THE PROJECT! (not the targets)
  • STEP ONE DONE :smile:

2. Add some code (source: AdMob  |  Google for Developers)

  • Open the “AppController.mm” file
  • Add following code below the last #import:
//// ******************* ////
//// *** AdMob START *** ////
//// ******************* ////
#import "GADBannerView.h"
//// ******************* ////
//// *** AdMob END   *** ////
//// ******************* ////
  • Add the following code below the line “UIWindow * _window;”
//// ******************* ////
//// *** AdMob START *** ////
//// ******************* ////
GADBannerView* _adBanner;
//// ******************* ////
//// *** AdMob END   *** ////
//// ******************* ////
  • Add the following code before the line “[_window makeKeyAndVisible];”
    AND!!! replace “PUT YOUR ID HERE” with your AdMob publisher ID
    //// ******************* ////
    //// *** AdMob START *** ////
    //// ******************* ////
    // 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.adUnitID = @"PUT YOUR ID HERE";
    [_adBanner setRootViewController:controller];
    [view addSubview:_adBanner];
    _adBanner.center = CGPointMake(_adBanner.center.x+
                MAX(
                    view.frame.size.height-kGADAdSizeBanner.size.width,
                    view.frame.size.width-kGADAdSizeBanner.size.width),
                _adBanner.center.y);
    GADRequest *request = [GADRequest request];
    [_adBanner loadRequest:request];
    //// ******************* ////
    //// *** AdMob END   *** ////
    //// ******************* ////
  • Add the following code before the line “[_window release];”
    //// ******************* ////
    //// *** AdMob START *** ////
    //// ******************* ////
    _adBanner.delegate = nil;
    [_adBanner release];
    //// ******************* ////
    //// *** AdMob END   *** ////
    //// ******************* ////
  • STEP 2 DONE :stuck_out_tongue:
  1. Run in circles and be happy, feel free to change the bunner style or position

Over Power guide. You sir are my hero :stuck_out_tongue:
Bump this guide up please guys

Been looking for something like that :smile:

Is there a version for Unity3D 4.1+ ???

Thanks!

I’m still on Unity 3, but I suppose that this code will also work on Unity 4

Hey guys,

No, it doesn’t work in Unity 4. Any ideas?

Thanks.

For Unity 4 you have to do these changes in iPhone_View.mm. The banner must be added in CreateViewHierarchy()

I moved [_mainDisplay->window makeKeyAndVisible]; at the end of the method and added the code slightly changed right before it.

So the modified function would look like this:

void CreateViewHierarchy()
{
    
    //// ******************* ////
    //// *** AdMob START *** ////
    //// ******************* ////
    
    GADBannerView* _adBanner;
    
    //// ******************* ////
    //// *** AdMob END   *** ////
    //// ******************* ////

    _mainDisplay = [[DisplayManager Instance] mainDisplay];
    
    
    //[_mainDisplay->window makeKeyAndVisible];

    static bool _ClassInited = false;
    if(!_ClassInited)
    {
        AddOrientationSupportDefaultImpl([UnityViewController class]);
        _ClassInited = true;
    }

    _viewController = [[UnityViewController alloc] init];
    _viewController.wantsFullScreenLayout = TRUE;
    _viewController.view = _mainDisplay->view;

    [UIView setAnimationsEnabled:NO];

    ShowSplashScreen(_mainDisplay->window);

    NSNumber* style = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"Unity_LoadingActivityIndicatorStyle"];
    ShowActivityIndicator([SplashScreen Instance], style ? [style intValue] : -1 );
    
    
    //// ******************* ////
    //// *** AdMob START *** ////
    //// ******************* ////
    
    // Initialize the banner at the bottom of the screen.
    
    CGPoint origin = CGPointMake(10.0, 0.0);
    
    // Use predefined GADAdSize constants to define the GADBannerView.
    
    _adBanner = [[[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner origin:origin] autorelease];
    
    _adBanner.adUnitID = @"PUT YOUR ID HERE";
    
    [_adBanner setRootViewController:_viewController];
    
    [_mainDisplay->view addSubview:_adBanner];
    
    _adBanner.center = CGPointMake(_adBanner.center.x+
                                   
                                   MAX(
                                       
                                       _mainDisplay->view.frame.size.height-kGADAdSizeBanner.size.width,
                                       
                                       _mainDisplay->view.frame.size.width-kGADAdSizeBanner.size.width),
                                   
                                   _adBanner.center.y);
    
    GADRequest *request = [GADRequest request];
    request.testDevices = [NSArray arrayWithObjects:GAD_SIMULATOR_ID, nil];
    
    [_adBanner loadRequest:request];
    
    //// ******************* ////
    //// *** AdMob END   *** ////
    //// ******************* ////
    
    [_mainDisplay->window makeKeyAndVisible];
}

Hello Guys,
I have some problems with integrating the Admob into my game. It just shows this:


What should I do?
PS: Which is the viewcontroller from the row 26-29?

Hey Developer_X,

are you working with Unity 3.x or Unity 4.x?

If you are working with Unity 4 have you seen what Florin O. has written?

Yes, I’ve done how Florin O. wrote there.
I have an error:

Maybe you can send Florin O. a private message. I’m still not on Unity 4, hence I cannot tell you wants wrong with your code…

I am getting the error “duplicate symbols for architecture armv7 admob”
:frowning:

Hey Developer_X, if you still have that problem, this is my modification from Florin.O’s code

void CreateViewHierarchy()
{
    //// ******************* ////
    //// *** AdMob START *** ////
    //// ******************* ////

    GADBannerView* _adBanner;

    //// ******************* ////
    //// *** AdMob END   *** ////
    //// ******************* ////
    _mainDisplay = [[DisplayManager Instance] mainDisplay];

    //[_mainDisplay->window makeKeyAndVisible];

    static bool _ClassInited = false;
    if(!_ClassInited)
    {
        AddOrientationSupportDefaultImpl([UnityViewController class]);
        _ClassInited = true;
    }

    [UIView setAnimationsEnabled:NO];

    ShowSplashScreen(_mainDisplay->window);
    NSNumber* style = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"Unity_LoadingActivityIndicatorStyle"];
    ShowActivityIndicator([SplashScreen Instance], style ? [style intValue] : -1 );

    //// ******************* ////
    //// *** AdMob START *** ////
    //// ******************* ////

    // Use predefined GADAdSize constants to define the GADBannerView.
    _adBanner = [[[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner] autorelease];
    _adBanner.adUnitID = @"PUT YOUR ID HERE";

    [_adBanner setRootViewController:UnityGetGLViewController()];

    [UnityGetGLViewController().view addSubview:_adBanner];

    GADRequest *request = [GADRequest request];
    request.testDevices = [NSArray arrayWithObjects:GAD_SIMULATOR_ID, nil];

    [_adBanner loadRequest:request];

    //// ******************* ////
    //// *** AdMob END   *** ////
    //// ******************* ////

    [_mainDisplay->window makeKeyAndVisible];
}

This will show the ads on the top. You can modify it yourself to show it on the bottom.
It works for me on the simulator.

It is supposed to work right away? I used the code above but I’m getting this linker error:

Ld /Users/carloscarrera/Library/Developer/Xcode/DerivedData/Unity-iPhone-bbzwxoyvxoxuqkfrzyojvzipopek/Build/Products/ProductName.app/ProductName normal i386
    cd "/Users/carloscarrera/Documents/Unity Projects/Flappy Bird/build 0.0"
    setenv IPHONEOS_DEPLOYMENT_TARGET 4.0
    setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk -L/Users/carloscarrera/Library/Developer/Xcode/DerivedData/Unity-iPhone-bbzwxoyvxoxuqkfrzyojvzipopek/Build/Products -L/Users/carloscarrera/Documents/Unity\ Projects/Flappy\ Bird/build\ 0.0 -L\"/Users/carloscarrera/Documents/Unity\ Projects/Flappy\ Bird/build\ 0.0/Libraries\" -L/Users/carloscarrera/Documents/Unity\ Projects/Flappy\ Bird/build\ 0.0/GoogleAdMobAdsSdkiOS -F/Users/carloscarrera/Library/Developer/Xcode/DerivedData/Unity-iPhone-bbzwxoyvxoxuqkfrzyojvzipopek/Build/Products -filelist /Users/carloscarrera/Library/Developer/Xcode/DerivedData/Unity-iPhone-bbzwxoyvxoxuqkfrzyojvzipopek/Build/Intermediates/Unity-iPhone.build/Debug-iphonesimulator/Unity-iPhone.build/Objects-normal/i386/ProductName.LinkFileList -Xlinker -objc_abi_version -Xlinker 2 -weak_framework CoreMotion -weak-lSystem -ObjC -all_load -stdlib=libstdc++ -fobjc-link-runtime -Xlinker -no_implicit_dylibs -mios-simulator-version-min=4.0 -framework StoreKit -lGoogleAdMobAds -framework AdSupport -framework MessageUI -framework Foundation -framework UIKit -framework OpenGLES -framework QuartzCore -framework OpenAL -liconv.2 -liPhone-lib -framework AudioToolbox -framework CFNetwork -framework MediaPlayer -framework CoreLocation -framework SystemConfiguration -weak_framework iAd -framework CoreMedia -framework CoreVideo -weak_framework AVFoundation -framework CoreGraphics -weak_framework CoreMotion -weak_framework GameKit -Xlinker -dependency_info -Xlinker /Users/carloscarrera/Library/Developer/Xcode/DerivedData/Unity-iPhone-bbzwxoyvxoxuqkfrzyojvzipopek/Build/Intermediates/Unity-iPhone.build/Debug-iphonesimulator/Unity-iPhone.build/Objects-normal/i386/ProductName_dependency_info.dat -o /Users/carloscarrera/Library/Developer/Xcode/DerivedData/Unity-iPhone-bbzwxoyvxoxuqkfrzyojvzipopek/Build/Products/ProductName.app/ProductName

ld: warning: directory not found for option '-L"/Users/carloscarrera/Documents/Unity Projects/Flappy Bird/build 0.0/Libraries"'
ld: library not found for -liPhone-lib
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Any ideas?

Using the ad-mob sample I am having issues after building unity project to xcode.

After importing the Admob framework to xcocde and setting other linking flags, ive reciving a few errors:

Undefined symbols for architecture armv7:
OBJC_CLASS$_CTTelephonyNetworkInfo”, referenced from:
objc-class-ref in libGoogleAdMobAds.a(GADDevice.o)
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

please help !

I have used :Google Ads Developer Blog: AdMob Plugin for Unity3D Add-on and instructions.

and AdMob  |  Google for Developers

@tolokobcn

Hi,

Yes, you have to edit : Libray Search Paths in XCode Build Settings and remove “back slashes” symbols in this path :

“$(SRCROOT)/Libraries”

So I used this code and it works great except I’m not extremely experienced with these things (first app ever). So I was wondering how I would modify it to show on the bottom or the sides or whatnot like you mentioned. Thanks!

Thank you FreebordMAD and Florin O, it looks awesome. Have not done my implementation yet, but one quick question: does this require Unity PRO and/or Unity IOS Pro?

@Larsey: no PRO is needed

i have the same linker problem
the directory is "$(SRCROOT)/Libraries" /Users/…
but it doesn’t work.
anyone can help me?