- Home /
Admob ads work but are on all scenes
I got a i guess ad package? from here: https://github.com/googleads/googleads-mobile-plugins/tree/master/unity/android . It worked got ads displaying and making money on Admob. The problem is that ads are displaying on all scenes (which I don't want) even though I only put the AdBanner.prefab in the scene Main. So is there a hid script or something I need to add? I'm kinda a noob so its probably a easy fix. If you want you can download the beta version of my game here: https://play.google.com/store/apps/details?id=com.joh.brickde . The screen shots are old so don't believe them.
Hello, I have no idea about your problem, so sorry. But I am having some problem with showing admob's ad. It doesn't show anything at all, and I don't know if I did something wrong. So I am very happy if you can spend a little time to help me. Thank you for reading. :)
You have opened the same question here: http://answers.unity3d.com/questions/667599/admob-ads-display-on-all-scenes.html
Please close or delete this one.
Answer by cboxgo · Dec 10, 2014 at 09:14 PM
Once you setup the plugin you should be able to use one of these two methods:
Method 1 - Add this to GoogleMobileAdsDemoScript and put GoogleMobileAdsDemoScript only on levels you want ads to appear on:
void Update() {
//Destroy when leaving the level
if (Input.GetKeyDown(KeyCode.Escape)) {
bannerView.Hide ();
bannerView.Destroy();
}
}
Method 2 - Modify GoogleMobileAdsDemoScript with this minor change:
public static BannerView bannerView;
Then add this to your levels to control hiding and showing ads:
void OnGUI() {
try {
GoogleMobileAdsDemoScript.bannerView.Hide (); //if you want to hide it
//or
GoogleMobileAdsDemoScript.bannerView.Show (); //if you want to show it
}
catch(Exception e) {
Debug.Log(e.ToString());
}
}
The GoogleMobileAdsDemoScript would only be attached to the main scene in Method 2 as it would only be loaded at the beginning and would not be destroyed at any time.
Answer by John Sartain · Mar 12, 2014 at 04:53 AM
what would i change here using UnityEngine; using System.Runtime.InteropServices;
public class AdMobManager : MonoBehaviour { public enum Position { TOP, BOTTOM, TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT }
private static AdMobManager mInstance = null;
[SerializeField]
private string iPhoneAdmobID;
[SerializeField]
private string iPadAdmobID;
[SerializeField]
private string androidAdmobID;
[SerializeField]
private Position position;
[SerializeField]
private string[] iosTestDeviceIDs;
[SerializeField]
private string[] androidTestDeviceIDs;
if UNITY_IPHONE
[DllImport("__Internal")] private static extern void installAdMobIOS_(string admobID, Position position); [DllImport("__Internal")] private static extern void addTestDeviceIDIOS_(string testDeviceID); [DllImport("__Internal")] private static extern void hideAdIOS_(); [DllImport("__Internal")] private static extern void showAdIOS_(); [DllImport("__Internal")] private static extern void refreshAdIOS_(); [DllImport("__Internal")] private static extern void releaseAdMobIOS_(); [DllImport("__Internal")] private static extern bool isIpadAdMob_(); #elif UNITY_ANDROID private AndroidJavaObject adViewController = null; #endif
public static AdMobManager instance
{
get
{
return mInstance;
}
}
public void Awake()
{
if (mInstance == null)
{
mInstance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
public void OnDestroy()
{
if (Application.isEditor) return;
if UNITY_IPHONE
if (mInstance == this)
{
releaseAdMobIOS_();
}
endif
}
public void Start()
{
if (Application.isEditor) return;
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
bool ipad = false;
if UNITY_IPHONE
ipad = isIpadAdMob_();
endif
if (ipad && iPadAdmobID != "")
{
install(iPadAdmobID, position);
}
else
{
install(iPhoneAdmobID, position);
}
}
else if (Application.platform == RuntimePlatform.Android)
{
install(androidAdmobID, position);
}
}
public void OnApplicationPause(bool pause)
{
if (Application.isEditor) return;
if (pause)
{
if UNITY_ANDROID
adViewController.Call("cancelRefreshTimer");
endif
}
else
{
refresh();
}
}
private void install(string admobID, Position position)
{
if UNITY_IPHONE
installAdMobIOS_(admobID, position);
foreach (string device_id in iosTestDeviceIDs)
{
addTestDeviceIDIOS_(device_id);
}
refreshAdIOS_();
elif UNITY_ANDROID
adViewController = new AndroidJavaObject("net.mikinya.admob.AdViewController");
foreach (string device_id in androidTestDeviceIDs)
{
adViewController.Call("addTestDevice", device_id);
}
adViewController.Call("installAdMobForAndroid", admobID, (int)position);
adViewController.Call("refreshAd");
endif
}
public void refresh()
{
if UNITY_IPHONE
refreshAdIOS_();
elif UNITY_ANDROID
adViewController.Call("refreshAd");
endif
}
public void hide()
{
if UNITY_IPHONE
hideAdIOS_();
elif UNITY_ANDROID
adViewController.Call("hideAd");
endif
}
public void show()
{
if UNITY_IPHONE
showAdIOS_();
elif UNITY_ANDROID
adViewController.Call("showAd");
endif
} }
Just remove the line DontDestroyOnLoad(gameObject);
Also don't post answers as comments. There is a comment option for a reason.
Thanks man for all help btw I hate to do this to you but its a diffrent script using UnityEngine; using System.Collections;
public class AdBannerObserver : $$anonymous$$onoBehaviour { private static AdBannerObserver sInstance;
public static void Initialize() {
Initialize(null, null, 0.0f);
}
public static void Initialize(string publisherId, string testDeviceId, float refresh) {
if (sInstance == null) {
// $$anonymous$$ake a game object for observing.
GameObject go = new GameObject("_AdBannerObserver");
go.hideFlags = HideFlags.HideAndDontSave;
DontDestroyOnLoad(go);
// Add and initialize this component.
sInstance = go.AddComponent<AdBannerObserver>();
sInstance.mAd$$anonymous$$obPublisherId = publisherId;
sInstance.mAd$$anonymous$$obTestDeviceId = testDeviceId;
sInstance.mRefreshTime = refresh;
}
}
public string mAd$$anonymous$$obPublisherId;
public string mAd$$anonymous$$obTestDeviceId;
public float mRefreshTime;
IEnumerator Start () {
if UNITY_IPHONE
ADBannerView banner = new ADBannerView();
banner.autoSize = true;
banner.autoPosition = ADPosition.Bottom;
while (true) {
if (banner.error != null) {
Debug.Log("Error: " + banner.error.description);
break;
} else if (banner.loaded) {
banner.Show();
break;
}
yield return null;
}
elif UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaClass plugin = new AndroidJavaClass("jp.radiumsoftware.unityplugin.admob.AdBannerController");
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
while (true) {
plugin.CallStatic("tryCreateBanner", activity, mAd$$anonymous$$obPublisherId, mAd$$anonymous$$obTestDeviceId);
yield return new WaitForSeconds($$anonymous$$athf.$$anonymous$$ax(30.0f, mRefreshTime));
}
else
return null;
endif
} } its still displaing on all scences ive removed so much and still no luck
Remove DontDestroyOnLoad(go);
Just anywhere you see the DontDestroyOnLoad function...remove it.
Answer by Sildaekar · Mar 12, 2014 at 03:02 AM
On line 42 of Assets/Plugins/GoogleMobileAds/GoogleMobileAdsPlugin.cs remove the following line:
DontDestroyOnLoad(this);
Now it will only display on the scenes you place the prefab in
That is not how this works. The script will create a new activity to display the ads. Destroying the prefab that called the activity, will not destroy the activity itself. So you are going to have ads once they are initialized until the game is exited.
Answer by ginesinho · Mar 18, 2015 at 02:58 PM
Make public the Destroy method of the script of the Admob banner and add the next code:
public void Destroy(){
bannerView.Hide();
bannerView.Destroy();
}
After that, call this method when you want to hide the banner:
bannerScript = adMobBunner.GetComponent<AdMobBunnerScript>();
bannerScript.Destroy();
I tried to use only the Hide method inside the Destroy method but it works slower. It's better to put both of them.
Your answer
Follow this Question
Related Questions
Unity will not allow me to export anything, or save any new scenes 1 Answer
Passing Data Through Scenes? 1 Answer
UnitySerializer Delete Saved Games 0 Answers
Creating Scene Chunk in 2d game and Altering Prefab 0 Answers
how to accumulate game objects from the game (runtime) into the current scene? 2 Answers