1
votes

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

1

1 Answers

0
votes

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.