繁体   English   中英

激励广告提供多种奖励而不是一种(说明中的视频)

[英]Rewarded Ads give multiple rewards instead of one (Video in the description)

我使用了Unity的代码,奖励是倍数……首先是 1,然后是 2,然后是 3,然后增加……我尝试删除一些代码,但继续这样做,然后就发生了这种情况。

我在网上搜索,但找不到任何可以解释多次点击奖励广告按钮的内容。

一切(显然)只有在我不按按钮并将“SerializeField”留空时才能正常工作,因为如果我删除按钮,返回给予更多奖励......有人可以检查一下并告诉我发生了什么? 我在这里添加代码

    using UnityEngine;
    using UnityEngine.Advertisements;
    using UnityEngine.UI;
    
    public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
    {
        [SerializeField] string _androidGameId = "4634758";
        [SerializeField] string _iOSGameId = "4634759";
        [SerializeField] bool _testMode = false;
        private string _gameId;
    
        [SerializeField] Button _showAdButton;
        [SerializeField] string rewardAndroidAdUnitId = "Rewarded_Android";
        [SerializeField] string rewardiOSAdUnitId = "Rewarded_iOS";
        string rewardAdUnitId = null; // This will remain null for unsupported platforms
    
        void Awake()
        {
            _gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
                ? _iOSGameId
                : _androidGameId;
    
            rewardAdUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
                        ? rewardiOSAdUnitId
                        : rewardAndroidAdUnitId;
    
            Advertisement.Initialize(_gameId, _testMode, this);
    
            rewardAdUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
                        ? rewardiOSAdUnitId
                        : rewardAndroidAdUnitId;
    
        }
    
        public void OnInitializationComplete()
        {
            Debug.Log("Unity Ads initialization complete.");
            LoadRewardedAd();
        }
    
        public void OnInitializationFailed(UnityAdsInitializationError error, string message)
        {
            Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
        }
    
        #region REWARDED ADS
    
        public void LoadRewardedAd()
        {
            Debug.Log("Loading Ad: " + rewardAdUnitId);
            Advertisement.Load(rewardAdUnitId, this);
        }
    
        public void ShowRewardedAd()
        {
            Advertisement.Show(rewardAdUnitId, this);
        }
    
        #endregion
    
    
        public void OnUnityAdsAdLoaded(string adUnitId)
        {
            if (adUnitId.Equals(rewardAdUnitId))
            {
                // Configure the button to call the ShowAd() method when clicked:
                //_showAdButton.onClick.AddListener(ShowRewardedAd);
                // Enable the button for users to click:
                //_showAdButton.interactable = true;
                Debug.Log("RewardedAds Loaded");
            }
        }
    
        public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
        {
            if (adUnitId.Equals(rewardAdUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
            {
                Debug.Log("Unity Ads Rewarded Ad Completed");
                // Grant a reward.
                _showAdButton.onClick.RemoveAllListeners(); //with this line of code the problem is solved but shows the NullReference.
                // Load another ad:
                Advertisement.Load(rewardAdUnitId, this);
            }
        }
    
        public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
        {
            if (adUnitId.Equals(rewardAdUnitId))
            {
                Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
            }
        }
    
        public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
        {
            if (adUnitId.Equals(rewardAdUnitId))
            {
                Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
            }
        }
    
        public void OnUnityAdsShowStart(string adUnitId) { }
        public void OnUnityAdsShowClick(string adUnitId) { }
    
        void OnDestroy()
        {
            //with or without it doesn't change, it works only when the game is closed, and when reopen is working the same
            //_showAdButton.onClick.RemoveAllListeners(); 
        }

}

解决了!!

所以,基本上来自 Unity 的脚本不知何故有一个未知错误,我无法解释自己如何或从哪里...所以我犯规的解决方案是在这种情况下添加一个 bool showAd = false并在showAdOnUnityAdsShowComplete函数,幸运的是,这足以解决问题,现在我可以将脚本放在按钮或 AdManager 中,并从OnClick()部分中的按钮调用函数,无论哪种方式现在都没有显示错误,也没有显示错误的倍数奖励。


希望它对其他人有用。


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

public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
{
    [SerializeField] string _androidGameId = "4634758";
    [SerializeField] string _iOSGameId = "4634759";
    [SerializeField] bool _testMode = true;
    private string _gameId;

    [SerializeField] Button _showAdButton; //You can remove this if want to add the function manually from OnClick()
    [SerializeField] string _androidAdUnitId = "Rewarded_Android";
    [SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
    string _adUnitId = null; // This will remain null for unsupported platforms

    private bool showAd = false;


    void Awake()
    {
        InitializeAds();

        Debug.Log("Awake");
        _adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
            ? _iOSAdUnitId
            : _androidAdUnitId;

        Debug.Log("the _adUnitId is: " + _adUnitId);
    }

    public void InitializeAds()
    {
        _gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
            ? _iOSGameId
            : _androidGameId;
        Advertisement.Initialize(_gameId, _testMode, this);

    }

    public void OnInitializationComplete()
    {
        Debug.Log("Unity Ads initialization complete.");
        LoadAd();
    }

    public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    {
        Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    }

    public void LoadAd()
    {
        // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled on top of the 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); //You can remove this if want to add the function manually from OnClick()
            // Enable the button for users to click:
            _showAdButton.interactable = true; //You can remove this if want to add the function manually from OnClick()
        }
    }

    // Implement a method to execute when the user clicks the button:
    public void ShowAd()
    {
        if (showAd == false)
        {
            Debug.Log("Showing Ad");
            // Disable the button:
            _showAdButton.interactable = false; //You can remove this if want to add the function manually from OnClick()
            // Then show the ad:
            Advertisement.Show(_adUnitId, this);
            _showAdButton.onClick.RemoveAllListeners(); //You can remove this if want to add the function manually from OnClick()
            Debug.Log("All Listeners Removed");
            showAd = true;
        }
    }

    // Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
    public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
    {
        if (showAd == true)
        {
            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);
                showAd = false;

            }
        }
    }

    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) { }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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