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

Unity 2020 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 make clear what we expect when implementing our SDK:

  1. Trigger the 1st gameplayStart() upon the first user-input like a keystroke or mouse-click when the user is in the actual gameplay, so not in the menu.

  2. Trigger a gameplayStop() when the gameplay stops and is not interactive anymore.
    e.g. at a pause-screen or when the user has finished a level or is game-over.

  3. Implement the first commercialBreak() right before the first gameplayStart(). Implement at least 1 other commercialBreak() in the game.
    Ideally the commercialBreak() is always right before a gameplayStart().

  4. If applicable, implement at least 1 rewardedBreak() position in the game.

  5. Implement at least 1 happy moment in the game happyTime(), e.g. upon finishing a level or unlocking an achievement.

Common problems:

  • Make sure to export your build as a WASM-file. This reduces file-size by +/-30% and increases parsing speed.

  • Remember to block gameplay and gameplay audio during a commercialBreak() or rewardedBreak().
    You can find instructions below

  • Fire gameplayStart()s and gameplayStop()s at appropriate moments.

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

index.html

The contents of index.html are rewritten by our upload system. Custom html must be wrapped with <!-- poki include body --> and <!-- poki include end --> Replace 'body' with 'head' to place the wrapped html in the resulting tag.

2. Initialize the SDK

Copy the PokiUnitySDK.cs file and the Plugins folder into your project's Assets folder so you can access the PokiSDK from your code.

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

3. Integrating the functions

We require you to implement the following functions:

gameplayStart

The gameplayStart() event should be triggered every time when a level starts or when the user first interacts with the actual game (i.e. not in a menu, unless the game is the menu for example in a clicker game). The 1st gameplayStart() should be triggered upon the first user-input (keystroke or mouse-click) in the actual gameplay.

gameplayStop

The gameplayStop() should be triggered every time the gameplay halts, like in the case of a level finish, game-over or when going back to a menu or pausing the game, etc.

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

Continue playing

Heads up! Don’t forget to trigger a gameplayStart after triggering a gameplayStop, i.e. after a "pause"

commercialBreak

As described earlier, commercial breaks are used to display advertisements and should be triggered on natural breaks in your game. We' like to see that you implement the commercialBreak when the user has shown the intent to continue playing , i.e. on a play/restart/level-select button. We require you to implement at least 2 breaks:

  • It is mandatory to implement the first commercialBreak() in the beginning of the game right before the first gameplayStart().

  • For your other commercialBreak(s), we recommend you to always trigger a commercialBreak() right before a gameplayStart().
    If you are having trouble to identify the correct moments, 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.

Block gameplay and game-audio

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();
}

rewardedBreak

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.). Don't forget to block audio and gameplay during these rewardedBreaks

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();

happyTime

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. These happy moments can be used 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);

4. 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, poki-gdn.com (or *.poki.com)

5. 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

6. 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

Now test your SDK-implementation via our QA-tool. Make sure to send us the log of your QA-session by downloading it at the bottom of the page. This will not work offline or you must use a local server.

Compression

Make sure to disable any compression (no gzip, no brotli) when building, Poki will compress files on upload. WebGL-Deploying

Final deliverables

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

  • Final game build (Production Build, so no debug-version)
  • QA-log
  • 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)

That's it

We’ll check your QA-report and if all correct notify you that your game can go live!