Reputation: 90
I was using Unity ads on my android game and everything was working perfectly, except from some devices where Unity ads where not showing sometimes. So, I wanted to test Admob rewarded video to see if I could get a better performance. Here's the code I'm using for Admob:
public void RequestRewardBasedVideo()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-243186545632812xxxxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "unused";
#else
string adUnitId = "unexpected_platform";
#endif
RewardBasedVideoAd rewardBasedVideo = RewardBasedVideoAd.Instance;
AdRequest request = new AdRequest.Builder().Build();
rewardBasedVideo.LoadAd(request, adUnitId);
showAdvertisment(rewardBasedVideo);
}
private void showAdvertisment(RewardBasedVideoAd rewardBasedVideo)
{
if (rewardBasedVideo.IsLoaded())
{
rewardBasedVideo.Show();
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
//reward
}
I assigned the RequestRewardBasedVideo() to a button, but the issue is that the videos are not showing! I think I'm in the right path because when I click the button, the console logs:
Dummy .ctor
Dummy CreateRewardBasedVideoAd
Dummy LoadAd
Dummy IsLoaded
Dummy ShowRewardBasedVideoAd
I have already tried putting the app on my Android device and imported the Google Admob package for Unity, also configured the ads in the Admob panel. Anyone have any ideas for what I can do to solve this??
Upvotes: 3
Views: 5192
Reputation: 335
what if video isn't loaded? You need to handle that case, listen to load event and then hit show.
if (rewardBasedVideo.IsLoaded())
{
rewardBasedVideo.Show();
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}else{
rewardBasedVideo.OnAdLoaded += HandleVideoLoaded;
}
public void HandleVideoLoaded(object sender, Reward args)
{
rewardBasedVideo.Show();
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
rewardBasedVideo.OnAdRewarded -= HandleRewardBasedVideoRewarded;
}
Upvotes: 0
Reputation: 21
Your code is wrong. You should separate request ad and show ad into two functions. In Start() function, you call request function and show ad function should be hooked into your button. It's because when you request ad, it'll take some time to make ad available for you.
Upvotes: 0
Reputation: 147
It may be due to no reward video or no ad video available currently. Try testing with test ads. Do include test device ID in your request code like this:
AdRequest request = new AdRequest.Builder()
.AddTestDevice("34343")
.Build();
Try with sample ad UNIT Id
. Hopefully it works.
Upvotes: 0