简体   繁体   中英

Unity ads don't work; they won't appear on my screen

I just finished a tiny game and I wanted to learn how to implement ads, for fun. I won't post the game. I did what Unity was saying, I finished the monetization and I made a game object called Ads (I did put "andriod ID" and "iOS ID") with the following code:

using UnityEngine;
using UnityEngine.Advertisements;

public class InterstitialAdExample : MonoBehaviour, IUnityAdsLoadListener, 
IUnityAdsShowListener
{
[SerializeField] string _androidAdUnitId = "Interstitial_Android";
[SerializeField] string _iOsAdUnitId = "Interstitial_iOS";
string _adUnitId;

void Awake()
{
    // Get the Ad Unit ID for the current platform:
    _adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
        ? _iOsAdUnitId
        : _androidAdUnitId;
}

// 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);
}

// Show the loaded content in the Ad Unit:
public void ShowAd()
{
    // Note that if the ad content wasn't previously loaded, this method will fail
    Debug.Log("Showing Ad: " + _adUnitId);
    Advertisement.Show(_adUnitId, this);
}

// Implement Load Listener and Show Listener interface methods: 
public void OnUnityAdsAdLoaded(string adUnitId)
{
    // Optionally execute code if the Ad Unit successfully loads content.
}

public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
{
    Debug.Log($"Error loading Ad Unit: {adUnitId} - {error.ToString()} - {message}");
    // Optionally execute code if the Ad Unit fails to load, such as attempting to try again.
}

public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
{
    Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
    // Optionally execute code if the Ad Unit fails to show, such as loading another ad.
}

public void OnUnityAdsShowStart(string adUnitId) { }
public void OnUnityAdsShowClick(string adUnitId) { }
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState) { }
}

I don't get any errors, but the ads don't appear and I am stuck. Does anyone have any ideas?

You can try the following steps:

1.Turn on the advertising service in the project settings, as shown in the figure.

在此处输入图像描述

2.Import the Advertisement package in the package manager.

在此处输入图像描述

3.In the build settings, set the export platform to Android platform.

在此处输入图像描述

  1. Add two buttons to the scene, one to load the ad and one to display the ad.

  2. Create a new script AdsInitializer and mount it on the camera.

     [SerializeField] string _androidGameId; [SerializeField] string _iOsGameId; [SerializeField] bool _testMode = true; [SerializeField] bool _enablePerPlacementMode = true; private string _gameId; void Awake() { InitializeAds(); } public void InitializeAds() { _gameId = (Application.platform == RuntimePlatform.IPhonePlayer) ? _iOsGameId : _androidGameId; Advertisement.Initialize(_gameId, _testMode, _enablePerPlacementMode, this); } public void OnInitializationComplete() { Debug.Log("Unity Ads initialization complete."); } public void OnInitializationFailed(UnityAdsInitializationError error, string message) { Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}"); }
  3. Create a new script RewardedAdsButton, which is also mounted on the camera, specify _showAdButton as the button to display the advertisement, and add the LoadAd() method in the RewardedAdsButton script to the button to load the advertisement.

     void Awake() { // Get the Ad Unit ID for the current platform: _adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer) ? _iOsAdUnitId : _androidAdUnitId; //Disable button until 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(); }
  4. Get the game ID of Android and iOS, and click Dashboard in the service page of the project settings.

在此处输入图像描述

After the webpage is opened, click Monetization => ad Units to see the game IDs of Android and Apple, you need to enable them yourself.

在此处输入图像描述

  1. Go back to Unity and enter the queried id in the AdsInitializer script on the camera.

在此处输入图像描述

  1. Packaged into the Android emulator to run.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM