iAd discussion question

Discussion in 'iOS Development' started by DexRobinson, Feb 27, 2012.

  1. DexRobinson

    DexRobinson

    Member

    Joined:
    Jul 26, 2011
    Messages:
    569
    I am looking into some of the iAd functionality that Unity provides and I was wondering if this is all I have to call in order to have iAd into my game. I know that I have to setup the iAd in the apple development section, but is this all I have to call in Unity to display it?

    Code (csharp):
    1.  
    2. private var banner : ADBannerView = null;
    3.    
    4. function ShowBanner() {
    5.     while (!banner.loaded  banner.error == null)
    6.         yield;
    7.        
    8.     if (banner.error == null)
    9.         banner.Show();
    10.     else banner = null;
    11. }
    12.  
    13. function OnGUI() {
    14.     GUI.enabled = (banner == null ? true : false);
    15.    
    16.     if (GUILayout.Button("Show Banner")) {
    17.         banner = new ADBannerView();
    18.         banner.autoSize = true;
    19.         banner.autoPosition = ADPosition.Bottom;
    20.         StartCoroutine(ShowBanner());
    21.     }
    22. }
    23.  
    I wouldn't use a button to display the ad but is this the only call I need to make?
  2. _Daniel_

    _Daniel_

    Member

    Joined:
    Feb 28, 2007
    Messages:
    1,820
    It's pretty simple to create an iAd. I haven't tested the specific script you posted, but just glancing over it, it appears like it should work fine. Why don't you test it yourself?
  3. DexRobinson

    DexRobinson

    Member

    Joined:
    Jul 26, 2011
    Messages:
    569
    I would but I don't have a mac until the weekend. I have to borrow a friends. I just am trying to get all my code figured out before then.
  4. technicat

    technicat

    Member

    Joined:
    Nov 22, 2006
    Messages:
    1,067
    That script is from the Script Reference for AdBannerView so I'd expect it to work. I have a button-less version listed here - http://drupal.technicat.com/games/unity/ios/iads.html - but it seems to me that it doesn't refresh the ad after the first one is displayed, so I've filed a bug report.
  5. DexRobinson

    DexRobinson

    Member

    Joined:
    Jul 26, 2011
    Messages:
    569
    Thanks technicat. Yeah I haven't done much work with ads in my game so I just am unsure on how they actually get displayed. Will this show up in my development version? Or does it only work in published versions?
  6. technicat

    technicat

    Member

    Joined:
    Nov 22, 2006
    Messages:
    1,067
    I always see a test ad displayed until the app is approved by Apple.
  7. nshack31

    nshack31

    New Member

    Joined:
    Nov 4, 2011
    Messages:
    95
    just checking this script will only compile on a mac?
  8. Le_nain

    Le_nain

    New Member

    Joined:
    Jun 20, 2011
    Messages:
    47
    @technicat: Does it work for you on iPhone? Because I can display the test ad on my iPad, but there's nothing showing on my iPhone, eventhough my Debug Logs say the opposite...
  9. nshack31

    nshack31

    New Member

    Joined:
    Nov 4, 2011
    Messages:
    95
    iphone works fine, although the test ad eventually turns into a plain white banner and im not sure if this is normal behaviour
  10. Le_nain

    Le_nain

    New Member

    Joined:
    Jun 20, 2011
    Messages:
    47
    Meh I'm gonna check some more then, my banner may be obscured by an error of placement then.
  11. technicat

    technicat

    Member

    Joined:
    Nov 22, 2006
    Messages:
    1,067
    @Le_nain It does show up for me on my 4th-gen iPod touch (iOS 5) but will disappear if I suspend and resume - I assume it's the same non-refreshing issue that produces the white banner. @nshack31 re your earlier question, the doc only mentions an implementation of AdBannerView for iAds (and thus, only on iOS) - I would hope the code compiles and does nothing for non-IOS targets but if not, you can just wrap an #if UNITY_IPHONE around it.
  12. Le_nain

    Le_nain

    New Member

    Joined:
    Jun 20, 2011
    Messages:
    47
    Found it: the issue relies in the retina display.
    Let me explain in case someone has the same issue someday:

    - First, I'm not using like you guys the autposition/autosize features, because as within my app I manually handle the screen rotations (not using Unity's autorotation because I want to change some settings on my camera/GUI when the screen rotates), it wouldn't autoposition nor autosize my banners.

    - So I was manually positionning my banners, but I was doing it the way it should be handled by Unity: using Screen.height - the Y size of my banner. Here's lies the problem. in fact, Unity just transfers those parameters to native and let it handles the banners. THOUGH, one should know that Apple's SDK handles retina and non-retina displays the exact same way, so even if you're on a retina display device you should use the non-retina display screen sizes to place your banner. (e.g. for iPhone 3 AND iPhone 4, the banner should be placed vertically at 320 - bannerYSize for landscape and 480 - bannerYSize for portrait if you want your banner at the bottom)
  13. synapsemassage

    synapsemassage

    New Member

    Joined:
    Jul 27, 2009
    Messages:
    284
    You guys should also keep in mind that fillrate of real ad request is only around 15% depending on the country. So for most countries you will never see an iAd! Only 7 countries are supported. Make sure to support other ad networks if iAd fails to deliver!
  14. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Messages:
    10,440
    it's called adwhirl folks and the kindly mr prime31 with his top hat and white gloves is ready to dazzle you
  15. synapsemassage

    synapsemassage

    New Member

    Joined:
    Jul 27, 2009
    Messages:
    284
    Actually every ad network is getting on the mediation layer train, means trying something similar like adwhirl. Prime has also similar solutions to adwhirl. There is also some free stuff in the asset store. I decided to use Prime's MoPub. MoPub gives you the possibility to use other ad networks via http requests so that you don't need to integrate SDKs other than MoPubs. Means less dependencies to third party software and you can easily change the networks via their web interface on the fly.
  16. nshack31

    nshack31

    New Member

    Joined:
    Nov 4, 2011
    Messages:
    95
    just to confirm this... my app went live but after the first advert it will not refresh correctly. Problem with unity.
  17. masterchafe

    masterchafe

    New Member

    Joined:
    Oct 2, 2011
    Messages:
    46
    yeah, I'm using the same code and the ads disappear after the first one.. could it just be the buttonless code that Technicat kindly provided is missing something?
  18. technicat

    technicat

    Member

    Joined:
    Nov 22, 2006
    Messages:
    1,067
    You can compare and try the original code I copied and modified from the AdBannerView documentation in the Scripting REference.
  19. masterchafe

    masterchafe

    New Member

    Joined:
    Oct 2, 2011
    Messages:
    46
    Oh I didnt mean you had introduced a bug with your rewrite or anything ;)
    I should have said: Could there be something that could be added to the buttonless code that wasnt in the documentation to begin with?
    Last edited: Apr 11, 2012
  20. technicat

    technicat

    Member

    Joined:
    Nov 22, 2006
    Messages:
    1,067
    That's a good point (and I just remembered the doc example is conveniently in the original post here). Maybe we have to periodically check AdBannerView properties like error/loaded/visible and call Show again, but that seems pretty complicated (at least compared to how the Prime31 plugin is used). I haven't heard anything back from my bug report, either.
  21. masterchafe

    masterchafe

    New Member

    Joined:
    Oct 2, 2011
    Messages:
    46
    technicat, it looks like it was a problem inherited from the unity script reference as I just checked through it a little more thoroughly and it seems there was a problem with the logic in the While loop of ShowBanner(). This is the code I'm using now:

    Code (csharp):
    1. function Start () {
    2.   StartCoroutine(ShowBanner());
    3. }
    4.  
    5. function ShowBanner() {
    6.   var banner:ADBannerView;
    7.   banner = new ADBannerView();
    8.   banner.autoSize = true;
    9.   banner.autoPosition = ADPosition.Top;
    10.   while (!banner.loaded  banner.error != null) //  banner.error == null)
    11.   yield;
    12.  
    13.   if (banner.error == null)
    14.     banner.Show();
    15.   else banner = null;
    16. }
    after changing "while (!banner.loaded banner.error==null" to "while (!banner.loaded banner.error!=null" the Test Ad hasn't disappeared for the last 10 minutes! :cool:
  22. technicat

    technicat

    Member

    Joined:
    Nov 22, 2006
    Messages:
    1,067
    The original code looks correct to me. It's yielding until the ad is loaded or there is an error, and then if there's no error it shows the ad. With your change, assuming it starts out with no error, it doesn't wait for the ad to be loaded but instead immediately calls Show on it. But hey, if it works, it works, and it's a nice tidbit to provide in a bug report (if the sample code is incorrect, then it's a documentation bug)
  23. masterchafe

    masterchafe

    New Member

    Joined:
    Oct 2, 2011
    Messages:
    46
    Isn't it actually that it's yielding until the ad has loaded and there is no error? (.error == null)
  24. technicat

    technicat

    Member

    Joined:
    Nov 22, 2006
    Messages:
    1,067
    No, the negation of

    Code (csharp):
    1. (!banner.loaded  banner.error == null)
    is

    Code (csharp):
    1. (banner.loaded || banner.error != null)
    So the while loop ends as soon as banner.loaded is true OR banner.error is not null. In other words, it will stop yielding if the banner is loaded, but it will also stop yielding if there is an error. Which makes sense.
  25. masterchafe

    masterchafe

    New Member

    Joined:
    Oct 2, 2011
    Messages:
    46
    I feel like I'm going round in circles here! :D

    you seem to have said that "(banner.error==null)" does not mean there is no error, I'd say it does

    anyway, I'm currently using:

    Code (csharp):
    1. while (!banner.loaded || banner.error != null)
    2.   yield;
    so while the banner hasn't loaded or there is an error, yield.

    which to me reads correctly, and seems to be working with testads. If you read that as not being correct, please feel free to correct me, I would like to know if I am indeed missing the point and going the wrong way about this! :)
  26. technicat

    technicat

    Member

    Joined:
    Nov 22, 2006
    Messages:
    1,067
    Where did I say that?
    Last edited: Apr 11, 2012
  27. masterchafe

    masterchafe

    New Member

    Joined:
    Oct 2, 2011
    Messages:
    46
    Your previous post, when you opened with "No" after my quote, was where I thought you had said that. Apparently I am misunderstanding everything you say?
  28. technicat

    technicat

    Member

    Joined:
    Nov 22, 2006
    Messages:
    1,067
    I said the original Unity example while loop terminates if banner.error != null, in other words if there is an error (and also terminates if the banner is loaded). It seems to me that's the opposite of what you said I said. But if you still think that's what I said, let's forget it - it's not worth taking up more time or forum space.
  29. masterchafe

    masterchafe

    New Member

    Joined:
    Oct 2, 2011
    Messages:
    46
    Sorry if it seemed I was only trying to make a "you're wrong!" argument, certainly wasn't my intention, I just want to have this code working. I was only trying to understand, I feel like I'm just guessing what to do with the code for this now.

    I do think the unity example is wrong and does seem, with the changes I've made here, that iAd test banners are working. I guess I just have to wait a few weeks till I've submitted an app with it to see if it actually does work for sure.

    btw, your twitter link in your sig is incorrect.
    Last edited: Apr 12, 2012
  30. masterchafe

    masterchafe

    New Member

    Joined:
    Oct 2, 2011
    Messages:
    46
    ok, I really don't know anymore, they're disappearing again now.. I think I'm resigned to grabbing a prime31 plugin now :)
  31. masterchafe

    masterchafe

    New Member

    Joined:
    Oct 2, 2011
    Messages:
    46
    I decided to have one more crack at doing something positive for this community. Previously I've only came here for help and it'd be nice if I've now been able to give someone else help. All I've done is started with Technicat's buttonless code, moved the banner var out of ShowBanner, and added a check to see if banner is null in an Update function which, if true, starts the coroutine ShowBanner().

    Code (csharp):
    1.  
    2. private var banner : ADBannerView = null;
    3.  
    4. function Start () {
    5.     DontDestroyOnLoad(this);
    6.     //StartCoroutine(ShowBanner());
    7. }
    8.  
    9. function ShowBanner() {
    10.   //var banner:ADBannerView;
    11.     banner = new ADBannerView();
    12.     banner.autoSize = true;
    13.     banner.autoPosition = ADPosition.Bottom;
    14.  
    15.     //if(!banner.visible) print("iAdBanner : banner.visible = False");
    16.  
    17.     while (!banner.loaded  banner.error == null)
    18.     yield;
    19.  
    20.     if (banner.error == null)
    21.         banner.Show();
    22.     else banner = null;
    23. }
    24.  
    25. function Update(){
    26.  
    27.     if(banner==null) StartCoroutine(ShowBanner());
    28.  
    29. }
    30.  
    I've had it running successfully for an hour solid now. I also tested disconnecting from the internet while the app was running and when internet was returned to the device, the iAd began working again.

    Hopefully this is all that will be needed for iAds to work out of the box in Unity. :cool:

    Is there any reason starting a new coroutine, every time banner goes null, would be a bad idea?
  32. technicat

    technicat

    Member

    Joined:
    Nov 22, 2006
    Messages:
    1,067
    masterchafe, in your new code banner is never set to null after the first call to Show. However, looking at your code I just realized I may have screwed up something in mine - unlike the Unity doc example and your latest code, I declare the banner variable local to the coroutine, so it could be garbage-collected any time after the coroutine is exited and maybe that's why it's not refreshing. I'm flip-flopping between Unity 3.4 and 3.5 right now, so I'll try moving that variable declaration back out next time I'm back on 3.5.
  33. masterchafe

    masterchafe

    New Member

    Joined:
    Oct 2, 2011
    Messages:
    46
    hey cool. I'm currently backing all my stuff up to make the move to 3.5.1 ( just incase :D ), hoping the iAd stuff will still work in 3.5.1 too although I think I am still going to give prime's Adwhirl or similar plugin a try before I submit anything to Apple anyway
  34. nshack31

    nshack31

    New Member

    Joined:
    Nov 4, 2011
    Messages:
    95
    this works really well! but do you sometimes find that the banner doesnt appear? if so is this an iAd network problem or problem with unity?
  35. masterchafe

    masterchafe

    New Member

    Joined:
    Oct 2, 2011
    Messages:
    46
    I have noticed what seemed to be a load time that I assumed was a testAd feature. This is my first play with iAd so I'm not entirely sure if it's normal functionality either! :) I'm currently waiting on a review of my second submition after the first was rejected due to lack of iOS specific UI and functionality
  36. nshack31

    nshack31

    New Member

    Joined:
    Nov 4, 2011
    Messages:
    95
    i've been running this for a while now and sadly it is unreliable. Often the adverts don't initiate :( hopefully unity will get something sorted soon
  37. masterchafe

    masterchafe

    New Member

    Joined:
    Oct 2, 2011
    Messages:
    46
    oh that's a shame, hopefully Unity will get around to it sometime :)
  38. nshack31

    nshack31

    New Member

    Joined:
    Nov 4, 2011
    Messages:
    95
    on reflection its hard to know whether this a code problem, or an iAd fill rate problem as I notice that ads are not always 'filled' despite being requested.


    ... if this is the case, would it be possible to run admob when the iAd is not filled? if so at which point in the code should admob be initiated?! thanks
    Last edited: May 3, 2012
  39. nshack31

    nshack31

    New Member

    Joined:
    Nov 4, 2011
    Messages:
    95
    not sure if this is correct (currently untested) but for anyone who wants to try and load admob while iAd isnt showing a banner, i've added to masterchafe's code:

    Code (csharp):
    1. #if UNITY_IPHONE
    2.  
    3. private var banner : ADBannerView = null;
    4.  
    5. public var publisherId : String = "id here";// IOS ADMOB
    6.  
    7. var admobshowing : boolean;
    8.  
    9. function Start () {
    10.  
    11.     DontDestroyOnLoad(this);
    12.     AdMobBinding.init( publisherId );//IOS ADMOB
    13.  
    14.  
    15. }
    16.  
    17. function ShowBanner() {
    18.     banner = new ADBannerView();
    19.     banner.autoSize = true;
    20.     banner.autoPosition = ADPosition.Bottom;
    21.  
    22.  
    23.     while (!banner.loaded  banner.error == null)
    24.  
    25.     yield;
    26.  
    27.  
    28.     if (banner.error == null){
    29.         banner.Show();
    30.         AdMobBinding.destroyBanner();
    31.         admobshowing = false;}// IOS ADMOB
    32.     else {
    33.         banner = null;
    34.        
    35.         if (!admobshowing){
    36.         AdMobBinding.createBanner( AdMobBannerType.SmartBannerPortrait, 0, 1 );
    37.         admobshowing = true;
    38.         }
    39.     }
    40.  
    41. }
    42.  
    43. function Update(){
    44.     if(banner==null) StartCoroutine(ShowBanner());
    45. }
    46.  
    47. #endif
    obviously it requires Prime31's plugin.
    Last edited: May 5, 2012
  40. laserlars

    laserlars

    Member

    Joined:
    Nov 17, 2011
    Messages:
    251
    Hi guys,

    I got iAd and Admob configured in Adwhirl. I have set iAd allocation to 70% and Admob to 30%. But what happens in countries with no iAd support? Will I lose the 70% of potential displayed banners or will Adwhirl try to show an iAd and (quickly) show an Admob banner instead when failing to do so?

    This is my first attempt to implement ads, so excuse the basic question. Better safe than sorry :)
  41. technicat

    technicat

    Member

    Joined:
    Nov 22, 2006
    Messages:
    1,067
    I cleaned up my code so it's quite a bit simpler (once you remove the GUI, you don't really need the separate coroutine)

    Code (csharp):
    1. // don't make this a local variable, or else it'll get garbage collected
    2. private var banner:ADBannerView;
    3.  
    4. function Start () {
    5.     GameObject.DontDestroyOnLoad(this); // keep ad alive if we load a new scene
    6.     banner = new ADBannerView();
    7.     banner.autoSize = true;
    8.     banner.autoPosition = ADPosition.Bottom;
    9.     while (!banner.loaded  banner.error == null)
    10.         yield;
    11.     if (banner.error == null)
    12.         banner.Show();
    13.     else banner = null;
    14. }
  42. Ethan

    Ethan

    Member

    Joined:
    Jan 2, 2008
    Messages:
    493
    So do you recommend the prime31 iAd plugin or is it just as good as this piece of code?

    I think you mentioned you are prefering the prime31 plugin somewhere.
  43. technicat

    technicat

    Member

    Joined:
    Nov 22, 2006
    Messages:
    1,067
    I have started to switch everything over to AdBannerView, except for my apps that are still stuck on Unity 3.4, but think the prime31 plugin actually works better, e.g. with AdBannerView I notice I often see a blank white banner when I resume an app before it fills in with an ad, whereas it seems to me the prime31 version destroys and recreates the banner more cleanly. I suggest trying AdBannerView and if it doesn't look right or if it looks like it's going to take more than $20 of your time to get it working then try the prime31 plugin, and you'll have the benefit of his support thread on this forum.

    By the way, I was having trouble with my AdBannerView code running properly when it switches scenes, so I modified the DontDestroyOnLoad line (but actually I'm only using in one-scene apps for now)

    Code (csharp):
    1.  
    2. // don't make this a local variable, or else it'll get garbage collected
    3.  
    4. private var banner:ADBannerView;
    5.  
    6.  
    7.  
    8. function Start () {
    9.  
    10.     GameObject.DontDestroyOnLoad(gameObject); // keep ad alive if we load a new scene
    11.  
    12.     banner = new ADBannerView();
    13.  
    14.     banner.autoSize = true;
    15.  
    16.     banner.autoPosition = ADPosition.Bottom;
    17.  
    18.     while (!banner.loaded  banner.error == null)
    19.  
    20.         yield;
    21.  
    22.     if (banner.error == null)
    23.  
    24.         banner.Show();
    25.  
    26.     else banner = null;
    27.  
    28. }
    29.  
    Last edited: Jun 19, 2012
  44. reptilebeats

    reptilebeats

    Member

    Joined:
    Apr 28, 2012
    Messages:
    128
    nice code im a new indie developer and have been working on my first release for the last 8 months mainly creating objects as i had none, anyways i was going to buy prime but broke as a joke at the moment. has anyone tested this code out yet i see the test ad works fine but does it display ads after release.

    i will be releasing a sort of demo on iphone for my game but really its just a way to make sure ads and what not work before i release my game
  45. toto2003

    toto2003

    Member

    Joined:
    Sep 22, 2010
    Messages:
    261
    hello
    my head start to go crazy, spend 2 hours on it with no success, i m using unity 4.2 and xcode 4.6.2
    i try the code above, but i don t have any test ad window pop up, i ad the iad framework in xcode, is there anything more to do to show up the test ad?
    any hint would be appreciated
    thanks
  46. fdsfsdf

    fdsfsdf

    New Member

    Joined:
    Apr 23, 2013
    Messages:
    5
  47. toto2003

    toto2003

    Member

    Joined:
    Sep 22, 2010
    Messages:
    261
    thanks fdsfsdf
    seem s like i m not the only one who got that problem... might check prime 31 plug in as it should work right of the box.
  48. zombie_psy

    zombie_psy

    Member

    Joined:
    Oct 9, 2012
    Messages:
    51
    The advertisements for the PAY plugins are off-topic, is soliciting spam advertising, and should be removed by the moderator.

    To advertisers: Please read the title of this post and either contribute to the question or nothing at all.
    Last edited: Apr 5, 2014
  49. SouthernCoder69

    SouthernCoder69

    Member

    Joined:
    Oct 23, 2013
    Messages:
    74
    No one is advertising plugins anymore than people talking about Unity is advertising for unity. This WAS a thread (from YEARS ago) that discussed iADS and someone mentioned that there exists asset to take care of this for you. You should really get over this vendetta against people trying to make a living...
  50. technicat

    technicat

    Member

    Joined:
    Nov 22, 2006
    Messages:
    1,067
    My iAd script has been rewritten for the newer (and simpler) API in Unity 4.3 and also to allow easy switching between between the Unity and prime31 implementations (one of these days I will add u3dxt) and also for AdMob on Android. It's on github.

    https://github.com/technicat/FuguMobile/blob/master/AdBanner.cs

    Also, that whitespace bug I mentioned has been fixed by Unity in one of the 4.x releases.