[英]Unity ads don't work; they won't appear on my screen
我刚刚完成了一个小游戏,我想学习如何实现广告,以获取乐趣。 我不会发布游戏。 我按照 Unity 所说的做了,我完成了货币化,并使用以下代码制作了一个名为 Ads 的游戏对象(我确实输入了“andriod ID”和“iOS ID”):
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) { }
}
我没有收到任何错误,但广告没有出现,我被卡住了。 有没有人有任何想法?
您可以尝试以下步骤:
1.在项目设置中开启广告服务,如图。
2.在包管理器中导入广告包。
3.在构建设置中,将导出平台设置为Android平台。
向场景中添加两个按钮,一个用于加载广告,一个用于显示广告。
创建一个新脚本 AdsInitializer 并将其安装在相机上。
[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}"); }
新建一个脚本RewardedAdsButton,同样挂载在摄像头上,指定_showAdButton为展示广告的按钮,在按钮中添加RewardedAdsButton脚本中的LoadAd()方法加载广告。
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(); }
获取Android和iOS的游戏ID,在项目设置的服务页面点击Dashboard。
打开网页后,点击Monetization => ad Units,可以看到Android和Apple的游戏ID,需要自己开启。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.