Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
86 views
in Technique[技术] by (71.8m points)

c# - Unity - Advertisment called multiple times

I'm having this problem using Unity's Advertisment. Specifically after watching the video and clicking the X button to close the video, I should give the prize to the player (go to the next level). The problem is that the OnUnityAdsDidFinish function calls if (showResult == ShowResult.Finished) multiple times. What am I doing wrong? how do I call the FindObjectOfType () .LoadNextLevel () function once; ? Thank you in advance

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");
}
question from:https://stackoverflow.com/questions/65922096/unity-advertisment-called-multiple-times

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would start my investigation by checking the placement id (in case there are more placements)

  1. Checking that the callback is for the proper placement id
    if (showResult == ShowResult.Finished && placementId == myPlacementId)
    {
        // Reward the user for watching the ad to completion.
        
        if (!adsClosed)
        {
            adsClosed = true;
            FindObjectOfType<LevelLoader>().LoadNextLevel();  
        }
        
    }

The second would be that I only have one active instance of UnityAdsInterstitial. You can check this in debug mode by the reference of the object. If more than one instance starts from somewhere else in your code, then you should just limit to one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...