Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Advertisement code for unity

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
 
public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
    [SerializeField] Button _showAdButton;
    [SerializeField] string _androidAdUnitId = "Rewarded_Android";
    [SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
    string _adUnitId = null; // This will remain null for unsupported platforms
 
    void Awake()
    {   
        // Get the Ad Unit ID for the current platform:
#if UNITY_IOS
        _adUnitId = _iOSAdUnitId;
#elif UNITY_ANDROID
        _adUnitId = _androidAdUnitId;
#endif

        //Disable the button until the ad is ready to show:
        _showAdButton.interactable = false;
    }
 
    // Load content to the Ad Unit:
    public void LoadAd()
    {
        // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
        Debug.Log("Loading Ad: " + _adUnitId);
        Advertisement.Load(_adUnitId, this);
    }
 
    // If the ad successfully loads, add a listener to the button and enable it:
    public void OnUnityAdsAdLoaded(string adUnitId)
    {
        Debug.Log("Ad Loaded: " + adUnitId);
 
        if (adUnitId.Equals(_adUnitId))
        {
            // Configure the button to call the ShowAd() method when clicked:
            _showAdButton.onClick.AddListener(ShowAd);
            // Enable the button for users to click:
            _showAdButton.interactable = true;
        }
    }
 
    // Implement a method to execute when the user clicks the button:
    public void ShowAd()
    {
        // Disable the button:
        _showAdButton.interactable = false;
        // Then show the ad:
        Advertisement.Show(_adUnitId, this);
    }
 
    // Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
    public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
    {
        if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
        {
            Debug.Log("Unity Ads Rewarded Ad Completed");
            // Grant a reward.

            // Load another ad:
            Advertisement.Load(_adUnitId, this);
        }
    }
 
    // Implement Load and Show Listener error callbacks:
    public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
    {
        Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
        // Use the error details to determine whether to try to load another ad.
    }
 
    public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
    {
        Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
        // Use the error details to determine whether to try to load another ad.
    }
 
    public void OnUnityAdsShowStart(string adUnitId) { }
    public void OnUnityAdsShowClick(string adUnitId) { }
 
    void OnDestroy()
    {
        // Clean up the button listeners:
        _showAdButton.onClick.RemoveAllListeners();
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: set request size c# 
Csharp :: c# query string builder 
Csharp :: install nuget package for S3 
Csharp :: dictionaries in unity 
Csharp :: asp.net c# get user email address from AD 
Csharp :: c# mongodb count documents 
Csharp :: unity how to make gamemanager instance 
Csharp :: unity2d switch camera 
Csharp :: query associative table ef6 
Csharp :: c# string to binary 
Csharp :: how to edit .csproj file 
Csharp :: how to iterate a generic list in c# 
Csharp :: Test for even Number 
Csharp :: convert from data adapter to Ienumerable C# 
Csharp :: c# datagridview cell align center 
Csharp :: serilog asp.net 5 
Csharp :: c# check that value is not null or 0 
Csharp :: color rgb to float c# 
Csharp :: c# string length 
Csharp :: how to show a first item in a combobox in c# winforms 
Csharp :: change object position 
Csharp :: convert bitmap to imagesource 
Csharp :: Transpose Matrix CSharp 
Csharp :: c# double 
Csharp :: send mail c# 
Csharp :: muovere un elemento in c# 
Csharp :: unity get quaternion z 
Csharp :: c# insert today datetime 
Csharp :: nuget Microsoft.EntityFrameworkCore.InMemory": "1.0.0" 
Csharp :: c# accept any enum 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =