Appodeal Ads Integration in Unity (Code)

11  comments

This post contains all the code that’s been written in the YouTube video.

You can read it at your own pace, although watching the video prior to looking at this code example is recommended.

 

AdManager.cs

using UnityEngine;
using AppodealAds.Unity.Api;
using AppodealAds.Unity.Common;

public class AdManager : MonoBehaviour, IRewardedVideoAdListener {

    int timesTriedToShowInterstitial = 0;

	// Use this for initialization
	void Start () {
        string appKey = "Put your app key here.";
        Appodeal.disableLocationPermissionCheck();
        Appodeal.setTesting(true);
        Appodeal.initialize(appKey, Appodeal.INTERSTITIAL | Appodeal.BANNER | Appodeal.REWARDED_VIDEO);
        Appodeal.setRewardedVideoCallbacks(this);
    }

    public void ShowBanner()
    {
        if (Appodeal.isLoaded(Appodeal.BANNER))
            Appodeal.show(Appodeal.BANNER_TOP);
    }
    public void HideBanner()
    {
        Appodeal.hide(Appodeal.BANNER);
    }

    public void ShowInterstitial()
    {
        timesTriedToShowInterstitial++;
        if (Appodeal.isLoaded(Appodeal.INTERSTITIAL) && timesTriedToShowInterstitial >= 5)
        {
            Appodeal.show(Appodeal.INTERSTITIAL);
            timesTriedToShowInterstitial = 0;
        }
    }

    public void ShowRewarded()
    {
        if (Appodeal.isLoaded(Appodeal.REWARDED_VIDEO))
            Appodeal.show(Appodeal.REWARDED_VIDEO);
    }

    public void onRewardedVideoLoaded() { }
    public void onRewardedVideoFailedToLoad() { }
    public void onRewardedVideoShown() { }
    public void onRewardedVideoClosed() { }
    public void onRewardedVideoFinished(int amount, string name) { ManagerScript.Instance.Coins++; }
}

 

ManagerScript.cs

using UnityEngine;
using UnityEngine.UI;

public class ManagerScript : MonoBehaviour {

    public static ManagerScript Instance { get; private set; }

    public Text CoinsTxt;

    private int coins;
    public int Coins
    {
        get
        {
            return coins;
        }

        set
        {
            coins = value;
            CoinsTxt.text = coins.ToString();
        }
    }

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
            Destroy(gameObject);
    }
}

 

About the author 

Matt Rešetár

Matt is an app developer with a knack for teaching others. Working as a Flutter Developer at LeanCode and a developer educator, he is set on helping other people succeed in their Flutter app development career.

You may also like

Flutter UI Testing with Patrol

Flutter UI Testing with Patrol
  • Not Working
    Error

    Assets/AdManager.cs(6,14): error CS0535: `AdManager’ does not implement interface member `AppodealAds.Unity.Common.IRewardedVideoAdListener.onRewardedVideoClosed(bool)’

  • error CS0535: `AdManager’ does not implement interface member `AppodealAds.Unity.Common.IRewardedVideoAdListener.onRewardedVideoClosed(bool)’

    Any idea why?

  • Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >