There's a simple guide on the Firebase
's website on how to implement the AdMob
's rewarded video:
https://firebase.google.com/docs/admob/unity/rewarded-video
Let's take some code:
public void OnVideoAdComplete(object sender, Reward reward){
SoundMgr.Instance().PlayRewardSound();
// double coins etc
}
Let's plug it in:
private void LoadVideoAd(){
videoAd = RewardBasedVideoAd.Instance;
AdRequest request = new AdRequest.Builder().AddTestDevice("XXXXXXXXXXXXX").Build();
videoAd.LoadAd(request, androidVideoAdID);
if(!videoCallbackSet){
videoCallbackSet = true;
videoAd.OnAdRewarded += OnVideoAdComplete;
}
}
And now let's get a ThreadException
cause we played a sound on the other thread:
Unity : Function SoundChannelStopList::~SoundChannelStopList() may only be called from main thread!
Unity : ExecutionEngineException: SIGILL
Unity : at SoundManager.PlaySuccess1Sound () [0x00000] in <filename unknown>:0
Unity : at Game.OnVideoAdClose () [0x00000] in <filename unknown>:0
Unity : at AdManager.OnVideoAdClose (System.Object sender, System.EventArgs args) [0x00000] in <filename unknown>:0
Unity : at GoogleMobileAds.Api.RewardBasedVideoAd.<RewardBasedVideoAd>m__4 (System.Object sender, System.EventArgs args) [0x00000] in <filename unknown>:0
Unity : at (wrapper delegate-invoke)
System.EventHandler`1<System.EventArgs>:invoke_void__this___object_EventArgs (object,System.EventArgs)
Unity : at GoogleMobileAds.Android.RewardBasedVideoAdClient.onAdClosed () [0x00000] in <filename unknown>:0
Unity : at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
Unity : Rethrow as TargetInvocationException: GoogleMobileAds.Android.RewardBasedVideoAdClient.onAdClosed()
Unity : at UnityEngine.AndroidJavaPro
How is this possible that it's year 2017 and the most leading game engine's plugin is runnning it's callbacks without some sort of App.PostRunnableOnMainThread()
?
Is there any workaround on this or we need to create an incredible pattern like:
update(){
if(adFinished)
//give rewards
}
I'd be grateful for any tips! :)