Unity (WebGL)
Connect Unity to the SDK with a .jslib plugin and a C# class, and export with the WebGL platform.
1. Create the plugin
Create the file Assets/Plugins/WebGL/HGSDK.jslib:
mergeInto(LibraryManager.library, {
HG_GameplayStart: function () { if (window.HGSDK) HGSDK.gameplayStart(); },
HG_GameplayStop: function () { if (window.HGSDK) HGSDK.gameplayStop(); },
HG_SendScore: function (score) { if (window.HGSDK) HGSDK.sendScore(score); },
});
2. Create the C# class
Create Assets/Scripts/HGSDK.cs. Outside WebGL (in the editor, for example) the functions do nothing:
using System.Runtime.InteropServices;
public static class HGSDK
{
#if UNITY_WEBGL && !UNITY_EDITOR
[DllImport("__Internal")] private static extern void HG_GameplayStart();
[DllImport("__Internal")] private static extern void HG_GameplayStop();
[DllImport("__Internal")] private static extern void HG_SendScore(int score);
public static void GameplayStart() => HG_GameplayStart();
public static void GameplayStop() => HG_GameplayStop();
public static void SendScore(int score) => HG_SendScore(score);
#else
public static void GameplayStart() { }
public static void GameplayStop() { }
public static void SendScore(int score) { }
#endif
}
3. Call the functions
HGSDK.GameplayStart(); // when gameplay starts or continues
HGSDK.GameplayStop(); // when gameplay stops (losing, pausing, menu)
HGSDK.SendScore(score); // optional: only if your game has a score
4. Export for WebGL
- In File → Build Profiles (Build Settings before Unity 6), switch to the Web platform (WebGL before Unity 6).
- In Player Settings → Publishing Settings you can leave Compression Format as Gzip or Brotli: the website serves those files with the right headers. Disabled works too.
- Make the build and open its
index.html. Add the SDK line inside<head>, before any other script:
<script src="/sdk/v1/hg-sdk.js"></script>
Unity generates a new index.html on every build. To avoid repeating it, create your own WebGL template (in Assets/WebGLTemplates) with that line included.
5. Ads (optional)
Your game doesn't load ads by itself: it asks the website and the website shows them on top, freezing and muting the game meanwhile. There are two kinds: break (between runs) and reward (the player chooses to watch it in exchange for something in the game). Don't report that gameplay stops or continues because of an ad: the SDK freezes and resumes your game on its own.
// 1) Add these two inside the mergeInto of Assets/Plugins/WebGL/HGSDK.jslib
HG_AdBreak: function () {
if (window.HGSDK) HGSDK.showAd({ type: 'break' });
},
HG_AdReward: function (name) {
var ad = window.HGSDK ? HGSDK.showAd({ type: 'reward', name: UTF8ToString(name) }) : Promise.resolve({ rewarded: false });
ad.then(function (r) {
SendMessage('HGSDK', 'OnAdFinished', r.rewarded ? '1' : '0');
});
},
// 2) Assets/Scripts/HGAds.cs, on a GameObject named "HGSDK" in your first scene
using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class HGAds : MonoBehaviour
{
public static Action<bool> Finished; // true = the player earned the reward
void Awake() => DontDestroyOnLoad(gameObject); // it survives scene changes
#if UNITY_WEBGL && !UNITY_EDITOR
[DllImport("__Internal")] private static extern void HG_AdBreak();
[DllImport("__Internal")] private static extern void HG_AdReward(string name);
public static void Break() => HG_AdBreak();
public static void Reward(string name) => HG_AdReward(name);
#else
public static void Break() { }
public static void Reward(string name) => Finished?.Invoke(false);
#endif
public void OnAdFinished(string rewarded) => Finished?.Invoke(rewarded == "1");
}
// 3) Use it
// set Finished right before each Reward(): that's how you know which reward it was
// when they ask to revive
HGAds.Finished = rewarded => { if (rewarded) Revive(); else GameOver(); };
HGAds.Reward("revive");
// at another moment: to double their coins
HGAds.Finished = rewarded => { if (rewarded) coins *= 2; };
HGAds.Reward("double_coins");
// between runs, never before the first one
if (runs > 0) HGAds.Break();
runs++;
StartGame();
Several rewards: give each one its own name in Reward(name), a short label (up to 40 characters) that says which reward it is. Set Finished right before each one: that's how you know which reward the answer belongs to.
A reward can come back without an ad at any moment (none available or ads switched off): your game has to keep working. And a reward never gives score — revives, continues, extra lives or skins are fine.
6. Recommended settings
- Portrait resolution: in Player Settings → Resolution and Presentation, set the canvas to 720 × 1280, for example, and make it fill the whole window.
- No start screen of your own: the website shows the cover. Start the game with the first tap anywhere.
- Don't save high scores with PlayerPrefs: if your game has a score, the website saves the best of each signed-in player with
HGSDK.SendScore. - Sound: the SDK pauses and mutes your game's audio on its own, following the website's pause and sound button.
- Size: Unity builds are large; check the maximum allowed in Upload game and use Managed Stripping Level (in Player Settings → Other Settings) to reduce it.
7. Test it
Upload the build folder (or its .zip) in Upload game and tap 🧪 Test before submitting. More details in Test and publish.
HypercasualGaming