- Home /
how to show ad at the end like flappy bird? iOS
Hi all,
I've done the whole mob ad deal from here (https://github.com/googleads/googleads-mobile-plugins/tree/master/unity) and I got to the point where I have my ad show up at the beginning of the game and is then hidden when the player touches on the START button on my first scene. I put the script (AdScript) with the RequestBanner, hideBanner and showBanner methods in an empty prefab with the DontDestroyOnLoad(transform.gameObject); on Awake void so it stays on through the 3 scenes of my game. I do this because apparently Google does not like it when an ad is requested more than a few times per session. I thought I could then call the showBanner method from a script in another object with a function event since this function(method) already instantiates a bunch of my game over GUI Textures and GUI Texts (with current and best score):
Public void gameOverStuff(){
...
AdScript adScript = GetComponent<AdScript>();
adScript.showBanner();
}
or
public AdScript adScript;
Public void gameOverStuff()
{
...
adScript.showBanner();
}
But, neither is working. . . I am not getting any errors but I am also not getting the banner ad to show when the gameOverStuff function event is called. Yes, I added the empty with the AdScript into the adScript slot in the editor. I could try destroying and instantiating the empty prefab with my AdScript requesting the banner on awake but that just sounds so dirty.
I hope it makes sense. My head is spinning now.
Thanks!
here is the Adscript:
using System;
using UnityEngine;
using Google$$anonymous$$obileAds;
using Google$$anonymous$$obileAds.Api;
// Example script showing how to invoke the Google $$anonymous$$obile Ads Unity plugin.
public class startAdsScript : $$anonymous$$onoBehaviour
{
private BannerView bannerView;
void Awake()
{
DontDestroyOnLoad(transform.gameObject);
}
void Start()
{
RequestBanner();
}
private void RequestBanner()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "INSERT_ANDROID_BANNER_AD_UNIT_ID_HERE";
#elif UNITY_IPHONE
string adUnitId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the top of the screen.
bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
// Register for ad events.
bannerView.AdLoaded += HandleAdLoaded;
bannerView.AdFailedToLoad += HandleAdFailedToLoad;
bannerView.AdOpened += HandleAdOpened;
bannerView.AdClosing += HandleAdClosing;
bannerView.AdClosed += HandleAdClosed;
bannerView.AdLeftApplication += HandleAdLeftApplication;
// Load a banner ad.
bannerView.LoadAd(createAdRequest());
}
// Returns an ad request with custom ad targeting.
private AdRequest createAdRequest()
{
return new AdRequest.Builder()
.AddTestDevice(AdRequest.TestDeviceSimulator)
.AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
.Add$$anonymous$$eyword("kpop")
.SetGender(Gender.$$anonymous$$ale)
.SetBirthday(new DateTime(1985, 1, 1))
.TagForChildDirectedTreatment(false)
.AddExtra("color_bg", "9B30FF")
.Build();
}
public void hideBanner()
{
bannerView.Hide();
}
public void showBanner()
{
bannerView.Show();
}
#region Banner callback handlers
public void HandleAdLoaded(object sender, EventArgs args)
{
print("HandleAdLoaded event received.");
}
public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("HandleFailedToReceiveAd event received with message: " + args.$$anonymous$$essage);
}
public void HandleAdOpened(object sender, EventArgs args)
{
print("HandleAdOpened event received");
}
void HandleAdClosing(object sender, EventArgs args)
{
print("HandleAdClosing event received");
}
public void HandleAdClosed(object sender, EventArgs args)
{
print("HandleAdClosed event received");
}
public void HandleAdLeftApplication(object sender, EventArgs args)
{
print("HandleAdLeftApplication event received");
}
#endregion
}
In xcode I am getting this: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0). and on this stack overflow entry apararently that means "A null pointer is returned if there are no tokens left to retrieve."
Answer by Reeceg · Jul 18, 2014 at 02:21 AM
You could use a collider at the start that sets showbanner.enabled = true then when you exit it showbanner.enabled = false and do the same at the end
Answer by EmilioDLC · Aug 06, 2014 at 07:56 PM
I got it to work with the .FindWithTag. I don't know why the previous 2 ways don't work for me:
private startAdsScript startadsScript;
GameObject AdsEmpty = GameObject.FindWithTag ("GoogleEmptyTag");
if (AdsEmpty != null)
{
startadsScript = AdsEmpty.GetComponent <startAdsScript>();
}
if (startadsScript == null)
{
Debug.Log ("Cannot find 'startAdsScript' script");
}
startadsScript.showBanner();
do we need this code?
{
return new AdRequest.Builder()
.AddTestDevice(AdRequest.TestDeviceSimulator)
.AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
.Add$$anonymous$$eyword("kpop")
.SetGender(Gender.$$anonymous$$ale)
.SetBirthday(new DateTime(1985, 1, 1))
.TagForChildDirectedTreatment(false)
.AddExtra("color_bg", "9B30FF")
.Build();
}
Your answer
Follow this Question
Related Questions
'GADAdmobExtras.h' file not found 0 Answers
Integrate Admob in iOS and windows phone 0 Answers
AdMob iOS for Unity 4.1 2 Answers
Admob interstitial suddenly disappear after showing 0 Answers
Unity iOS Build Bitcode Issue 1 Answer