unity ads implementation tutorial 2020 code example

Example: unity ads tutorial 2020

using System.Collections;
using UnityEngine;
using UnityEngine.Advertisements;

// how to add a banner ad in unity games
public class BannerAdScript : MonoBehaviour {

    #if UNITY_IOS
    private string gameId = "1486551";
    #elif UNITY_ANDROID
    private string gameId = "1486550";
    #endif
    
    public string placementId = "bannerPlacement";
    public bool testMode = true;

    void Start () {
        // Initialize the SDK if you haven't already done so:
        Advertisement.Initialize (gameId, testMode);
        StartCoroutine (ShowBannerWhenReady ());
      
      // set the position
        Advertisement.Banner.SetPosition (BannerPosition.TOP_CENTER);

    }

    IEnumerator ShowBannerWhenReady () {
        while (!Advertisement.IsReady (placementId)) {
            yield return new WaitForSeconds (0.5f);
        }
        Advertisement.Banner.Show (placementId);
    }
}