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

230
Visualizações
Handle Button interactivity for Ads in Unity

I'm using Unity Ads on my application. Everything works fine but I want to add an feature which handles the Ad button interactivity whether the Ad is ready or not, through the whole app lifetime.

The simplest solution I've coded consists in checking the availability of the ads within the update() method:

public class AdButton : MonoBehaviour
{
    // Some stuff here

    void Update()
    {
        if (myButton)
            myButton.interactable = Advertisement.IsReady(placementId);
    }
}

but I also thought about using Actions events:

public class AdsManager : MonoBehaviour
{
    public static event Action<bool> AdReady;

    // Some stuff here

    void update()
    {
        adReady?.invoke(Advertisement.isReady(placementId));
    }
}
public class AdButton : MonoBehaviour
{
    // Some stuff here

    void OnEnable()
    {
        AdsManager.AdReady += RefreshButton;
    }

    void RefreshButton(bool interactivity)
    {
        myButton.interactable = interactivity
    }

    void OnDisable()
    {
        AdsManager.AdReady -= RefreshButton;
    }
}

The problem is that I'm new in Unity and I'm a little bit scared about the update() method. I've been reading that it's easy to overload the application due to the nature of that function (called once per frame).

Should I be worried about these lines of code or calling every frame these instructions don't slow down my application?

Thanks for your help.

Simo

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

My suggestion would be to use the Action approach, that's the approach I have taken in the past, for example:

private Action onAdReadyListener;

public void RegisterOnReadyListener(Action onAdReadyListener)
{
    this.onAdReadyListener = onAdReadyListener;

    if (Advertisement.IsReady(myPlacementId))
    {
        onAdReadyListener?.Invoke();
    }
}

public void OnUnityAdsReady(string placementId)
{
    if (placementId == myPlacementId)
    {
        onAdReadyListener?.Invoke();
    }
}

OnUnityAdsReady() is an override method provided by the UnityAds package. It is invoked when an Ad is ready to be viewed.

The idea here is to register a listener, passing an Action through as a parameter to the RegisterOnReadyListener() method and storing it as a member variable.

If an Ad is ready immediately then the Action will be invoked immediately. If the Ad is not ready immediately then the Action will be invoked when the OnUnityAdsReady() callback is invoked by the UnityAds Package.

Every time an Ad is watched OnUnityAdsReady() will be invoked when the next Ad is ready to be viewed. This means that the Action will be invoked every time an Ad is ready.

A couple of tips for this subject:

  • Unity limits to 25 Ads being shown per day in a production environment.
  • Android is very strict about how you show Ads, such as making it clear the user is about to be presented with an Ad and not showing more than 1 Ad per "Screen"
  • If you watch your own Ads too many times in a production environment your account will get blocked. You can control whether Test Ads or Production Ads are shown from the Unity Dashboard for your project. This option overrides any test option you have in code.
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