0
votes

I have a simple platformer game in which I'm using chartboost and unity ads t show ads and I it was working fine during the test mode but ever since I deployed to production and disabled the test mode in both chartboost and unity ads I noticed that my interstitial ads and videos don't load or show except once in a blue moon that too of a same game and then it start failing again.

I also noticed that my ads impression are quite low on the chartboost and Unity. Can you please tell me if I code for it correctly? I used the chartboost example an built my ad controller through it, oh and I'm using caching for ads and unless ad isn't cached already I won't show it.

Here's the code:

    using UnityEngine;
    using System.Collections;
    using UnityEngine.Advertisements;
    using ChartboostSDK;
    using System;

    public class AdsController : MonoBehaviour
    {
         public static AdsController instance;

// app id for unity apps
private const string _appId = "someID";

public bool canShowChartBoostInterstitial;
public bool canShowChartBoostVideo;

private void Awake()
{
    MakeSingleton();

    if (!canShowChartBoostInterstitial)
    {
        LoadChartBoostInterstitialAds();
    }

    if (!canShowChartBoostVideo)
    {
        LoadChartBoostVideoAds();
    }

    LoadUnityAds();
}

private void MakeSingleton()
{
    if (instance != null)
    {
        Destroy(gameObject);
    }
    else
    {
        instance = this;
        DontDestroyOnLoad(gameObject);
    }
}

private void OnLevelWasLoaded()
{
    if (Application.loadedLevelName == "LevelMenu")
    {
        if (GameController.instance.canShowAds)
        {
            if (canShowChartBoostInterstitial)
            {
                ShowChartBoostInterstitial();
            }
            else
            {
                LoadChartBoostInterstitialAds();
            }
        }
    }
}

private void OnEnable()
{
    Chartboost.didCompleteRewardedVideo += VideoCompleted;
    Chartboost.didCacheInterstitial += DidCacheInterstitial;
    Chartboost.didDismissInterstitial += DidDismissInterstitial;
    Chartboost.didCloseInterstitial += DidCloseInterstitial;
    Chartboost.didCacheRewardedVideo += DidCacheVideo;
    Chartboost.didFailToLoadInterstitial += FailedToLoadInterstitial;
    Chartboost.didFailToLoadRewardedVideo += FailedToLoadVideo;
}

private void OnDisable()
{
    Chartboost.didCompleteRewardedVideo -= VideoCompleted;
    Chartboost.didCacheInterstitial -= DidCacheInterstitial;
    Chartboost.didDismissInterstitial -= DidDismissInterstitial;
    Chartboost.didCloseInterstitial -= DidCloseInterstitial;
    Chartboost.didCacheRewardedVideo -= DidCacheVideo;
    Chartboost.didFailToLoadInterstitial -= FailedToLoadInterstitial;
    Chartboost.didFailToLoadRewardedVideo -= FailedToLoadVideo;
}

public void VideoCompleted(CBLocation location, int reward)
{
    canShowChartBoostVideo = false;

    if (RewardController.instance != null)
    {
        RewardController.instance.VideoWatchedGiveUserAReward();
    }

    LoadChartBoostVideoAds();

}

public void DidCacheInterstitial(CBLocation location)
{
    canShowChartBoostInterstitial = true;
}

public void DidDismissInterstitial(CBLocation location)
{
    canShowChartBoostInterstitial = false;
    LoadChartBoostVideoAds();

    LoadChartBoostInterstitialAds();
}

public void DidCloseInterstitial(CBLocation location)
{
    canShowChartBoostInterstitial = false;
    LoadChartBoostVideoAds();

    LoadChartBoostInterstitialAds();
}

public void DidCacheVideo(CBLocation location)
{
    canShowChartBoostVideo = true;
}

private void FailedToLoadInterstitial(CBLocation location, CBImpressionError error)
{
    canShowChartBoostInterstitial = false;
    LoadChartBoostInterstitialAds();
}

private void FailedToLoadVideo(CBLocation location, CBImpressionError error)
{
    canShowChartBoostVideo = false;

    if (ShopMenuController.instance != null)
    {
        ShopMenuController.instance.FailedToLoadTheVideo();
    }

    LoadChartBoostVideoAds();
}

public void LoadChartBoostVideoAds()
{
    Chartboost.cacheRewardedVideo(CBLocation.Default);
}

public void LoadChartBoostInterstitialAds()
{
    Chartboost.cacheInterstitial(CBLocation.Default);
}

public void ShowChartBoostInterstitial()
{
    if (canShowChartBoostInterstitial)
    {
        Chartboost.showInterstitial(CBLocation.Default);            
    }
    else
    {
        LoadChartBoostInterstitialAds();
    }
}

public void ShowChartBoostVideo()
{
    if (canShowChartBoostVideo)
    {
        Chartboost.showRewardedVideo(CBLocation.Default);
    }
    else
    {
        LoadChartBoostVideoAds();
    }
}

public void LoadUnityAds()
{
    if (Advertisement.isSupported)
    {
        Advertisement.Initialize(_appId, false);
    }
}

public void ShowUnityAds()
{
    if (Advertisement.IsReady())
    {
        Advertisement.Show(null, new ShowOptions()
        {
            resultCallback = result =>
            {
                switch (result)
                {
                    case ShowResult.Finished:
                        GameController.instance.RewardPlayerWithSomething();
                        LoadUnityAds();
                        break;

                    case ShowResult.Failed:
                        GameController.instance.VideoNotLoadedOrUserSkippedTheVideo("Failed to load the video. Please try again.");
                        LoadUnityAds();
                        break;

                    case ShowResult.Skipped:
                        GameController.instance.VideoNotLoadedOrUserSkippedTheVideo("Video skipped.");
                        LoadUnityAds();
                        break;
                }
            }
        });
    }
    else
    {
        GameController.instance.VideoNotLoadedOrUserSkippedTheVideo("Failed to load the video. Please try again.");
        LoadUnityAds();
    }
}

 }
1

1 Answers

0
votes

Run cache after every time you show an interstitial.

like this :

   Chartboost.showInterstitial(CBLocation.Default); 
   Chartboost.cacheInterstitial(CBLocation.Default);

That way you will replenish the cache every time you show an ad.

Remember to cache as soon as its initialized as well.