[英]Handle Button interactivity for Ads in Unity
I'm using Unity Ads on my application.我在我的应用程序中使用 Unity Ads。 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:我编写的最简单的解决方案是在update()
方法中检查广告的可用性:
public class AdButton : MonoBehaviour
{
// Some stuff here
void Update()
{
if (myButton)
myButton.interactable = Advertisement.IsReady(placementId);
}
}
but I also thought about using Actions
events:但我也考虑过使用Actions
事件:
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.问题是我是 Unity 的新手,我对update()
方法有点害怕。 I've been reading that it's easy to overload the application due to the nature of that function (called once per frame).我一直在阅读,由于 function (每帧调用一次)的性质,很容易使应用程序过载。
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西莫
My suggestion would be to use the Action approach, that's the approach I have taken in the past, for example:我的建议是使用 Action 方法,这是我过去采用的方法,例如:
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. OnUnityAdsReady()
是 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.这里的想法是注册一个监听器,将一个Action
作为参数传递给RegisterOnReadyListener()
方法并将其存储为成员变量。
If an Ad is ready immediately then the Action
will be invoked immediately.如果广告立即准备就绪,则将立即调用Action
。 If the Ad is not ready immediately then the Action
will be invoked when the OnUnityAdsReady()
callback is invoked by the UnityAds Package.如果广告没有立即准备好,则在 UnityAds Package 调用OnUnityAdsReady()
回调时将调用Action
。
Every time an Ad is watched OnUnityAdsReady()
will be invoked when the next Ad is ready to be viewed.每次观看广告时,将在准备观看下一个广告时调用OnUnityAdsReady()
。 This means that the Action
will be invoked every time an Ad is ready.这意味着每次广告准备就绪时都会调用该Action
。
A couple of tips for this subject:关于这个主题的一些提示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.