Answer the question
In order to leave comments, you need to log in
How to fix an error when adding AdMob?
I decided to embed AdMob into the game, added it, wrote a class for displaying ads, built it, everything works for me. I updated the version of the game on Google Play, a friend downloaded it and the game crashed, giving the following error: The
question is how to fix it?
My code:
using UnityEngine;
using GoogleMobileAds.Api;
using System;
public class AdmobRewardedVideoHelper : MonoBehaviour
{
[SerializeField]
private GameCore core;
[SerializeField]
private const string adUnitId = "/* id */";
[SerializeField]
private RewardedAd rewardedAd;
[SerializeField]
private AnalyticsHelper analytics;
[SerializeField]
private BonusesHelper bonusesHelper;
[SerializeField]
private int type;
void Start()
{
core = FindObjectOfType<GameCore>();
bonusesHelper = FindObjectOfType<BonusesHelper>();
analytics = FindObjectOfType<AnalyticsHelper>();
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(initStatus => { Debug.Log("[AdmobRewardedVideoHelper] - MobileAds.Initialize - initStatus -> " + initStatus); });
RewardedAdRequest();
}
private void RewardedAdRequest()
{
rewardedAd = new RewardedAd(adUnitId);
// Called when an ad request has successfully loaded.
rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
// Called when an ad request failed to load.
rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad;
// Called when an ad is shown.
rewardedAd.OnAdOpening += HandleRewardedAdOpening;
// Called when an ad request failed to show.
rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
// Called when the user should be rewarded for interacting with the ad.
rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
// Called when the ad is closed.
rewardedAd.OnAdClosed += HandleRewardedAdClosed;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded ad with the request.
rewardedAd.LoadAd(request);
}
private void HandleRewardedAdClosed(object sender, EventArgs e)
{
Debug.Log("[AdmobRewardedVideoHelper] - HandleRewardedAdClosed -> Ad Closed");
RewardedAdRequest();
}
private void HandleUserEarnedReward(object sender, Reward e)
{
switch (type)
{
case 1:
bonusesHelper.GetReward();
break;
case 2:
core.tasks.taskType = 0;
analytics.TaskSkipAnalyticsHanlder();
break;
}
Debug.Log("[AdmobRewardedVideoHelper] - HandleUserEarnedReward -> Ad Earned Reward");
}
private void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs e)
{
Debug.Log("[AdmobRewardedVideoHelper] - HandleRewardedAdFailedToShow -> Ad Failed To Show");
RewardedAdRequest();
}
private void HandleRewardedAdOpening(object sender, EventArgs e)
{
Debug.Log("[AdmobRewardedVideoHelper] - HandleRewardedAdOpening -> Ad Opening");
}
private void HandleRewardedAdFailedToLoad(object sender, AdErrorEventArgs e)
{
Debug.Log("[AdmobRewardedVideoHelper] - HandleRewardedAdFailedToLoad -> Ad Not Loaded");
RewardedAdRequest();
}
private void HandleRewardedAdLoaded(object sender, EventArgs e)
{
Debug.Log("[AdmobRewardedVideoHelper] - HandleRewardedAdLoaded -> Ad Loaded");
}
public void WatchAd(int type)
{
this.type = type;
if (rewardedAd.IsLoaded())
{
rewardedAd.Show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.google.unity.ads" android:versionName="1.0" android:versionCode="1">
<application>
<uses-library android:required="false" android:name="org.apache.http.legacy" />
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="BitClicker">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="" />
</application>
</manifest>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question