Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different callbacks order #215

Open
kapyar opened this issue Feb 6, 2020 · 1 comment
Open

Different callbacks order #215

kapyar opened this issue Feb 6, 2020 · 1 comment

Comments

@kapyar
Copy link

kapyar commented Feb 6, 2020

Ok here is the thing.
[Given]
Unity:2019.2.19f1
GoogleMobileSdk:latest
AdController code: at the bottom

[When]

  1. Show Admob Rewarded Ads I have following callbacks order
    HandleRewardBasedVideoLoaded; HandleRewardBasedVideoOpened; HandleRewardBasedVideoStarted; **HandleRewardBasedVideoRewarded;** HandleRewardBasedVideoClosed;
  2. Show Unity Rewarded Ads
    HandleRewardBasedVideoLoaded; HandleRewardBasedVideoOpened; HandleRewardBasedVideoStarted; **HandleRewardBasedVideoClosed;** HandleRewardBasedVideoRewarded;

[Then]
I can't implement the correct behavior of my app.
Also, I can't find the methods to fetch only ads from Unity or Admob

using System;
using System.Collections.Generic;
using Assets.Scripts.Gameplay.Utils;
using GoogleMobileAds.Api;
using MEC;
using UnityLogger;

namespace Gameplay.Ads
{
    public class AdmobAdsController : IAdsController
    {
        #region Logger

        private static ILog Log;

        static AdmobAdsController()
        {
            Log = LogManager.GetLogger("AdmobAdsController");
            Log.IsEnabled = false;
        }

        #endregion


        private RewardBasedVideoAd _rewardBasedVideo;
        private InterstitialAd _interstitial;
        private readonly MainThreadDispatcher _mainThreadDispatcher;

        private Action _onRewardedVideoSuccess;
        private Action _onRewardedVideoFail;

        private bool _isLastVideoRewarded;

        public AdmobAdsController(MainThreadDispatcher mainThreadDispatcher)
        {
            _mainThreadDispatcher = mainThreadDispatcher;

#if DEBUG_ADS
            Log.Info("DEBUG_ADS enabled");
#endif

#if UNITY_ANDROID
            string appId = "ca-app-pub-3338596139932215~2628515619";
#elif UNITY_IPHONE
  string appId = "ca-app-pub-3338596139932215~2355200795";
#else
#endif


            MobileAds.Initialize(appId);

            InitRewardedAd();
            RequestRewardBasedVideo();

            RequestInterstitial();
        }

        private void DispatchMessage(string msg)
        {
            _mainThreadDispatcher.Dispatch(() => { Log.Info(msg); });
        }


        #region Rewarded

        private void InitRewardedAd()
        {
            _rewardBasedVideo = RewardBasedVideoAd.Instance;

            _rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
            _rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
            _rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
            _rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
            _rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
            _rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
            _rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
        }


        public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
        {
//            DispatchMessage("Reward ad loaded");
        }

        public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
        {
            _mainThreadDispatcher.Dispatch(() =>
            {
                Log.Err("rewarded HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);

                Timing.RunCoroutine(_LoadRewardedVideoAfterDelay());
            });
        }

        public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
        {
        }

        public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
        {
        }

        public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
        {
//            _mainThreadDispatcher.Dispatch(() =>
//            {
//                DispatchMessage("!!!>>> _onRewardedVideoClosed");
//
//                if (_isLastVideoRewarded == false)
//                {
//                    _onRewardedVideoFail?.Invoke();
//                }
//
//                RequestRewardBasedVideo();
//            });
        }

        public void HandleRewardBasedVideoRewarded(object sender, GoogleMobileAds.Api.Reward args)
        {
            _mainThreadDispatcher.Dispatch(() =>
            {
//                DispatchMessage("!!!>>> _onRewardedVideoSuccess");
                _onRewardedVideoSuccess?.Invoke();
                _isLastVideoRewarded = true;
            });
        }

        public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
        {
        }

        private IEnumerator<float> _LoadRewardedVideoAfterDelay()
        {
            yield return Timing.WaitForSeconds(5);

            RequestRewardBasedVideo();
        }


        private void RequestRewardBasedVideo()
        {
#if DEBUG_ADS
            string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#else
#if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3338596139932215/3750025595";
#elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3338596139932215/6621748869";
#else
#endif

#endif

#if DEBUG_ADS
            AdRequest request = new AdRequest.Builder().Build();
#else
            AdRequest request = new AdRequest.Builder().Build();
#endif

            _isLastVideoRewarded = false;
            // Load the rewarded video ad with the request.
            _rewardBasedVideo.LoadAd(request, adUnitId);
        }

        #endregion


        #region Interstitial

        private void RequestInterstitial()
        {
#if DEBUG_ADS
            string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#else
#if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3338596139932215/9002352277";
#elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3338596139932215/7415955786";
#else
#endif

#endif
            if (_interstitial != null)
            {
                _interstitial.Destroy();
            }

            _interstitial = new InterstitialAd(adUnitId);

            _interstitial.OnAdLoaded += HandleOnAdLoaded;
            _interstitial.OnAdFailedToLoad += HandleOnAdFailedToLoad;
            _interstitial.OnAdOpening += HandleOnAdOpened;
            _interstitial.OnAdClosed += HandleOnAdClosed;
            _interstitial.OnAdLeavingApplication += HandleOnAdLeavingApplication;

#if DEBUG_ADS
            AdRequest request = new AdRequest.Builder().Build();
#else
            AdRequest request = new AdRequest.Builder().Build();
#endif

            // Load the interstitial with the request.
            _interstitial.LoadAd(request);
        }


        public void HandleOnAdLoaded(object sender, EventArgs args)
        {
            DispatchMessage("Interstitial ad loaded");
        }

        public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
        {
            DispatchMessage("Interstitial ad failed to load");

            _mainThreadDispatcher.Dispatch(() => { Timing.RunCoroutine(_LoadInterstitialAfterDelay()); });
        }

        public void HandleOnAdOpened(object sender, EventArgs args)
        {
        }

        public void HandleOnAdClosed(object sender, EventArgs args)
        {
            _mainThreadDispatcher.Dispatch(() => { RequestInterstitial(); });
        }

        public void HandleOnAdLeavingApplication(object sender, EventArgs args)
        {
        }

        private IEnumerator<float> _LoadInterstitialAfterDelay()
        {
            yield return Timing.WaitForSeconds(5);

            RequestInterstitial();
        }

        #endregion


        public bool InterstitialAvailable => _interstitial.IsLoaded();

        public void ShowInterstitial()
        {
            _interstitial.Show();
        }

        public bool RewardedVideoAvailable => _rewardBasedVideo.IsLoaded();

        public void ShowRewardedVideo(Action onOk, Action onFailed)
        {
            _onRewardedVideoSuccess = onOk;
            _onRewardedVideoFail = onFailed;

            _rewardBasedVideo.Show();
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@kapyar and others