Question by
MaxUnity111 · Mar 09, 2020 at 10:48 AM ·
admobmemory-leak
Memory leak
Greetings, I have a script for displaying rewarding ads that hangs on the prefab button. This button is located on two different panels in each stage. On the Android device, only the end of the advertisement starts to appear after ten scenes and the player is not rewarded, and after twenty scenes the game is interrupted, therefore that there is a memory leak.link text
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using GoogleMobileAds.Api;
using UnityEngine.Monetization;
using System;
public class MineBtn : MonoBehaviour
{
public int valueOfButton = 5;
public bool admob = true;
private FieldFillingManager ffm;
private double amount = 0;
private int countUpdate = 0;
# if UNITY_ANDROID
private const string ad_reward_diamonds = "ca-app-pub-9578413026007667/3802113406";
private const string ad_reward_filedPanel = "ca-app-pub-9578413026007667/8330371577";
private const string test_adID = "ca-app-pub-3940256099942544/5224354917";
private string admob_ID;
private RewardedAd rewardedAdDiamonds;
private RewardedAd rewardedAdFiled;
#endif
// Use this for initialization
void Start()
{
GetComponentInChildren<Text>().text = "+" + valueOfButton.ToString();
ffm = FindObjectOfType<FieldFillingManager>();
#if UNITY_ANDROID
if (admob)
{
// MobileAds.Initialize("ca-app-pub-9578413026007667~1116240310"); перенес в FieldFillingManager
if (GetComponentInParent<FailedPanel>())
{
admob_ID = ad_reward_filedPanel;
this.rewardedAdFiled = CreateAndLoadRewardedAd(test_adID);//ad_reward_filedPanel);
}
else
{
admob_ID = ad_reward_diamonds;
this.rewardedAdDiamonds = CreateAndLoadRewardedAd(test_adID);// ad_reward_diamonds);
}
// admob_ID = test_adID;
}
#endif
}
public RewardedAd CreateAndLoadRewardedAd(string adUnitId)
{
RewardedAd rewardedAd = new RewardedAd(adUnitId);
rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad;
rewardedAd.OnAdOpening += HandleRewardedAdOpening;
rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
rewardedAd.OnAdClosed += HandleRewardedAdClosed;
// AdRequest request = new AdRequest.Builder().AddTestDevice(AdRequest.TestDeviceSimulator).AddTestDevice("E2940A62C1B655BA").Build();
// AdRequest request = new AdRequest.Builder().AddTestDevice("E2940A62C1B655BA").Build();
AdRequest request = new AdRequest.Builder().Build();
rewardedAd.LoadAd(request);
return rewardedAd;
}
public void ButtonClick()
{
#if UNITY_ANDROID
if (admob)
{
if (GetComponentInParent<FailedPanel>())
{
if (rewardedAdFiled.IsLoaded())
{
rewardedAdFiled.Show();
}
}
else
{
if (rewardedAdDiamonds.IsLoaded())
{
rewardedAdDiamonds.Show();
}
}
}
#endif
}
#if UNITY_ANDROID
public void HandleRewardedAdLoaded(object sender, EventArgs args)
{
Debug.Log("HandleRewardedAdLoaded event received ");
}
public void HandleRewardedAdFailedToLoad(object sender, AdErrorEventArgs args)
{
Debug.Log("HandleRewardedAdFailedToLoad event received with message: " + args.Message);
}
public void HandleRewardedAdOpening(object sender, EventArgs args)
{
Debug.Log("HandleRewardedAdOpening event received ");
}
public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args)
{
Debug.Log("HandleRewardedAdFailedToShow event received with message: "
+ args.Message);
}
public void HandleRewardedAdClosed(object sender, EventArgs args)
{
Debug.Log("HandleRewardedAdClosed");
}
public void HandleUserEarnedReward(object sender, Reward args)
{
string type = args.Type;
amount = args.Amount;
ffm.SetDiamonds(ffm.GetDiamonds() + (int)amount);
Debug.Log("UserEarnedReward");
}
#endif
}
minebtn.txt
(4.3 kB)
Comment
Your answer
Follow this Question
Related Questions
assetBundle memory can not be free 0 Answers
Memory error pls help 0 Answers
Help with AdMob interstitials 0 Answers
"Module 'GoogleMobileAds' not found." 1 Answer
Admob impressions are not counted 0 Answers