Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Google Mobile Ads SDK

Discussion in 'Android' started by Ceffa93, Feb 27, 2014.

  1. Ceffa93

    Ceffa93

    Joined:
    Dec 2, 2013
    Posts:
    46
    Hi everyone,

    i used to develop on android with unity and i was able to integrate the admob sdk in the projects...
    Now i've made a game in unity and i want to integrate it, and that now it is changed into "Google Mobile Ads SDK"...
    i searched online, i don't find anything...no guide...almost nothing, some one could explain to me what i have to do to correctly import it and make it work on unity?
     
  2. peterdeghaim

    peterdeghaim

    Joined:
    Apr 10, 2014
    Posts:
    154
    Wow this is a pretty old post and you've probably solved your situation by now but what you will want to do is check out the official Google Mobile Ads SDK Unity plugin on GitHub at https://github.com/googleads/googleads-mobile-plugins and have a dive through the example scene provided.

    If you're like me and you can't be bothered to open up a scene (lol) here's the script in the scene.
    Code (csharp):
    1. using System;
    2. using UnityEngine;
    3. using GoogleMobileAds;
    4. using GoogleMobileAds.Api;
    5.  
    6. // Example script showing how to invoke the Google Mobile Ads Unity plugin.
    7. public class GoogleMobileAdsDemoScript : MonoBehaviour {
    8.  
    9.     private BannerView bannerView;
    10.  
    11.     void Start()
    12.     {
    13.         #if UNITY_EDITOR
    14.             string adUnitId = "unused";
    15.         #elif UNITY_ANDROID
    16.             string adUnitId = "INSERT_YOUR_ANDROID_AD_UNIT_HERE";
    17.         #elif UNITY_IPHONE
    18.             string adUnitId = "INSERT_YOUR_IOS_AD_UNIT_HERE";
    19.         #else
    20.             string adUnitId = "unexpected_platform";
    21.         #endif
    22.  
    23.         // Create a 320x50 banner at the top of the screen.
    24.         bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
    25.         // Register for ad events.
    26.         bannerView.AdLoaded += HandleAdLoaded;
    27.         bannerView.AdFailedToLoad += HandleAdFailedToLoad;
    28.         bannerView.AdOpened += HandleAdOpened;
    29.         bannerView.AdClosing += HandleAdClosing;
    30.         bannerView.AdClosed += HandleAdClosed;
    31.         bannerView.AdLeftApplication += HandleAdLeftApplication;
    32.     }
    33.  
    34.     void OnGUI() {
    35.         // Puts some basic buttons onto the screen.
    36.         GUI.skin.button.fontSize = (int) (0.05f * Screen.height);
    37.  
    38.         Rect buttonRect = new Rect(0.1f * Screen.width, 0.1f * Screen.height,
    39.                                    0.8f * Screen.width, 0.2f * Screen.height);
    40.         if (GUI.Button(buttonRect, "Request Banner")) {
    41.             RequestBanner();
    42.         }
    43.  
    44.         Rect bannerRect = new Rect(0.1f * Screen.width, 0.4f * Screen.height,
    45.                                    0.8f * Screen.width, 0.2f * Screen.height);
    46.         if (GUI.Button(bannerRect, "Show Banner")) {
    47.             ShowBanner();
    48.         }
    49.  
    50.         Rect interstitialRect = new Rect(0.1f * Screen.width, 0.7f * Screen.height,
    51.                                          0.8f * Screen.width, 0.2f * Screen.height);
    52.         if (GUI.Button(interstitialRect, "Hide Banner")) {
    53.             HideBanner();
    54.         }
    55.     }
    56.  
    57.     void RequestBanner() {
    58.         // Request a banner ad, with optional custom ad targeting.
    59.         AdRequest request = new AdRequest.Builder()
    60.             .AddTestDevice(AdRequest.TestDeviceSimulator)
    61.             .AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
    62.             .AddKeyword("game")
    63.             .SetGender(Gender.Male)
    64.             .SetBirthday(new DateTime(1985, 1, 1))
    65.             .TagForChildDirectedTreatment(false)
    66.             .AddExtra("color_bg", "9B30FF")
    67.             .Build();
    68.         bannerView.LoadAd(request);
    69.     }
    70.  
    71.     void ShowBanner() {
    72.         bannerView.Show();
    73.     }
    74.  
    75.     void HideBanner() {
    76.         bannerView.Hide();
    77.     }
    78.  
    79.     #region Banner callback handlers
    80.  
    81.     public void HandleAdLoaded()
    82.     {
    83.         print("HandleAdLoaded event received.");
    84.     }
    85.  
    86.     public void HandleAdFailedToLoad(string message)
    87.     {
    88.         print("HandleFailedToReceiveAd event received with message: " + message);
    89.     }
    90.  
    91.     public void HandleAdOpened()
    92.     {
    93.         print("HandleAdOpened event received");
    94.     }
    95.  
    96.     void HandleAdClosing ()
    97.     {
    98.         print("HandleAdClosing event received");
    99.     }
    100.  
    101.     public void HandleAdClosed()
    102.     {
    103.         print("HandleAdClosed event received");
    104.     }
    105.  
    106.     public void HandleAdLeftApplication()
    107.     {
    108.         print("HandleAdLeftApplication event received");
    109.     }
    110.  
    111.     #endregion
    112. }
    I haven't implemented this into my game (yet) but it seems pretty straight forward if you're used to C# I guess...

    Good luck!

    Also, if this complication has already been solved for you, how did you go about implementing ads in your game?
     
  3. coolbird22

    coolbird22

    Joined:
    Mar 20, 2014
    Posts:
    38
    How do I hide a banner in Scene02, that was made visible in Scene01 ? How do I control the visibility of the banner in a different scene ?
     
  4. wuym67123

    wuym67123

    Joined:
    Jan 3, 2017
    Posts:
    11
    Unity Admob Plugin Features
    Platforms supported in one plugin :

    • Android, via SDK v7.8 (part of Google Play service platform)
    • iOS, via SDK v7.6.0
    • Support all native events
    • AdRequest targeting methods,such as children target,test mode
    • Not need change Android package name
    • Very simple API
    Ad Types:

    • Banner(All Banner Type and Custom banner sizes)
    • Interstitial (text, picture, video)
    • Rewarded Video
    • Native Express Banner
    Downloads Admob Unity Plugin
    AdmobPluginRes/GoogleMobileAds.framework and admob_unity_plugin.unitypackage is reqired
    Download those files from Admob Unity3d Plugin Project Home https://github.com/unity-plugins/Unity-Admob
    or Download all the Unity admob plugin project https://github.com/unity-plugins/Unity-Admob/archive/master.zip

    Installation Admob Unity
    1. Open your project in the Unity editor.
    2. Navigate to Assets -> Import Package -> Custom Package.
    3. Select the AdmobUnityPlugin.unitypackage file.
    4. Import all of the files for the plugins by selecting Import. Make sure to check for any conflicts with files.
    Unity Plugin Wiki and Documentation
    Quick Start
    1.Init Admob Unity Plugin
    Create A C# script ,drag the script to a object on scene , add the follow code in the script file

    using admob;
    Admob.Instance().initAdmob("admob banner id", "admob interstitial id");//admob id with format ca-app-pub-279xxxxxxxx/xxxxxxxx


    2.Add Admob Banner in Unity App
    Here is the minimal code needed to show admob banner.

    Admob.Instance().showBannerRelative(AdSize.Banner, AdPosition.BOTTOM_CENTER, 0);


    The AdPosition class specifies where to place the banner. AdSize specifies witch size banner to show

    3.Remove Banner
    By default, banners are visible. To temporarily hide a banner, call:

    Admob.Instance().removeBanner();

    4.How to integrate Interstitial into Unity 3d app?
    Here is the minimal code to create an interstitial.

    Admob.Instance().loadInterstitial();

    Unlike banners, interstitials need to be explicitly shown. At an appropriate stopping point in your app, check that the interstitail is ready before showing it:

    if (Admob.Instance().isInterstitialReady()) {
    Admob.Instance().showInterstitial();
    }

    5.Custom Admob Banner Ad Sizes
    In addition to constants on AdSize, you can also create a custom size:

    //Create a 250x250 banner.
    AdSize adSize = new AdSize(250, 250);
    Admob.Instance().showBannerAbsolute(adSize,0,30);

    6.Admob test Ads and children app
    If you want to test the ads or the your app with children target,you can set with admob unity plugin easy

    Admob.Instance().setTesting(true);
    Admob.Instance().setForChildren(true);
    string[] keywords = { "game","crash","male game"};
    Admob.Instance().setKeywords(keywords);

    7.Ad Events
    Both Banner and Interstitial contain the same ad events that you can register for. Here we'll demonstrate setting ad events on a interstitial,and show interstitial when load success:

    Admob.Instance().interstitialEventHandler += onInterstitialEvent;
    void onInterstitialEvent(string eventName, string msg)
    {
    Debug.Log("handler onAdmobEvent---" + eventName + " " + msg);
    if (eventName == AdmobEvent.onAdLoaded)
    {
    Admob.Instance().showInterstitial();
    }
    }

    You only need to register for the events you care about.

    8.How to integrate Admob Rewarded Video to Unity3d app?
    Here is the minimal code to create an admob video.

    Admob.Instance().loadRewardedVideo("ca-app-pub-312xxxxxxxxxxxx/xxxxxxxx");

    Simular with interstitial,video need to be explicitly shown at an appropriate stopping point in your app, check that the video is ready before showing it:

    if (Admob.Instance().isRewardedVideoReady()) {
    Admob.Instance().showRewardedVideo();
    }

    9.Show Admob Native Express Ad in IOS and Android App
    Here is the minimal code needed to show admob banner.

    Admob.Instance().showNativeBannerRelative(new AdSize(360,100), AdPosition.BOTTOM_CENTER, 0,"ca-app-pub-3940256099942544/2562852117");


    Unity Admob Demo Usage
    1. import AdmobUnityPlugin.unitypackage to your Unity project
    2. copy admobdemo.cs from AdmobPluginRes to your unity project/assets dic
    3. attach admobdemo.cs to the main camera
    4. edit admob id in admobdemo.cs
    5. build and run this in your device
    Important Tips
    1. Add GoogleMobileAds.framework. to Xcode Project
    2. Add the following framework to Xcode project
    AdSupport.framework,EventKit.framework,EventKitUI.framework,CoreTelephony.framework,StoreKit.framework,MessageUI.framework

    1. Attach admob to Object on scene,init admob before call admob fun
     
  5. immacul8_apps

    immacul8_apps

    Joined:
    Apr 4, 2015
    Posts:
    9
    wuym67123 - I Have tried implementing Admob in unity for iOS build project as you have mentioned above and cannot get ads to display? I have been able to use a different method to get ads working on Android build project but can for the life of me get ads to work on iOS project.

    do you have a working copy of your code for iOS build project??

    Thank You
     
  6. dianadorinamita

    dianadorinamita

    Joined:
    Jan 21, 2016
    Posts:
    25
    how to remove some permission from unity when build andorid ? I removed all permission and leave only 2 from androidmanifest but when upload on google playstore it show more permission, where can i find this to disable ?
     
  7. saadali211

    saadali211

    Joined:
    Feb 17, 2017
    Posts:
    19
    watch this video tutorial to fix problems (its in hindi language)

     
  8. unity_yop2uEWM-Dih_g

    unity_yop2uEWM-Dih_g

    Joined:
    Nov 18, 2017
    Posts:
    2
    You can get a stable income with a guaranteed minimum ECPM, regular payments, and automated access to vast media sources via a simple Alphagravel SDK
    Try This Mobile Advertising SDK to Maximize App Revenue.
    Our Alpha-copTM algorithm ensures that non-performing networks are black-listed and removed in real-time.