Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!