PokiSDK - Unity
1. Initialize the SDK
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:
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
:
index.html
Our upload system rewrites the contents of index.html. 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
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();
.
2. Implement the gameplay events
Use the gameplayStart() event to describe when users are playing your game (e.g. level start and unpause).
Use the gameplayStop() event to describe when users aren’t playing your game (e.g. level finish, game over, pause, quit to menu).
// first level loads, player clicks anywhere
PokiUnitySDK.Instance.gameplayStart();
// player is playing
// player loses round
PokiUnitySDK.Instance.gameplayStop();
// game over screen pops up
3. Implement commercialBreak
Commercial breaks are used to display video ads and should be triggered on natural breaks in your game. Throughout the rest of your game, we recommend you implement the commercialBreak() before every
gameplayStart() , i.e. whenever the user has shown an intent to continue playing.
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();
Important information about commercialBreaks
Not every single commercialBreak() will trigger an ad. Poki’s system will determine when a user is ready for another ad, so feel free to signal as many commercial break opportunities as possible.
4. Implement 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.). When using rewardedBreak() , please make it clear to the player beforehand that they’re about to watch an ad.
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();
About the rewardedBreak timer
rewardedBreak() affects the timing of
commercialBreak() - When a user interacts with a rewarded break, our system’s ad timer is reset to ensure the user does not immediately see another ad.
Final steps
Disable sound and input during ads
Make sure that audio and keyboard input are disabled during commercialBreaks, so that the game doesn’t interfere with the ad:
public void triggerRestartGame() {
// fire your mute audio function
// fire your disable keyboard input function
PokiUnitySDK.Instance.commercialBreakCallBack = restartGame;
PokiUnitySDK.Instance.commercialBreak();
}
public void restartGame(){
// reset game here
// fire your unmute audio function
// fire your enable keyboard input function
}
Sharable URLs & URL manipulation
You can create and use a sharable url with the following. Use triggerSharableUrl
to generate the sharable url. You can receive the url to share in the sharableURLResolved
function. Then, use triggerGetUrlParam
to retreive the parameters from the url.
PokiUnitySDK.Instance.shareableURLResolvedCallback = shareableURLResolved;
PokiUnitySDK.Instance.shareableURLRejectedCallback = shareableURLRejected;
public class urlParams : ScriptableObject {
public string param1 = "";
public string param2 = "";
public string test3 = "";
}
public void triggerShareableURL(){
urlParams data = ScriptableObject.CreateInstance<urlParams>();
data.param1 = "test1";
data.param2 = "test2";
data.test3 = "test3";
PokiUnitySDK.Instance.shareableURL(data);
}
public void shareableURLResolved(string url){
Debug.Log("shareableURL:"+url);
debugText.text = "shareableURL:"+url;
}
public void shareableURLRejected(){
Debug.Log("shareableURL rejected");
debugText.text = "shareableURL rejected";
}
public void triggerGetURLParam(){
string identifier = GameObject.Find("displayAd_inputIdentifier").GetComponent<InputField>().text;
string param = PokiUnitySDK.Instance.getURLParam(identifier);
Debug.Log("URL param "+identifier+"="+param);
debugText.text = "URL param "+identifier+"="+param;
}
Optimizing your Unity build
Here are some required steps to optimize your build for use on the web:
Publishing settings: (source)
- Set
Enable Exceptions
toExplicitly Thrown Exceptions Only
- Set the compression method to
Disabled
- Toggle
Name Files As Hashes
toON
Optimization settings (source)
- Toggle
Strip Engine Code
toON
- Set
Managed Stripping Level
tohigh
(if for some reason it breaks your game set it tomedium
)
Additionally we highly recommend to compress all your textures for a lower fileSize.
For each texture do the following:
- Set
Format
toAutomatic
(default) - Set
Compression
toLow Quality
or if the quality is really to low for some assetsHigh Quality
- Set
Use Crunch Compression
to92
or if the quality is to bad60
- If available set
Format
toRGB Crunched DXT1|BC1
See also: https://www.techarthub.com/an-introduction-to-texture-compression-in-unity/
And: https://www.techarthub.com/what-is-crunch-compression/
Additional helpful methods
Detecting if the SDK is initialized
Detecting if ads are being blocked
Upload and test your game in Poki for Developers
Congrats, you’ve successfully implemented the PokiSDK! Now upload your game to Poki for Developers and test it in our QA Tool. When you’re happy with the implementation, send us a review request and we’ll play the game. Feel free to contact us via Discord or developersupport@poki.com
if you’re stuck.
Export as WASM
Make sure to export your build as a WASM-file. This reduces file-size by +/-30% and increases parsing speed.