Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

457
Visualizações
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");
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda