Unity (WebGL)

The Unity package com.gamealia.sdk is a .jslib plugin plus a C# wrapper over the JavaScript SDK. Unity 2021.3 or newer, WebGL build target.

Install

The easy way: run gamealia init --write in the Unity project folder (or ask your assistant, which runs gamealia_integrate_sdk). It adds:

  • the package in Packages/com.gamealia.sdk/ (Runtime: Gamealia.cs, Gamealia.jslib);
  • the WebGL template Assets/WebGLTemplates/Gamealia/, which loads the SDK before the Unity loader.

Then pick the template in Player Settings → WebGL → Resolution and Presentation → WebGL Template → Gamealia.

Your own template? Add this to its <head>, before the Unity loader script:

<script src="https://gamealia.app/sdk/v1.js"></script>

Build for WebGL and upload the build folder (the one with index.html and Build/). If your build uses threads (SharedArrayBuffer), set the listing's isolation to coop-coep.

Use

void Start()
{
    Gamealia.OnAdStart += () => Time.timeScale = 0f; // audio is muted for you (AutoMuteAudio)
    Gamealia.OnAdEnd += () => Time.timeScale = 1f;
    Gamealia.OnPause += () => Time.timeScale = 0f;
    Gamealia.OnResume += () => Time.timeScale = 1f;

    Gamealia.Init(info =>
    {
        // info.locale: "es", "en", ...; info.environment: "gamealia" | "preview" | "autotest" | "mock"
        Gamealia.Data.Load(r =>
        {
            if (r.ok) Gamealia.Data.Get<SaveData>("save", s => { if (s != null) save = s; Ready(); });
            else Ready(); // could not read the save: keep defaults, do not overwrite it
        });
    });
}

void Ready()
{
    Gamealia.Game.LoadingStop(); // required, once the game is playable
}

public void StartLevel() => Gamealia.Game.GameplayStart();

public void GameOver()
{
    Gamealia.Game.GameplayStop();
    Gamealia.Ad.RequestMidgame(shown => ShowGameOverScreen());
}

public void WatchAdForLife()
{
    Gamealia.Ad.RequestRewarded((shown, rewarded) => { if (rewarded) lives++; });
}

The package sample (Package Manager → Gamealia SDK → Samples → Basic) has a complete script.

API

Every callback runs on the main thread, asynchronously, and never throws. Failures give the same fallbacks as the JavaScript SDK.

C#JavaScript
Gamealia.Init(Action<InitResult>), Gamealia.Info, IsInitialized, Locale, Environment, IsMobileinit()
Gamealia.IsAvailablethe page loaded the SDK (false in the Editor)
Gamealia.Game.LoadingStart(), LoadingStop(), GameplayStart(), GameplayStop(), HappyTime()game.*
Gamealia.Ad.RequestMidgame(Action<bool> shown)ad.requestMidgame()
Gamealia.Ad.RequestRewarded(Action<bool, bool> (shown, rewarded))ad.requestRewarded()
Gamealia.User.Get(Action<PublicUser>), GetToken(Action<string>), RequestLogin(Action<PublicUser>)user.*
Gamealia.Data.Load(Action<LoadResult>) (ok, json, error)data.load()
Gamealia.Data.Get<T> / Set<T> (JsonUtility types), GetJson / SetJson, GetString / SetString, GetInt / SetInt, GetFloat / SetFloat, GetBool / SetBool, Remove, Cleardata.get/set/remove/clear()
Gamealia.Room.GetJoinCode() (sync), InviteLink(code, Action<string>), ShowInvite(code), Publish(RoomInfo), Unpublish()room.*
Gamealia.Presence.SetStatus(text)presence.setStatus()
events OnAdStart, OnAdEnd, OnPause, OnResume, OnMute, OnUnmute, OnUserChange, OnRoomJoinon(event)
  • Audio: with Gamealia.AutoMuteAudio (default true), AudioListener.pause is set while an ad plays.
  • Saves: 1 MB per player and game. Set* callbacks get false when the value was not saved.
  • Locale: Gamealia.Locale is the player's language; before Init finishes it is the system language.

In the Editor

In the Editor, on other platforms, and in a WebGL page without the SDK (for example offline), every call works locally: Init answers an environment: "mock" result, ads answer "not shown" and data is kept in PlayerPrefs. Build and run gamealia preview to see the real flow with mock ads.

How it works

Gamealia.jslib calls window.Gamealia and answers with SendMessage("GamealiaBridge", "OnResult", json); events arrive through SendMessage("GamealiaBridge", "OnEvent", json). The GamealiaBridge GameObject is created for you and survives scene loads: do not rename or destroy it.

Common autotest failures

  • sdk_loaded: the build does not use the Gamealia template (or yours lacks the script tag).
  • loading_stop: Gamealia.Game.LoadingStop() is not called within 15 s; call it as soon as the first scene is playable, not after a long download.
  • engine_integrity: upload the whole build folder; the .loader.js, .framework.js, .data and .wasm files must all be there.