Unity - 多次调用广告

Unity - Advertisment called multiple times

我在使用 Unity 的广告时遇到了这个问题。具体看完视频点击X键关闭视频后,我应该把奖品给玩家(进入下一关)。问题是 OnUnityAdsDidFinish 函数多次调用 if (showResult == ShowResult.Finished) 。我究竟做错了什么?我如何调用 FindObjectOfType () .LoadNextLevel () 函数一次; ?提前谢谢你

public class UnityAdsInterstitial : MonoBehaviour, IUnityAdsListener
{
private string gameID = "******";
//nome scelto nella DashBoard di Unity
private string interstitialID = "interstitial";

private string myPlacementId = "rewardedVideo";

public int randomHighValue = 30;

private bool TestMode = true;

private bool adsClosed = false;

public Button _button;

private void Awake()
{
    _button = GetComponent<Button>();
}

void Start()
{
    Debug.Log("Ads  start");
    _button = GameObject.Find("StartAds").GetComponent<Button>();

    _button.interactable = Advertisement.IsReady(myPlacementId);

    if (_button) _button.onClick.AddListener(ShowRewardedVideo);

    Advertisement.Initialize(gameID, TestMode);
    Advertisement.AddListener(this);
    if (adsClosed)
    {
        adsClosed = false;
    }
    
}

public void ShowInterstitial()
{
    if (Advertisement.IsReady(interstitialID) )
    {
        Advertisement.Show(interstitialID);
    }
}

public void ShowRewardedVideo()
{
    if (Advertisement.IsReady(myPlacementId))
    {
        Debug.Log("Rewarded video is Ready");
        Advertisement.Show(myPlacementId);
    }
    else
    {
        Debug.Log("Rewarded video is not ready at the moment! Please try again later!");
    }
}

public void HideBanner()
{
    Advertisement.Banner.Hide();
}

public void OnUnityAdsReady(string placementdID)
{
    if (placementdID == interstitialID)
    {
        Debug.Log("InterstitialIsReady");
    }

    if (placementdID == myPlacementId)
    {
        Debug.Log("RewardedIsReady");
        _button.interactable = true;
    }

}

public void OnUnityAdsDidFinish(string placementdID, ShowResult showResult)
{
    if (showResult == ShowResult.Finished)
    {
        // Reward the user for watching the ad to completion.
        
        if (!adsClosed)
        {
            adsClosed = true;
            FindObjectOfType<LevelLoader>().LoadNextLevel();  
        }
        
    }
    else if (showResult == ShowResult.Skipped)
    {
        // Do not reward the user for skipping the ad.
    }
    else if (showResult == ShowResult.Failed)
    {
        Debug.LogWarning("The ad did not finish due to an error.");
    }
}

public void OnUnityAdsDidError(string message)
{

    Debug.Log("OnUnityAdsDidError");
}
public void OnUnityAdsDidStart(string message)
{

    Debug.Log("OnUnityAdsDidStart");
}

我会通过检查展示位置 ID 开始我的调查(以防有更多展示位置)

  1. 检查回调是否针对正确的展示位置 ID
    if (showResult == ShowResult.Finished && placementId == myPlacementId)
    {
        // Reward the user for watching the ad to completion.
        
        if (!adsClosed)
        {
            adsClosed = true;
            FindObjectOfType<LevelLoader>().LoadNextLevel();  
        }
        
    }

第二个是我只有一个 UnityAdsInterstitial 的活动实例。您可以通过对象的引用在调试模式下检查它。如果不止一个实例从代码中的其他地方开始,那么您应该只限制一个。