Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Unity banner ad C#

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;

public class BannerAdExample : MonoBehaviour
{
    // For the purpose of this example, these buttons are for functionality testing:
    [SerializeField] Button _loadBannerButton;
    [SerializeField] Button _showBannerButton;
    [SerializeField] Button _hideBannerButton;

    [SerializeField] BannerPosition _bannerPosition = BannerPosition.BOTTOM_CENTER;

    [SerializeField] string _androidAdUnitId = "Banner_Android";
    [SerializeField] string _iOsAdUnitId = "Banner_iOS";
    string _adUnitId;

    void Start()
    {
        // Disable the button until an ad is ready to show:
        _showBannerButton.interactable = false;
        _hideBannerButton.interactable = false;

        // Set the banner position:
        Advertisement.Banner.SetPosition(_bannerPosition);

        // Configure the Load Banner button to call the LoadBanner() method when clicked:
        _loadBannerButton.onClick.AddListener(LoadBanner);
        _loadBannerButton.interactable = true;
    }

    // Implement a method to call when the Load Banner button is clicked:
    public void LoadBanner()
    {
        // Set up options to notify the SDK of load events:
        BannerLoadOptions options = new BannerLoadOptions
        {
            loadCallback = OnBannerLoaded,
            errorCallback = OnBannerError
        };

        // Load the Ad Unit with banner content:
        Advertisement.Banner.Load(_adUnitId, options);
    }

    // Implement code to execute when the loadCallback event triggers:
    void OnBannerLoaded()
    {
        Debug.Log("Banner loaded");

        // Configure the Show Banner button to call the ShowBannerAd() method when clicked:
        _showBannerButton.onClick.AddListener(ShowBannerAd);
        // Configure the Hide Banner button to call the HideBannerAd() method when clicked:
        _hideBannerButton.onClick.AddListener(HideBannerAd);

        // Enable both buttons:
        _showBannerButton.interactable = true;
        _hideBannerButton.interactable = true;      
    }

    // Implement code to execute when the load errorCallback event triggers:
    void OnBannerError(string message)
    {
        Debug.Log($"Banner Error: {message}");
        // Optionally execute additional code, such as attempting to load another ad.
    }

    // Implement a method to call when the Show Banner button is clicked:
    void ShowBannerAd()
    {
        // Set up options to notify the SDK of show events:
        BannerOptions options = new BannerOptions
        {
            clickCallback = OnBannerClicked,
            hideCallback = OnBannerHidden,
            showCallback = OnBannerShown
        };

        // Show the loaded Banner Ad Unit:
        Advertisement.Banner.Show(_adUnitId, options);
    }

    // Implement a method to call when the Hide Banner button is clicked:
    void HideBannerAd()
    {
        // Hide the banner:
        Advertisement.Banner.Hide();
    }

    void OnBannerClicked() { }
    void OnBannerShown() { }
    void OnBannerHidden() { }

    void OnDestroy()
    {
        // Clean up the listeners:
        _loadBannerButton.onClick.RemoveAllListeners();
        _showBannerButton.onClick.RemoveAllListeners();
        _hideBannerButton.onClick.RemoveAllListeners();
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: group by linq multiple columns c# 
Csharp :: unity making a coroutine wait until another coroutine is done 
Csharp :: despicable me 
Csharp :: c# winforms textbox cursor position 
Csharp :: transformar de string a int c# 
Csharp :: bootstrap distane between col 
Csharp :: get directory of file c# 
Csharp :: c# split string into characters 
Csharp :: c# multiple catch exceptions 
Csharp :: c# how do you check if a string contains only digits 
Csharp :: invert string c# 
Csharp :: consecutive numbers c# 
Csharp :: c# error CS0515 
Csharp :: c# remove spaces from string 
Csharp :: if char is upper csharp 
Csharp :: how to move towards an object unity 
Csharp :: entity framework update child records 
Csharp :: write text files with C# 
Csharp :: httppostedfilebase in .net core 3.1 
Csharp :: unity get all components in gameobject 
Csharp :: debug c# console 
Csharp :: c# create folder 
Csharp :: c# string to variable name 
Csharp :: blazor onchange event not firing with inputselect 
Csharp :: c# stop process 
Csharp :: unity detect keyboard not mouse 
Csharp :: unit test throw exception c# xunit 
Csharp :: how to save datagridview data to database in c# windows application 
Csharp :: function in c# to do addition 
Csharp :: unity event 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =