HomeCoding TutorialsHow to Integrate Admob Banner Ads in Unity

How to Integrate Admob Banner Ads in Unity

Google Admob ad is an easy way to generate revenue for your mobile application. Admob shows ad to app users to generate a sustainable amount of revenue to help in the growth of your business.

We Will Learn How to Integrate Admob Banner Ads in Unity:

1. Download the Google Mobile Ad package from the below URL. It is also know as Mobile Ads Unity plugin

2. Import the package which you have downloaded from the above URL.To Import the package, Click on Assets -> Import-Package -> Custom Package

Admob Banner Ads in Unity 1
How to Integrate Admob Banner Ads in Unity 8

3. Click on the import button to import all the files.

Admob Banner Ads in Unity 2
How to Integrate Admob Banner Ads in Unity 9

4. To make sure the Google Ad Mob package is imported correctly. Click on Asset -> Play Services Resolver -> Android Resolver -> Force Resolve.

Must Read: Google Login and Registration for Android Using Firebase

5. Resolution Succeeded Message will be shown if package is imported correctly.

Admob Banner Ads in Unity 3
How to Integrate Admob Banner Ads in Unity 10

6. Now, Set your AdMob ID. Check below images to how to set your AdMob ID
Click Assets -> Google Mobile Ads -> Setting.

Admob Banner Ads in Unity 4
How to Integrate Admob Banner Ads in Unity 11

7. Create a new script named BannerAdDemo by clicking on Create -> C# Script

Admob Banner Ads in Unity 5
How to Integrate Admob Banner Ads in Unity 12

8. Create a Gameobject named BannerAdDemo in a Scene and attach BannerAdDemo Script to it

Admob Banner Ads in Unity 6
How to Integrate Admob Banner Ads in Unity 13

Now add the below code in BannerAdDemo script

using GoogleMobileAds.Api;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BannerAdDemo : MonoBehaviour
{
    private BannerView bannerView;
    [SerializeField] private string appId= "ca-app-pub-3940256099942544~3347511713";
    [SerializeField] private string bannerAdId = "ca-app-pub-3940256099942544/6300978111";

    public void Start()
    {


        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

        this.RequestBanner();
    }

    private void RequestBanner()
    {

        string adUnitId = bannerAdId;
        // Create a 320x50 banner at the top of the screen.
        this.bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();

        // Load the banner with the request.
        this.bannerView.LoadAd(request);
    }
}

BannerAdDemo script is responsible for showing the banner in your mobile application. In start method of unity we are initializing the mobile ads with appId. In RequestBanner method, We are creating the empty request and loading the banner with it.

9. Create APK and open it in your mobile phone and you will see test banner ad on the top part of the screen.

Admob Banner Ads in Unity 7

NOTE: Change the app Id and banner Id to your production IDs when you are uploading APK on the production. Use Test IDs only when you are doing testing of ads.

Add AndroiHire to your Google News feed.
Dhaval
Dhavalhttps://www.androidhire.com
Dhaval is a consummate tech enthusiast with a penchant for software development and game creation. His technical prowess is reflected not only in his innovative projects but also in the insightful articles he pens. Beyond the world of technology, Dhaval immerses himself in the world of literature, indulging in novels that captivate his imagination during his leisure hours.

Related Articles