In this topic you will learn how to implement any ad networks in this project.

First step is to import the SDK into the project. And as soon as it is imported we can start to write implementation code. All the code will be included in one file. Lets create it.

It will be a C# script with name that include the ad network name and "Integration" work. But it is just a recommendation — you may use any script names that you want.

In this script you need to create a class which will inherit AdIntegration (Yurowm.Advertising) type.

Here is a small example of the such script. It will not work yet.

using System;

namespace Yurowm.Advertising {
    public class OtherAdNetworkIntegration : AdIntegration {

        public override string GetName() {

        }

        public override bool IsReady(AdType type) {

        }

        public override void Show(AdType type, 
																	Action onComplete, 
																	Action<Adverts.Callback> callback) {

        }
    }
}

In the first method we need to return the name of the network we are integrating. In will be used in the Integrations editor. In our example the code will looks like this:

public override string GetName() {
    return "Other Ads";
}

Next two method are two most important method in the integration. IsReady method gives the project to know if the network is ready to show ads right now. But almost all the networks has different types of ads (Interstitial, Rewarded, etc), and some of them can be ready to show, and some — not. Because of this we have additional parameter for this method, which indicates what type of ads the project mean in this moment.

In our example we will use an API of some fake network SDK. So you can read it to understand how to it work and use this information for implementation other real SDKs.

OtherAdsManager manager;

public override bool IsReady(AdType type) {
    switch (type) {
        case AdType.Interstitial: return manager.IsInterstitalPreloaded();
        case AdType.Rewarded: return manager.IsRewardedPreloaded();
    }
    return false;
}

As soon as we return true in this method we must be ready to show the requested ad immediately. In the case it is requested to show, the Show method will be invoked. This method has AdType parameter too, because there are different types of ads which can be requested to show.

There is also onComplete action, that must be invoked when the ads will be shown succesfully — this action usually contains reward callback, but can be null if the requested type is not Rewarded.

And the last parameter is action too, but it is allow the project to know how it is going. Call callback action with None paratemeter is something went wrong and the showing was failed. Call it with Start paramter if it is started, but not complete yet, and Complete when it is complete.

One important thing: the actions from this methods can be invoked delayed, so here you can use coroutines. It can be very useful if the SDK not pausing the game while showing ads.

public override void Show(AdType type,
													Action onComplete,
													Action<Adverts.Callback> callback) {

    switch (type) {
        case AdType.Interstitial: {
            manager.ShowInterstital(r => {
                if (r == Result.Success) {
                    onComplete?.Invoke();
                    callback.Invoke(Adverts.Callback.Complete);
                } else
                    callback.Invoke(Adverts.Callback.None);
            });
            break;
        }
        case AdType.Rewarded: {
            manager.ShowRewarded(r => {
                if (r == Result.Success) {
                    onComplete?.Invoke();
                    callback.Invoke(Adverts.Callback.Complete);
                } else
                    callback.Invoke(Adverts.Callback.None);
            });
            break;
        }
        default: {
            callback.Invoke(Adverts.Callback.None);
            break;
        }
    }
}

And of course any SKD required to be initialized, so we have a special virtual method for this. Use it for the SDK initialization.

OtherAdsManager manager;

public override void Initialize() {
    base.Initialize();

    var appKey = GetAppKey();

    if (appKey == null) return;

    var platform = GetPlatform();

    var adsKeys = GetSuitableAdKeys().ToArray();

    if (adsKeys.IsEmpty()) return;

    manager.SetAppID(appKey.key);

    foreach (var adKey in adsKeys)
        manager.AddAdID(adKey.key);

    manager.Initialize();
}

GetAppKey, GetAdKey, GetSuitableAdKeys are the standard ad integration methods for storing the network IDs. It allows to write all neccesary keys right in the project dashboard and store them encrypted.

Untitled