Skip to content

Unity

In order to simplify the implementation of our SDK in Unity WebGL games, we've created a template and C# class that can be used instead of having to implement the Javascript SDK.

First, make sure to download the required files:


Unity 5.6+ here.


Testing

Please note that ads will only show when testing a WebGL export. Inside of the Unity Editor only some simple logs will be sent to the console.

Checklist

Before we start with how, let’s quickly recap what we expect when implementing our SDK:

  1. Trigger a gameplayStart() when the user can actually control the game (e.g. start of a level) and a gamePlayStop() when the gameplay stops.

  2. Implement 2 commercialBreak() positions in the game.

  3. Implement at least 1 rewardedBreak() position in the game.

  4. Implement the loading events gameLoadingStart(), gameLoadingProgress(), gameLoadingFinished().

  5. Implement 1 happy moment in the game happyTime().

Common mistakes:

  • Remember to block gameplay and gameplay audio during a commercialBreak or rewardedBreak

  • Fire the gameplayStart and gameplayStop at the right moments

  • provide us a gzip compressed build of your game

Basic implementation

1. Set up our template

Copy the WebGLTemplates directory from the downloaded zip into your Unity game's Assets directory.

Now, select the template in Player Settings > WebGL > Resolution and Presentation:

Poki Template in Player Settings

2. Initialize the SDK

Copy the PokiUnitySDK.cs file into your project's Assets so you can access it from your code.

Now, make sure you initialize the SDK as early as possible in your application by calling PokiUnitySDK.Instance.init();.

3. Showing your first ad

In order to show an ad, all you have to do is call PokiUnitySDK.Instance.commercialBreak().

public void commercialBreakComplete(){
    Debug.Log("Commercial break finished");
}
//Set the complete callback
PokiUnitySDK.Instance.commercialBreakCallBack = commercialBreakComplete;
PokiUnitySDK.Instance.commercialBreak();

Next steps

1. Integrating the functions

We require you to implement the following functions:

Gameplay

The gameplayStart event should be triggered when a level starts or when the user can interact with the actual game (i.e. not in a menu, unless the game is the menu for example in a clicker game).

The gameplayStop should be triggered every time the gameplay halts, like in the case of death, going back to a menu, etc.

Pause

Heads up! Don’t forget to trigger a gameplay stop when the user pauses the game and a gameplay start when they choose to resume.

PokiUnitySDK.Instance.gameplayStart();
PokiUnitySDK.Instance.gameplayStop();

Commercial break

As described earlier, commercial breaks are used to display advertisements and should be triggered on natural breaks in your game. We require you to implement at least 2 breaks. Generally, we recommend placing one commercial break in the beginning of the game (after a first selection screen for instance) and then after the first gameplay (on ‘Play Again’ or ‘Restart’ for example). Your Poki contact will also help with properly identifying these moments.

PokiUnitySDK.Instance.commercialBreakCallBack = <function>;
PokiUnitySDK.Instance.commercialBreak();

//<function> is of type Function and will be triggered when the commercial break is finished.

//Example:
public void commercialBreakComplete(){
    Debug.Log("Commercial break finished");
}
//Set the complete callback
PokiUnitySDK.Instance.commercialBreakCallBack = commercialBreakComplete;
PokiUnitySDK.Instance.commercialBreak();

AdBlock

Make sure that users who have AdBlock enabled are still able to continue with the game. You can use the function PokiUnitySDK.Instance.adsBlocked() for this. This function returns true when the ads are blocked.

Rewarded Break

Rewarded breaks allow for a user to choose to watch a rewarded video ad in exchange for a certain benefit in the game (e.g. more coins, etc.).

PokiUnitySDK.Instance.rewardedBreakCallBack = <function(withReward)>;
PokiUnitySDK.Instance.rewardedBreak();

//<function> is of type Function and will be triggered when the rewarded break is finished.

//Example:
public void rewardedBreakComplete(bool withReward){
    Debug.Log("Rewarded break finished, should i get a reward:"+withReward.ToString());
}
//Set the complete callback
PokiUnitySDK.Instance.rewardedBreakCallBack = rewardedBreakComplete;
PokiUnitySDK.Instance.rewardedBreak();

Happy time

We would love to know what you believe to be the happy moments in your game. This could be the unlocking of a new car, or triggering a super combo in the game. We will use this in order to display cool animations on our portals. Please integrate at least one happy time.

PokiUnitySDK.Instance.happyTime(<intensity>);
//<intensity> is of type float between 0.0 and 1.0 and allows us to scale the intensity of happiness. You can for instance on a small happy moment trigger a happyTime(0.1) and on an extremely happy moment trigger happyTime(1.0)

//Example:
PokiUnitySDK.Instance.happyTime(0.5);

2. Integrate game logic

It's very likely that some extra logic is required, between triggering a commercialBreak and finishing it. For example if your game has music, this should be muted during the break like so:

public void triggerRestartGame() {
    muteMusic();
    PokiUnitySDK.Instance.commercialBreakCallBack = restartGame;
    PokiUnitySDK.Instance.commercialBreak();
}

public void restartGame(){
    //reset game here
    unMuteMusic();
}

This extra logic not only applies to music. Please keep in mind that for the best possible user experience, nothing in your game should interfere with the ad.

3. Sitelocking

Protecting your game from theft is very important to us, therefore your Unity game is automatically sitelocked to the following domains: games.poki.com, qa.poki.com, qa-files.poki.com, game-cdn.poki.com (or *.poki.com)

4. Additional helpful methods

Detecting if the SDK is initialized:

    PokiUnitySDK.Instance.isInitialized() // returns boolean

Detecting if ads are being blocked

    PokiUnitySDK.Instance.adsBlocked() //returns boolean

5. Full example

If your entire Unity game would be contained in one MonoBehaviour, the full implementation would look something like this:

using UnityEngine;

public class Game : MonoBehaviour {
    public void Awake () {
        // Initialize SDK
        PokiUnitySDK.Instance.init();
        StartLoading();
    }

    public void StartLoading() {
        // Load the game!

        // [...loading logic goes here...]

        // When all files are loaded
        OpenMainMenu();
    }

    public void OpenMainMenu(){
        gamePause(); // we mute audio and stop gameplay here
        if(!PokiUnitySDK.Instance.adsBlocked()){
            PokiUnitySDK.Instance.commercialBreakCallBack = StartGame; // after the commercial break we trigger StartGame
            PokiUnitySDK.Instance.commercialBreak();
        }else StartGame();
    }

    public void StartGame() {
        Debug.Log("Starting a new round!");
        PokiUnitySDK.Instance.gameplayStart();

        //[... do all the gameplay stuff];

        //Yes! We made a super combo!
        PokiUnitySDK.Instance.happyTime(0.8f); // scale is 0.0 to 1.0

        // We died but we can get one more shot by watching a rewarded video
        if(PokiUnitySDK.Instance.adsBlocked()){
            //hide the rewarded video
        }else{
            PokiUnitySDK.Instance.rewardedBreakCallBack = RevivePlayer;
            PokiUnitySDK.Instance.rewardedBreak();
        }

        // ahh we died again, let’s restart
        RestartLevel();
    }

    public void RevivePlayer(bool withReward){
        if(withReward){
            Debug.Log("Revive!");
            //resume game
        }else{
            //exit level
        }
    }

    public void RestartLevel(){
        // we failed the level, let’s close this gameplay session and trigger a break
        gamePause();
        PokiUnitySDK.Instance.gameplayStop();
        // we closed the gameplay now let’s trigger the break

        if(!PokiUnitySDK.Instance.adsBlocked()){
            PokiUnitySDK.Instance.commercialBreakCallBack = StartGame; // after the commercial break we trigger StartGame
            PokiUnitySDK.Instance.commercialBreak();
        }else StartGame();
    }

    public void gamePause() {
        Debug.Log("Starting break");
        //muteMusic();
        //pauseGamePlay();
    }

    public void continueGame() {
        Debug.Log("Break completed");
        //unMuteMusic();
        //resumeGameplay();
    }
}

QA Tool - Testing your game

To test your implementation you can upload your gzipped webgl build to our online QA tool.

gzip

The QA Tool only allows gzip compressed builds. Learn more here: WebGL-Deploying

That's it

Congrats! You’ve now successfully implemented the Poki SDK. Please send the following to creatorsupport@poki.com or to your Poki contact:

  • Final game build (Production Build)
  • Image assets:
  • Thumbnail (preferably 628x628 px, PNG-file)
  • Character-image (Large size, PNG-file)
  • Game-title (PNG-file)
  • Main background of the game (PNG-file)
  • 2 to 3 game-items (PNG-files)

We’ll then QA the game and let you know if there are any issues before putting it live!