Search code examples
c#androidunity-game-engineadsbanner

How to place a ad banner in unity using Unity ads? No plugins download


Im tying to figure out how to place an ad banner in unity but all the topics are about admobs banner in unity. I want to know how to use Unity ads banner.

using System.Collections;
using System.Collections.Generic;

using UnityEngine.Advertisements;

using UnityEngine;

public class UnityAdManager : MonoBehaviour
{

    public string gameId = "gameid";

    public string placementId = "Adbanner";

    public bool testMode = true;

    public static UnityAdManager instance;

    void Awake()
    {
        DontDestroyOnLoad(this.gameObject);
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(this.gameObject);
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        Advertisement.Initialize(gameId, testMode);
        StartCoroutine(ShowBannerWhenReady());
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void ShowAd()
    {
        if (PlayerPrefs.HasKey("Adcount"))
        {
            //number of ads
            if (PlayerPrefs.GetInt("Adcount") == 2)
            {




                if (Advertisement.IsReady("video"))
                {
                    Advertisement.Show("video");
                }


                PlayerPrefs.SetInt("Adcount", 0);

            }
            else
            {
                PlayerPrefs.SetInt("Adcount", PlayerPrefs.GetInt("Adcount") + 1);
            }
        }

        else
        {
            PlayerPrefs.SetInt("Adcount", 0);
        }
    }
    IEnumerator ShowBannerWhenReady()
    {
        while (!Advertisement.IsReady(placementId))
        {
            yield return new WaitForSeconds(0.5f);
        }
        Advertisement.Banner.Show(placementId);
    }
}

**i keep getting the error

Error CS0117 'Advertisement' does not contain a definition for 'Banner' ** EDIT: I got it working in test mode but when the I try it on my phone nothing pops up. I checked around and this problem has been happening to other people. I live in the US, so this feature should be working. Other people reccomend to use another type of ads. But unity ads is very convenient.


Solution

  • Banner ad example from Unity Docs:

    using System.Collections;
    using UnityEngine;
    using UnityEngine.Advertisements;
    
    public class BannerAdScript : MonoBehaviour {
    
        public string gameId = "1234567";
        public string placementId = "bannerPlacement";
        public bool testMode = true;
    
        void Start () {
            Advertisement.Initialize (gameId, testMode);
            StartCoroutine (ShowBannerWhenReady ());
        }
    
        IEnumerator ShowBannerWhenReady () {
            while (!Advertisement.IsReady (placementId)) {
                yield return new WaitForSeconds (0.5f);
            }
            Advertisement.Banner.Show (placementId);
        }
    }