0
\$\begingroup\$

I ask for your help..

I have a crash after rewarded video ads (Thread 1: signal SIGABRT):

UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: AppControllerClassName]);

Last log:

objc[5106]: Cannot form weak reference to instance (0x282a1c4e0) of class GADMAdapterUnityRewardedAd. It is possible that this object was over-released, or is in the process of deallocation.
(lldb)

Full log: https://pastebin.com/yqg7rS6S

Ads controller script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using DarkFlameDX.Ads;
using DarkFlameDX.Purchases;
using DarkFlameDX.Saves.Variables;
using GoogleMobileAds.Api.Mediation.IronSource;
using GoogleMobileAds.Api.Mediation.UnityAds;
using GoogleMobileAds.Api.Mediation.Vungle;
using GoogleMobileAds.Api;

public class AdsController : MonoBehaviour
{
    #region Variables
    private int countGameOvers = 0;

    public InterstitialAd InterstitialAd;
    public RewardedAd RewardVideoAd;

    private static UnityAction onVideoWatched;
    private static UnityAction onInterstitialWatched;

    [HideInInspector]
    public bool FirstLaunch = true;
    public sbool NoAds;
    public UnityAction OnBuyNoAds;
    private static bool canGetReward;
    public static AdsController Instance;
    #endregion

    #region UnityMethods
    private void Awake()
    {
        if (!Instance)
        {
            Instance = this;

            NoAds = new sbool("NoAds");
        }
    }

    private void Start()
    {
#if UNITY_ANDROID
        string appId = "my_id";
#elif UNITY_IPHONE
        string appId = "my_id";
#else
        string appId = "unexpected_platform";
#endif

        MobileAds.Initialize(appId);

        IronSource.SetConsent(true);
        UnityAds.SetGDPRConsentMetaData(true);
        Vungle.UpdateConsentStatus(VungleConsent.ACCEPTED);

        LaunchInterstitial();
        LaunchRewardVideo();

        Dictionary<string, UnityAction> NonConsumableItems = new Dictionary<string, UnityAction>();
        NonConsumableItems.Add("com.littlebitgames.sportsidle.noads", BuyNoAdsCallback);

        Purchaser.Instance.Launch(null, NonConsumableItems, null);
    }

    private void FixedUpdate()
    {
        if (!xAds.RewardedVideoStatus.CanWatch && RewardVideoAd.IsLoaded())
        {
            xAds.RewardedVideoStatus.CanWatch = true;
            xAds.RewardedVideoStatus.OnLoaded?.Invoke();
        }
    }
    #endregion

    #region Methods
    private AdRequest CreateAdRequest()
    {
        return new AdRequest.Builder().Build();//.AddTestDevice(AdRequest.TestDeviceSimulator).AddTestDevice("ff4a643815fb1a10a162f650eafccfc5").Build();
    }

    private void LaunchInterstitial()
    {
#if UNITY_ANDROID
        string adUnitId = "ca-app-pub-3604050960266869/2421616881";
#elif UNITY_IPHONE
        string adUnitId = "ca-app-pub-3604050960266869/5471942744";//"ca-app-pub-3940256099942544/1033173712"; - test id
#else
        string adUnitId = "unexpected_platform";
#endif
        if (InterstitialAd != null)
            InterstitialAd.Destroy();

        InterstitialAd = new InterstitialAd(adUnitId);

        InterstitialAd.OnAdClosed += onInterstitialClosed;

        InterstitialAd.LoadAd(CreateAdRequest());
    }

    private void LaunchRewardVideo()
    {
#if UNITY_ANDROID
        string adUnitId = "ca-app-pub-3604050960266869/6916940259";
#elif UNITY_IPHONE
        string adUnitId = "ca-app-pub-3604050960266869/4482348606";//"ca-app-pub-3940256099942544/5224354917"; - test id
#else
        string adUnitId = "unexpected_platform";
#endif

        RewardVideoAd = new RewardedAd(adUnitId);

        // Called when the user should be rewarded for watching a video.
        RewardVideoAd.OnUserEarnedReward += onRewardedVideoFinished;
        // Called when the ad is closed.
        RewardVideoAd.OnAdClosed += onRewardedVideoClosed;

        RewardVideoAd.LoadAd(CreateAdRequest());
    }

#if !UNITY_EDITOR
    public void ShowRewardedVideo(UnityAction Callback)
    {
        if (Application.internetReachability != NetworkReachability.NotReachable)
        {
            canGetReward = false;
            Debug.Log("1");
            if (RewardVideoAd.IsLoaded())
            {
                onVideoWatched = Callback;
                Time.timeScale = 0f;

                xAds.RewardedVideoStatus.OnOpened?.Invoke();
                xAds.RewardedVideoStatus.CanWatch = false;

                RewardVideoAd.Show();
            }
            else
                Debug.Log("2");
        }
        else
        {
            Debug.Log("3");
            OffSoundsMute(true);
        }
    }

    public void ShowInterstitial(UnityAction Callback)
    {
        if (Application.internetReachability != NetworkReachability.NotReachable)
        {
            if (InterstitialAd.IsLoaded())
            {
                canGetReward = false;
                onInterstitialWatched = Callback;

                xAds.RewardedVideoStatus.OnOpened?.Invoke();
                xAds.RewardedVideoStatus.CanWatch = false;
#if UNITY_IOS
                Time.timeScale = 0f;
#endif
                InterstitialAd.Show();
            }
        }
        else
            OffSoundsMute(false);
    }
#else
    public void ShowRewardedVideo(UnityAction Callback)
    {
        onVideoWatched = Callback;

        xAds.RewardedVideoStatus.OnOpened?.Invoke();
        xAds.RewardedVideoStatus.CanWatch = false;
        canGetReward = true;
        Time.timeScale = 0f;
        OffSoundsMute(true);
        Debug.Log("onVideoWatched");
    }

    public void ShowInterstitial(UnityAction Callback)
    {
        onInterstitialWatched = Callback;

        xAds.RewardedVideoStatus.OnOpened?.Invoke();
        xAds.RewardedVideoStatus.CanWatch = false;
        canGetReward = true;
        Time.timeScale = 0f;
        OffSoundsMute(false);
    }
#endif

    public static void OffSoundsMute(bool RewardedVideo)
    {
        Instance.StartCoroutine(Coroutine_OffSoundMute(RewardedVideo));
    }

    private static IEnumerator Coroutine_OffSoundMute(bool RewardedVideo)
    {
        yield return new WaitForSecondsRealtime(1f);

        Time.timeScale = 1f;

        if (canGetReward)
        {
            onVideoWatched?.Invoke();
            onInterstitialWatched?.Invoke();
        }

        onVideoWatched = null;
        onInterstitialWatched = null;

        Instance.LaunchInterstitial();
        Instance.LaunchRewardVideo();
    }

    public void BuyNoAdsCallback()
    {
        NoAds.Value = true;
        OnBuyNoAds?.Invoke();
    }
    #endregion

    #region Actions
    public void onRewardedVideoClosed(object sender, System.EventArgs e) { OffSoundsMute(true); }
    public void onRewardedVideoFinished(object sender, Reward e) { canGetReward = true; }

    public void onInterstitialClosed(object sender, System.EventArgs e) { canGetReward = true; OffSoundsMute(false); }
    #endregion
}

Info:

  • Unity 2019.1.0f1
  • XCode 10.2
  • iPhone 5S
  • Tutorial
  • Ads (UnityAds/Vungle/ironSource/AdMob)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.