Coding Tutorials

How to Integrate Admob Banner Ads in Unity

Dhaval
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

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

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.

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

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

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

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.

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.