Search Unity

ads with AdMob

Discussion in 'iOS and tvOS' started by scinfu, Jan 12, 2009.

  1. scinfu

    scinfu

    Joined:
    Oct 23, 2008
    Posts:
    404
    there is a way to integrate the ads of admob to a project XCODE ?

    http://www.admob.com/ ...

    i try to change the file "xib" but nothing ....


    there is someone who has succeeded?
     
  2. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    I talked to them a while ago and asked them if they can create some lib to be used in Unity as a lot of games get created with Unity. So far the answer was that they sent my request off to their engineers, but never heard back.

    Integration their core lib would not work because the app would run in Open GL I guess, but I might be wrong.
     
  3. scinfu

    scinfu

    Joined:
    Oct 23, 2008
    Posts:
    404
    then it is not a problem of unity !!!!

    ok ... any way to make ads ?
     
  4. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Well no, of course it's not a problem of Unity. Uhm, did I say that somewhere? Didn't mean that.

    Just wanted to say: I talked to the Admob people if they can make their stuff compatible to Unity so that we get a simple solution to display ads in Unity made iPhone games.
     
  5. ReJ

    ReJ

    Unity Technologies

    Joined:
    Nov 1, 2008
    Posts:
    378
    @Martin: what API Admob provides for iPhone right now? Do they have ObjectiveC/C++ APIs? (I couldn't found anything usefull on their site without registering)
     
  6. scinfu

    scinfu

    Joined:
    Oct 23, 2008
    Posts:
    404
    this project
     

    Attached Files:

  7. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    @ReJ: Yes, that's what they told me (Objective-c lib). A simple to use library which can be plugged into existing code and which would then display the ads when called from code. I'm registered on the site and await soon my clearing to be able to download the SDK.
     
  8. cheezorg

    cheezorg

    Joined:
    Jun 5, 2008
    Posts:
    394
    This would be great, especially for getting something back on free Lite versions.

    Also, App Store customers are expecting quite a bit for 99 cents these days. I'm wondering how long indies will be able to compete in the paid app space. I'm hoping for a long while, but it's getting pretty crowded in there.
     
  9. gutripper

    gutripper

    Joined:
    Sep 30, 2008
    Posts:
    45
    Did anybody ever figure this problem out?

    I'd be interested to know about how to integrate AdMob myself.

    Thanks.
     
  10. scinfu

    scinfu

    Joined:
    Oct 23, 2008
    Posts:
    404
  11. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    This would not be hard to implement at all. I can take a crack at it. Does AdMob have requirements about when the ads appear? Before and after the Unity engine runs would be simplest.

    Edit: Actually, this looks really easy. Can someone PM me a publisher ID so I can test it?
     
  12. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901


    Got it working. The ad refreshes every 15 seconds (crazy fast, I know, but I wanted to make it fast for testing).

    Since the Unity keeps the phone in portrait orientation (while actually renderings its own view in a landscape orientation) the ad appears rotated along the side. At some point I'll use a CGAffineTranform to rotate the ad.
     
  13. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    576
    Bravo sir!
     
  14. scinfu

    scinfu

    Joined:
    Oct 23, 2008
    Posts:
    404
    you used my code ?


    because I saw that there is an impression, but in Europe ... strange
     
  15. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    scinfu: I used my own publisher id code (and as you can see it's in test mode anyway). And I'm not in Europe, sadly.

    Even sadder is the fact that ad clicks switch to Safari instead of opening a WebKit view. My code is pausing and restarting the Unity engine when the AdMob API says it's about to display or hide a modal view... which it never does.
     
  16. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Nice! Mind sharing your efforts how it was done?
     
  17. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Do you want code snippets, or something you can drop in and forget about? The later will require a PostProcessBuildScript because there's code changes to be made.

    The important part is:
    Code (csharp):
    1.     AdMobView *ad = [[AdMobView requestAdWithDelegate: selfController] retain];
    2.     ad.frame = CGRectMake(0, 0, 320, 48);
    3.     [_window addSubview:ad];
    4.     _timer = [NSTimer scheduledTimerWithTimeInterval:(15.0) target: ad selector:@selector(requestFreshAd) userInfo:nil repeats:YES];
    I retained the view because I don't want it going away if the ad server doesn't return an ad (which apparently it might do) but it's probably not necessary. And obviously, change the timer interval to whatever works for you.

    Oh, and of course, add the AdMob libraries and header files (and the CoreGraphics and CoreLocation frameworks) to the project in XCode.

    You also need to define and set selfController.

    Lots of little details. I'll write up the little details later. Or maybe making a PPBS would be simper.
     
  18. Randy-Edmonds

    Randy-Edmonds

    Joined:
    Oct 10, 2005
    Posts:
    1,122
    Very cool!! Thanks you! Maybe you could add this info to the wiki?
     
  19. mehware

    mehware

    Joined:
    Nov 19, 2007
    Posts:
    739
    i looked at admob and don't you get money if the user clicks the add, well don't you need iphone advanced to open urls?
     
  20. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    The Admob stuff is implemented in Objective-C, not Unity. It should work fine in Basic.
     
  21. rawrman2381238

    rawrman2381238

    Joined:
    Jan 28, 2008
    Posts:
    122
    where and how do i do this?

    also where to i put the code you provided

    thanks
     
  22. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Here's all the steps. I will add this to the wiki later.

    1. First, add these files to the project: AdMobView.h, AdMobDelegate.h, libAdMobDevice.a. (You can just drag them into the source list.) Then add these frameworks to the project (right-click on the "Unity-iPhone" item in the source list, select the menu item for "Add existing framework...", and navigate down to the framework name, and click "Add"): CoreLocation.framework and CoreGraphics.framework.


    2. In AppController.mm, near the top (I've got it at line 11) add:

    Code (csharp):
    1. #import "AdMobView.h"
    That loads the AdMob headers.


    3. In AppController.mm, around line 46, add this line:

    Code (csharp):
    1. AppController* selfController;
    We need add the AdMob view from inside C++ code, so we need a reference to the app controller itself, because the AdMob view needs its delegate. We'll set this to something useful in a bit.


    4. Around line 337, in function "OpenEAGL_UnityCallback", before "return true;", add:

    Code (csharp):
    1.     AdMobView * ad = [AdMobView requestAdWithDelegate: selfController];
    2.     ad.frame = CGRectMake(0, 0, 320, 48);
    3.     [_window addSubview:ad];
    That creates the Ad Mob view. (That's the left/top edge; you can move that rectangle as you need. Portrait orientation only, for now -- I don't have a rotated view working well yet -- hopefully soon! However AdMob seems to have no requirements that their ad match the orientation of an app.)


    5. Around line 455, at the beginning of applicationDidFinishLaunching:, add:
    Code (csharp):
    1.     selfController = self;

    6. Around line 506, before the "@end" of the AppController implementation, add:

    Code (csharp):
    1. #pragma mark AdMobDelegate
    2.  
    3. - (NSString *)publisherId;
    4. {
    5.     return @"a251832df60b780";
    6. }
    7.  
    8. - (BOOL)useTestAd;
    9. {
    10.     return YES;
    11. }
    That's a bogus publisher id, change it out for yours. And change the YES to NO when you're done with test ads.


    7. That should do it! In XCode, Build Run to test it. When you run your app, the ad view will appear along the left/top edge. If you aren't in test mode, AdMob might not send your app an ad, in which case the AdMob view releases itself (it's in the libAdModDevice.a, I can't change that) and so you might not see an ad. This is especially true for apps newly created in the AdMob system. So always start out with useTestAd returning YES to make sure you see something. Also, if you decide to shift your content to the right to make room for the ad, make sure it's not too weird without an ad.


    8. SAVE A COPY! Remember -- the next time you build in Unity, this will get overwritten. You can save this modified file outside the project diretory, and copy it over the one that Unity gives you each time you make a new Unity build. Or you can edit the source file (inside the "Unity iPhone" app bundle) so that Unity is using the modified file when making your build. Or you can use a post process build script to copy this changed file over. It's up to you.


    Note: I've had some folks PM me and ask if I can do it for them. I'm happy to help out, but I do need to charge something for my time.
     
  23. zoul

    zoul

    Joined:
    May 5, 2007
    Posts:
    50
    Using the instructions from the previous post, I get the following errors:

    warning: class 'AppController' does not implement the 'AdMobDelegate' protocol

    As well as 41 linking errors, stating that various things are missing from libAdMobDevice.a(AdMobContact.o) and other various things ... such as _ABAddressBookCopyArrayOfAllPeople, _CGContextClip, _CGRectContainsPoint, etc ...


    Looking at the first error, I'm left with assuming that AppController.h should be updated and the AdMobDelegate added ...

    Adding #import "AdMobDelegateProtocol.h" to AppController.h and then appending "AdMobDelegate" to the protocols for AppController, I still received 41 linking errors.

    Reviewing the AdMob README, I then added AddressBook.framework, CoreGraphics.framework and CoreLocation.framework (AddressBook.framework was missing from the instructions)

    After making these changes, the above instructions compiled successfully and tested without problem.
     
  24. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Sorry David, since I added AdMob support to the Enhancement Pack I forgot about this thread. Those instructions were for the older version of the admob library. Thanks for explaining how to make it work!
     
  25. felixk

    felixk

    Joined:
    Dec 17, 2008
    Posts:
    38
    Guys,

    I got the admob ad showing up in portrait. Anyone know how to make it visible in landscape view?

    I know objective-c so if you can steer me in the right direction i'll figure it out.

    Thanks,
    Felix
     
  26. iphonefreak

    iphonefreak

    Joined:
    Feb 25, 2009
    Posts:
    157
    I followed the instructions above but when trying to install the additional frameworks (CoreLocation.framework and CoreGraphics.framework) they were not in the list of available frameworks under the "Add existing frameworks" option

    Do these frameworks need to be installed? I have iPhone SDK 3.0 installed
     
  27. iphonefreak

    iphonefreak

    Joined:
    Feb 25, 2009
    Posts:
    157
    I just found it. Has to alter the path that came up in XCode to point to the SDK Libs.
     
  28. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Don't forget the Addressbook.Framework, also needed by Admob.
     
  29. iphonefreak

    iphonefreak

    Joined:
    Feb 25, 2009
    Posts:
    157
    Hi Felixk ...did you ever work out how to ge the admob ads working in landscape view?
     
  30. iphonefreak

    iphonefreak

    Joined:
    Feb 25, 2009
    Posts:
    157
    Hi guys ..i worked out how to do AdMob banner in landscape mode. Just replace the AdMobView code in the OpenEAGL_UnityCallback function with this. Enjoy!

    Code (csharp):
    1.  
    2. AdMobView * ad = [AdMobView requestAdWithDelegate: selfController];
    3. ad.frame = CGRectMake(260, 432, 320, 48);
    4.  
    5. #define degreesToRadians(x) (M_PI * x / 180.0)
    6.  
    7. CGAffineTransform makeLandscape = CGAffineTransformMakeRotation(degreesToRadians(90));
    8. makeLandscape = CGAffineTransformTranslate(makeLandscape, -480/2 + 48/2, 320/2 - 48/2 - 12);
    9. ad.transform = makeLandscape;
    10.    
    11. [_window addSubview:ad];
    12. _timer = [NSTimer scheduledTimerWithTimeInterval:(15.0) target: ad selector:@selector(requestFreshAd) userInfo:nil repeats:YES];
    13.  
     
  31. nikko

    nikko

    Joined:
    Mar 20, 2009
    Posts:
    436
    Any step by step tutorial to use Adwhirl? I can't include their SDK...

    Any plan to add support to Adwhirl in the Enhancement pak?
     
  32. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Can we show/hide (switch) admob ads visibility from inside a unity game?
     
  33. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    Using the Enhancement Pack, you can show and hide ads whenever you like.
     
  34. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Did anyone ever experience this rare/random bug where your iphone game screen is mirrored/inverted? I believe it might be related to my admob implementation on 1.1. I should have precisely followed the implementation instructions on the previous page.

    We just got an app rejected for this reason. I only experienced this bug a few times after a new fresh build. Rebuilding or relaunching the app always made it disappear. I have not yet experienced this on unity iphone 1.5 and hope that fixes it (1.5 of course also forced me to add the admob code again..)
     
  35. rawrman2381238

    rawrman2381238

    Joined:
    Jan 28, 2008
    Posts:
    122
    Rob -- your instructions dont work with 1.5

    How do i fix this?
     
  36. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    rawrman2381238, they still work with 1.5 here. What problem are you experiencing?
     
  37. rawrman2381238

    rawrman2381238

    Joined:
    Jan 28, 2008
    Posts:
    122
    the app shows the splash screen, but then quits to the main menu.... every time....

    without the admob code, it works fine
     
  38. rawrman2381238

    rawrman2381238

    Joined:
    Jan 28, 2008
    Posts:
    122
    can someone shoe me how they got admob working with os 3 and unity iphone 1.5?

    thanks
     
  39. blt3d

    blt3d

    Joined:
    Oct 23, 2008
    Posts:
    192
    I was able to add the frameworks and include the changes to AppController.mm successfully in 1.5 But when I Build and Go, I'm not seeing any ads. Any steps I'm missing?

    EDIT:

    I'm getting a warning upon Build : class 'AppController' does not implement the 'AdMobDelegate' protocol.
     
  40. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Yes! :)

    In the AppController.h you need to add the Admob interface ("AdMobDelegate") to get the Admob callbacks work in your controller. This is how my AppController.h looks like:
    (Note the AdMobDelegate as well as some more variables I have in there)

    Code (csharp):
    1. #import <UIKit/UIKit.h>
    2. #import "AdMobDelegateProtocol.h"
    3.  
    4. @class AdMobView;
    5.  
    6. @interface AppController : NSObject<UIAccelerometerDelegate, UIApplicationDelegate, AdMobDelegate>
    7. {
    8.     UIWindow*           _window;
    9.     UIWindow*           aw;
    10.     AdMobView *ad;
    11.     NSTimer *adMobTimer;
    12.     double adMobRefreshInterval;   
    13. }
    14. - (void) startUnity:(UIApplication*)application;
    15. - (void) startRendering:(UIApplication*)application;
    16. - (void) showAdView;
    17. @end
    18.  
    19. #define NSTIMER_BASED_LOOP 0
    20. #define THREAD_BASED_LOOP 1
    21. #define EVENT_PUMP_BASED_LOOP 2
    22.  
    And in the AppController.mm, you need to have this import in:

    Code (csharp):
    1. #import "AdMobView.h"
    Then, in the function "- (void) startUnity:(UIApplication*)application" I usually put in my own code to call Admob 3 seconds after Unity launched, like this:

    Code (csharp):
    1.     [self registerAccelerometer];
    2.     iphone::SetScreenOrientation((iphone::ScreenOrientation)[application statusBarOrientation]);
    3.     iphone::KeyboardOnScreen::Init();
    4.    
    5. // HERE THE CODE TO CALL ADMOB!!!
    6.     NSLog(@"Show Ad: 1");
    7.     [NSTimer scheduledTimerWithTimeInterval: 5.0 target: self selector: @selector(showAdView) userInfo: nil repeats: NO];
    8.     NSLog(@"Show Ad: 2");  
    and finally the function to show Admob:

    Code (csharp):
    1. - (void) showAdView;
    2. {
    3.     NSLog(@"showAdView: about to make Ad View!");
    4.    
    5.     aw = [[UIWindow alloc] initWithFrame: CGRectMake(0, 0, 320, 48)];  
    6.     ad = [AdMobView requestAdWithDelegate: self];
    7.     [aw addSubview: ad];
    8.     aw.hidden = NO;
    9.    
    10.     adMobRefreshInterval = 30.0;
    11.    
    12.     NSLog(@"showAdMobView: starting refresh timer with interval of %f seconds", adMobRefreshInterval);
    13.     adMobTimer = [NSTimer scheduledTimerWithTimeInterval:(adMobRefreshInterval) target: ad selector:@selector(requestFreshAd) userInfo:nil repeats:YES];
    14.    
    15.     NSLog(@"showAdView: Ad View done!");
    16. }
    And after that you need to implement the Admob delegate methods:

    Code (csharp):
    1. #pragma mark -
    2. #pragma mark AdMobDelegate methods
    3.  
    4. - (NSString *)publisherId {
    5.     return @"a149dcf5bb6c26a";
    6. }
    7.  
    8. - (BOOL)mayAskForLocation {
    9.     return NO; // this should be prefilled; if not, see AdMobProtocolDelegate.h for instructions
    10. }
    11.  
    12. - (void)didReceiveAd:(AdMobView *)adView;
    13. {
    14.     NSLog(@"didReceiveAd");
    15. }
    16.  
    17. - (void)didFailToReceiveAd:(AdMobView *)adView;
    18. {
    19.     NSLog(@"didFailToReceiveAd");
    20.     adMobTimer = [NSTimer scheduledTimerWithTimeInterval:(adMobRefreshInterval) target: ad selector:@selector(requestFreshAd) userInfo:nil repeats:YES];
    21. }
    22.  
    Hope this helps. It's not super fancy plugin code that I have here, but works 100% flawless. :)
     
  41. blt3d

    blt3d

    Joined:
    Oct 23, 2008
    Posts:
    192
    Awesome thanks! One question, thoough. How does the refresh interval work? Am I suppose to see different ads appear during a set amount of time? When I click on the ad, it disappears but a new does not take its place. Is the normal? Or does that not function because of Test Mode. Thanks again!
     
  42. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    No, you should see every 30 seconds a new ad coming in, but I have to add that I mostly never use the testmode, but instantly switch into the production mode.
     
  43. blt3d

    blt3d

    Joined:
    Oct 23, 2008
    Posts:
    192
    This probably has something to do with the way Agon works, but whenever I access a blade, and then return to Unity, the Ads disappear and don't return. I'm not sure if thats normal behavior, but everything else is working great, thanks for the help!
     
  44. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    I bet it's just that the Admob view is behind the Unity view. That happens if you let some other API get in front and then switch back to Unity. I would recommand to destroy the Admob view when you call a different API and re-create it once you get back to Unity.
     
  45. blt3d

    blt3d

    Joined:
    Oct 23, 2008
    Posts:
    192
    :eek: Wow..I know the whole give a man a fish or teach him how thing is telling me I should probably figure out how to do that on my own, but since I have NO clue lol maybe you could throw me a bone? 8) I'd appreciate it!
     
  46. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    It took me ages to find all that out, maybe it helps you spend not ages on it :)
     
  47. blt3d

    blt3d

    Joined:
    Oct 23, 2008
    Posts:
    192
    Got it! If anyone else is wondering how to get Admob to work with Agon, I'll share it:

    In AppController.mm, look for the function
    Code (csharp):
    1.  
    2. -(void)agonDidHide
    3.  
    after UnityPause(false); add this
    Code (csharp):
    1. [NSTimer scheduledTimerWithTimeInterval: 5.0 target: self selector: @selector(showAdView) userInfo: nil repeats: NO];
    2.  
    Thats it. When an Agon blade is dimissed, after 5 seconds the Ad will return. Thanks again for the help!
     
  48. rawrman2381238

    rawrman2381238

    Joined:
    Jan 28, 2008
    Posts:
    122
    Hi Martin...

    I followed your steps, but I get this in the console when I build with 3.0:
    and when I try to build with 3.1 I get this error i get a g++ error......
     
  49. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Did you add the whole Admob folder to your project with the 2 lib files and 2 header files?
     
  50. rawrman2381238

    rawrman2381238

    Joined:
    Jan 28, 2008
    Posts:
    122
    I added the two headers and the one lib.. I did not add the simulator one.