@oasiz/sdk 1.6.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,69 +1,47 @@
1
- # Oasiz game SDKs
1
+ # @oasiz/sdk
2
2
 
3
- Games on Oasiz can integrate using either of these official SDKs:
3
+ Typed SDK for integrating games with the Oasiz platform. Handles score submission, haptic feedback, cross-session state persistence, multiplayer room codes, navigation hooks, and app lifecycle events for local development.
4
4
 
5
- | Platform | Package | Use for |
6
- | --- | --- | --- |
7
- | **HTML5 / TypeScript** | [`@oasiz/sdk`](#html5--typescript-oasizsdk) (npm) | Canvas, Phaser, custom JS/TS, any browser game |
8
- | **Unity WebGL** | [Unity runtime](#unity-webgl-sdk) in this repo (`packages/OasizSDK/`) | Unity projects targeting WebGL |
9
-
10
- Both talk to the same host bridges (`window.submitScore`, `__oasizLeaveGame`, layout APIs, custom DOM events such as `oasiz:pause`, etc.). Unsupported hosts no-op safely; local dev usually logs warnings instead of crashing.
11
-
12
- ---
13
-
14
- ## HTML5 / TypeScript (`@oasiz/sdk`)
15
-
16
- Typed SDK for integrating browser games with the Oasiz platform: score, haptics, cross-session state, multiplayer hooks, layout (safe area, leaderboard visibility), graphics performance, navigation (back / leave), and lifecycle events.
17
-
18
- ### Install
5
+ ## Install
19
6
 
20
7
  ```bash
21
- npm install @oasiz/sdk
8
+ npm install @oasiz/sdk@^0.1.0
22
9
  ```
23
10
 
24
- The published package includes ESM, CommonJS, and TypeScript declarations.
25
-
26
11
  ## Quick start
27
12
 
28
13
  ```ts
29
14
  import { oasiz } from "@oasiz/sdk";
30
15
 
31
- // 1. Load persisted state at the start of each session
16
+ // 1. Emit score normalization config once on init
17
+ oasiz.emitScoreConfig({
18
+ anchors: [
19
+ { raw: 30, normalized: 100 },
20
+ { raw: 60, normalized: 300 },
21
+ { raw: 120, normalized: 600 },
22
+ { raw: 300, normalized: 950 },
23
+ ],
24
+ });
25
+
26
+ // 2. Load persisted state at the start of each session
32
27
  const state = oasiz.loadGameState();
33
28
  let level = typeof state.level === "number" ? state.level : 1;
34
29
 
35
- // 2. Save state at checkpoints
30
+ // 3. Save state at checkpoints
36
31
  oasiz.saveGameState({ level, coins: 42 });
37
32
 
38
- // 3. Trigger haptics on key events
33
+ // 4. Trigger haptics on key events
39
34
  oasiz.triggerHaptic("medium");
40
35
 
41
- // 4. Respect the host's top safe area (percent of viewport height → CSS vh)
42
- document.documentElement.style.setProperty(
43
- "--safe-top",
44
- `${oasiz.safeAreaTop}vh`,
45
- );
46
-
47
- // 5. Pick graphics settings for this device
48
- const graphics = oasiz.getGraphicsPerformance();
49
- renderer.setPixelRatio(graphics.tier === "high" ? 1.5 : graphics.tier === "medium" ? 1.25 : 1);
50
-
51
- // 6. Submit score when the game ends
36
+ // 5. Submit score when the game ends
52
37
  oasiz.submitScore(score);
53
-
54
- // 7. Optionally hide the leaderboard while a custom overlay is open
55
- oasiz.setLeaderboardVisible(false);
56
-
57
- // 8. Optionally surface console logs in-game while debugging
58
- oasiz.enableLogOverlay({
59
- enabled: new URLSearchParams(window.location.search).has("oasizLogs"),
60
- collapsed: true,
61
- });
62
38
  ```
63
39
 
64
- ### Score
40
+ ---
41
+
42
+ ## Score
65
43
 
66
- #### `oasiz.submitScore(score: number)`
44
+ ### `oasiz.submitScore(score: number)`
67
45
 
68
46
  Submit the player's final score at game over. Call this exactly once per session, when the game ends. The platform handles leaderboard persistence — do not track high scores locally.
69
47
 
@@ -76,9 +54,39 @@ private onGameOver(): void {
76
54
  - `score` must be a non-negative integer. Floats are floored automatically.
77
55
  - Do not call on intermediate scores or level completions, only on final game over.
78
56
 
79
- ### Haptics
57
+ ---
58
+
59
+ ### `oasiz.emitScoreConfig(config)`
60
+
61
+ Maps raw score values to the platform's normalized 0–1000 scale. Call once during initialization, not every frame.
62
+
63
+ ```ts
64
+ oasiz.emitScoreConfig({
65
+ anchors: [
66
+ { raw: 10, normalized: 100 }, // beginner
67
+ { raw: 30, normalized: 300 }, // good
68
+ { raw: 75, normalized: 600 }, // great
69
+ { raw: 200, normalized: 950 }, // godlike
70
+ ],
71
+ });
72
+ ```
73
+
74
+ **Anchor rules:**
75
+ - Exactly 4 anchors required.
76
+ - `raw` values must be strictly increasing.
77
+ - `normalized` values must end at exactly `950`.
78
+ - Choose thresholds based on realistic player skill bands.
79
+
80
+ **Practical guidance by game type:**
81
+ - Survival / time games → use seconds survived as `raw`
82
+ - Score accumulation games → use points as `raw`
83
+ - Puzzle games → use level reached or stars earned as `raw`
80
84
 
81
- #### `oasiz.triggerHaptic(type: HapticType)`
85
+ ---
86
+
87
+ ## Haptics
88
+
89
+ ### `oasiz.triggerHaptic(type: HapticType)`
82
90
 
83
91
  Trigger native haptic feedback. Always guard with the user's haptics setting.
84
92
 
@@ -87,7 +95,7 @@ type HapticType = "light" | "medium" | "heavy" | "success" | "error";
87
95
  ```
88
96
 
89
97
  | Type | When to use |
90
- | --- | --- |
98
+ |---|---|
91
99
  | `"light"` | UI button taps, menu navigation, D-pad press |
92
100
  | `"medium"` | Collecting items, standard collisions, scoring |
93
101
  | `"heavy"` | Explosions, major impacts, screen shake |
@@ -118,45 +126,20 @@ private onGameOver(): void {
118
126
 
119
127
  Haptics are throttled internally (50ms cooldown) to prevent spam.
120
128
 
121
- ### Debugging
122
-
123
- #### `oasiz.enableLogOverlay(options?: LogOverlayOptions)`
124
-
125
- Mount an opt-in in-game console viewer for local debugging, QA sessions, or creator support. It mirrors `console.log`, `console.info`, `console.warn`, `console.error`, and `console.debug` into a floating overlay inside the game iframe. The overlay can be collapsed, repositioned by dragging the top bar, and resized from the bottom-right corner while the action buttons remain clickable.
126
-
127
- ```ts
128
- const logOverlay = oasiz.enableLogOverlay({
129
- enabled: new URLSearchParams(window.location.search).has("oasizLogs"),
130
- collapsed: true,
131
- });
132
-
133
- console.log("[Boot] Scene ready");
134
-
135
- // Optional cleanup if your game tears down and remounts
136
- logOverlay.destroy();
137
- ```
138
-
139
- Options:
140
-
141
- - `enabled`: defaults to `true`. Pass your own flag or query-param check here.
142
- - `collapsed`: start with only the toggle pill visible.
143
- - `maxEntries`: cap retained log lines. Defaults to `200`.
144
- - `title`: optional label shown at the top of the panel. Defaults to `SDK Logs`.
145
-
146
- The returned handle supports `show()`, `hide()`, `clear()`, `isVisible()`, and `destroy()`.
129
+ ---
147
130
 
148
- ### Game state persistence
131
+ ## Game state persistence
149
132
 
150
133
  Persist cross-session data such as unlocked levels, inventory, or lifetime stats. State is stored per-user per-game in the Oasiz backend — available across devices and app reinstalls.
151
134
 
152
- #### `oasiz.loadGameState(): Record<string, unknown>`
135
+ ### `oasiz.loadGameState(): Record<string, unknown>`
153
136
 
154
137
  Returns the player's saved state synchronously. Returns `{}` if no state has been saved yet. Call once at the start of the game.
155
138
 
156
139
  ```ts
157
140
  private initFromSavedState(): void {
158
141
  const state = oasiz.loadGameState();
159
- this.level = typeof state.level === "number" ? state.level : 1;
142
+ this.level = typeof state.level === "number" ? state.level : 1;
160
143
  this.lifetimeHits = typeof state.lifetimeHits === "number" ? state.lifetimeHits : 0;
161
144
  this.unlockedSkins = Array.isArray(state.unlockedSkins) ? state.unlockedSkins : [];
162
145
  }
@@ -164,7 +147,7 @@ private initFromSavedState(): void {
164
147
 
165
148
  Always validate the shape of loaded data — it may be `{}` on first play.
166
149
 
167
- #### `oasiz.saveGameState(state: Record<string, unknown>)`
150
+ ### `oasiz.saveGameState(state: Record<string, unknown>)`
168
151
 
169
152
  Queues a debounced save. Saves are batched automatically — call freely at checkpoints without worrying about request spam.
170
153
 
@@ -181,12 +164,11 @@ private onLevelComplete(): void {
181
164
  ```
182
165
 
183
166
  **Rules:**
184
-
185
167
  - State must be a plain JSON object (not an array or primitive).
186
168
  - Do not use `localStorage` for cross-session progress — use `saveGameState` so data syncs across platforms.
187
169
  - Do not store scores here — scores are submitted via `submitScore`.
188
170
 
189
- #### `oasiz.flushGameState()`
171
+ ### `oasiz.flushGameState()`
190
172
 
191
173
  Forces an immediate write, bypassing the debounce. Use at important checkpoints like game over or before the page unloads.
192
174
 
@@ -198,122 +180,19 @@ private onGameOver(): void {
198
180
  }
199
181
  ```
200
182
 
201
- ### Layout
202
-
203
- Use runtime viewport insets instead of direct CSS `env(safe-area-inset-*)` reads or hardcoded offsets. The SDK resolves host-provided Oasiz values first, then browser CSS `env(safe-area-inset-*)`, then legacy `constant(safe-area-inset-*)`, and finally `0`.
204
-
205
- The top inset preserves the existing Oasiz game-safe top behavior: host chrome, invite UI, and leaderboard clearance can contribute to it. Left, right, and bottom are device safe-area insets today and may include future host UI obstructions.
206
-
207
- #### `oasiz.getViewportInsets(): ViewportInsets`
208
-
209
- Returns effective viewport insets in both CSS pixels and normalized percentages:
210
-
211
- ```ts
212
- const insets = oasiz.getViewportInsets();
213
- hud.style.paddingTop = `${insets.pixels.top}px`;
214
- hud.style.paddingRight = `${insets.pixels.right}px`;
215
- hud.style.paddingBottom = `${insets.pixels.bottom}px`;
216
- hud.style.paddingLeft = `${insets.pixels.left}px`;
217
- ```
218
-
219
- Percentages use the matching active viewport axis:
220
-
221
- - `top` / `bottom` are percentages of viewport height
222
- - `left` / `right` are percentages of viewport width
223
-
224
- Hosts may expose pixels via `window.getViewportInsets()` or `window.__OASIZ_VIEWPORT_INSETS__`, for example `{ top, right, bottom, left }` or `{ pixels: { top, right, bottom, left } }`. Hosts may expose percentages via `window.getViewportInsetsPercent()`, `window.__OASIZ_VIEWPORT_INSETS_PERCENT__`, or a `percent` object. Per-side globals such as `window.__OASIZ_SAFE_AREA_BOTTOM__` are also supported.
225
-
226
- #### `oasiz.getSafeAreaTop(): number`
227
-
228
- Legacy alias for `oasiz.getViewportInsets().percent.top`. Returns the top inset as a percentage of viewport height (0–100). To get pixels in JavaScript, prefer `oasiz.getViewportInsets().pixels.top`. In CSS, the percent value matches **`vh`** units (for example `12.5vh` for 12.5% of the viewport height). Unsupported hosts return `0`.
229
-
230
- ```ts
231
- const safeTopPct = oasiz.getSafeAreaTop();
232
- document.documentElement.style.setProperty("--safe-top", `${safeTopPct}vh`);
233
- ```
234
-
235
- #### `oasiz.safeAreaTop`
236
-
237
- Getter alias for `getSafeAreaTop()`.
238
-
239
- #### `oasiz.viewportInsets`
240
-
241
- Getter alias for `getViewportInsets()`.
242
-
243
- Recommended CSS pattern:
244
-
245
- ```css
246
- :root {
247
- --safe-top: 0px;
248
- }
249
-
250
- #top-bar {
251
- padding-top: var(--safe-top);
252
- }
253
- ```
254
-
255
- #### `oasiz.setLeaderboardVisible(visible: boolean): void`
256
-
257
- Show or hide the host leaderboard UI from inside the game. This only affects the leaderboard; back and social controls remain visible. Calls `window.__oasizSetLeaderboardVisible` when present.
258
-
259
- ```ts
260
- function openCustomOverlay(): void {
261
- oasiz.setLeaderboardVisible(false);
262
- }
263
-
264
- function closeCustomOverlay(): void {
265
- oasiz.setLeaderboardVisible(true);
266
- }
267
- ```
268
-
269
- Unsupported hosts safely no-op.
270
-
271
- ### Graphics performance
272
-
273
- #### `oasiz.getGraphicsPerformance(): GraphicsPerformanceMetric`
274
-
275
- Returns a recommended FPS target and suggested rendering tier:
276
-
277
- ```ts
278
- const graphics = oasiz.getGraphicsPerformance();
279
-
280
- switch (graphics.tier) {
281
- case "high":
282
- enablePostProcessing();
283
- renderer.setPixelRatio(1.5);
284
- break;
285
- case "medium":
286
- renderer.setPixelRatio(1.25);
287
- break;
288
- case "low":
289
- disableHeavyParticles();
290
- renderer.setPixelRatio(1);
291
- break;
292
- case "minimal":
293
- disableOptionalEffects();
294
- renderer.setPixelRatio(0.75);
295
- break;
296
- }
297
- ```
298
-
299
- The returned object is `{ fps, tier }`, where `fps` is the recommended render target and `tier` is `"minimal"`, `"low"`, `"medium"`, or `"high"`. Hosts can inject measured values with `window.getGraphicsPerformance()` or `window.__OASIZ_GRAPHICS_PERFORMANCE__`; otherwise the SDK estimates from browser, device, and WebGL capability signals.
300
-
301
- #### `oasiz.graphicsPerformance`
302
-
303
- Getter alias for `getGraphicsPerformance()`.
183
+ ---
304
184
 
305
- ### Lifecycle
185
+ ## Lifecycle
306
186
 
307
187
  The platform dispatches lifecycle events when the app goes to the background or returns to the foreground. Subscribe to pause game loops and audio accordingly.
308
188
 
309
- #### `oasiz.onPause(callback: () => void): Unsubscribe`
310
-
311
- #### `oasiz.onResume(callback: () => void): Unsubscribe`
189
+ ### `oasiz.onPause(callback: () => void): Unsubscribe`
190
+ ### `oasiz.onResume(callback: () => void): Unsubscribe`
312
191
 
313
192
  Both return an unsubscribe function.
314
193
 
315
194
  ```ts
316
- const offPause = oasiz.onPause(() => {
195
+ const offPause = oasiz.onPause(() => {
317
196
  this.gameLoop.stop();
318
197
  this.bgMusic.pause();
319
198
  });
@@ -328,17 +207,19 @@ offPause();
328
207
  offResume();
329
208
  ```
330
209
 
331
- ### Navigation
210
+ ---
211
+
212
+ ## Navigation
332
213
 
333
214
  Use navigation hooks when your game needs to control back behavior (Android back / web Escape) or participate in host-driven close events.
334
215
 
335
- #### `oasiz.onBackButton(callback: () => void): Unsubscribe`
216
+ ### `oasiz.onBackButton(callback: () => void): Unsubscribe`
336
217
 
337
218
  Registers a callback for platform back actions. While at least one back listener is subscribed, back actions are routed to your game instead of immediately closing it.
338
219
 
339
220
  Use this for pause menus, in-game overlays, or custom back-stack behavior.
340
221
 
341
- **If your callback throws**, the SDK calls `leaveGame()` (host close) and **rethrows** the error so you still see it in devtools or error reporting. Non-`Error` throws are normalized to an `Error` (strings become the message; otherwise `"Back button callback failed."`).
222
+ If your callback throws, Oasiz falls back to closing the game and returning the player to Oasiz home before rethrowing the error for debugging/reporting.
342
223
 
343
224
  ```ts
344
225
  const offBack = oasiz.onBackButton(() => {
@@ -353,30 +234,7 @@ const offBack = oasiz.onBackButton(() => {
353
234
  offBack();
354
235
  ```
355
236
 
356
- #### `oasiz.enableBackButtonTesting(options?: BackButtonTestingOptions): BackButtonTestingHandle`
357
-
358
- Opt-in local web helper for testing back override behavior without the Oasiz app bridge. Call it before registering `onBackButton` in local development:
359
-
360
- ```ts
361
- if (import.meta.env.DEV) {
362
- oasiz.enableBackButtonTesting();
363
- }
364
-
365
- const offBack = oasiz.onBackButton(() => {
366
- closePauseMenuOrOpenIt();
367
- });
368
- ```
369
-
370
- While a back listener is active, the helper maps Escape to the same `oasiz:back` event the app sends. By default it also traps one browser-history entry so the browser Back button dispatches `oasiz:back` instead of leaving the page.
371
-
372
- ```ts
373
- const backTest = oasiz.enableBackButtonTesting({ browserHistory: false });
374
- testBackButton.onclick = () => backTest.triggerBack();
375
- ```
376
-
377
- The returned handle also exposes `triggerLeave()` and `destroy()`. This helper is for local/dev web testing; in the app, the real bridge still owns back behavior.
378
-
379
- #### `oasiz.leaveGame(): void`
237
+ ### `oasiz.leaveGame(): void`
380
238
 
381
239
  Programmatically request the host to close the current game (for example, from a Quit button inside your game UI).
382
240
 
@@ -411,13 +269,13 @@ const offLeave = oasiz.onLeaveGame(() => {
411
269
  offLeave();
412
270
  ```
413
271
 
414
- ### Multiplayer
272
+ ---
415
273
 
416
- #### `oasiz.shareRoomCode(code: string | null, options?: { inviteOverride?: boolean })`
274
+ ## Multiplayer
417
275
 
418
- Notify the platform of the active multiplayer room so friends can join via the invite system. Pass `null` when leaving a room.
276
+ ### `oasiz.shareRoomCode(code: string | null)`
419
277
 
420
- Set `inviteOverride: true` when your game wants to hide the platform invite pill and render its own invite button/UI. The platform still tracks the room code, but your game owns the invite entry point.
278
+ Notify the platform of the active multiplayer room so friends can join via the invite system. Pass `null` when leaving a room.
421
279
 
422
280
  ```ts
423
281
  import { insertCoin, getRoomCode } from "playroomkit";
@@ -430,26 +288,7 @@ oasiz.shareRoomCode(getRoomCode());
430
288
  oasiz.shareRoomCode(null);
431
289
  ```
432
290
 
433
- ```ts
434
- // Game-owned invite UI: hide the platform pill, keep room tracking
435
- oasiz.shareRoomCode(getRoomCode(), { inviteOverride: true });
436
- ```
437
-
438
- #### `oasiz.openInviteModal(): void`
439
-
440
- Opens the platform invite-friends UI when the bridge is available. Typically used together with `shareRoomCode` (for example, your own invite button calls this).
441
-
442
- ```ts
443
- import { openInviteModal, shareRoomCode } from "@oasiz/sdk";
444
-
445
- shareRoomCode("ABCD", { inviteOverride: true });
446
-
447
- inviteButton.addEventListener("click", () => {
448
- openInviteModal();
449
- });
450
- ```
451
-
452
- #### Read-only injected values
291
+ ### Read-only injected values
453
292
 
454
293
  These are populated by the platform before the game loads. Always check for `undefined` before using.
455
294
 
@@ -463,30 +302,25 @@ if (oasiz.roomCode) {
463
302
  }
464
303
 
465
304
  // Player identity for multiplayer games
466
- const name = oasiz.playerName;
305
+ const name = oasiz.playerName;
467
306
  const avatar = oasiz.playerAvatar;
468
307
  ```
469
308
 
470
- ### Named exports
309
+ ---
310
+
311
+ ## Named exports
471
312
 
472
313
  All methods are also available as named exports if you prefer not to use the `oasiz` namespace object:
473
314
 
474
315
  ```ts
475
316
  import {
476
317
  submitScore,
477
- share,
318
+ emitScoreConfig,
478
319
  triggerHaptic,
479
320
  loadGameState,
480
321
  saveGameState,
481
322
  flushGameState,
482
323
  shareRoomCode,
483
- openInviteModal,
484
- enableLogOverlay,
485
- enableBackButtonTesting,
486
- getGraphicsPerformance,
487
- getSafeAreaTop,
488
- getViewportInsets,
489
- setLeaderboardVisible,
490
324
  onPause,
491
325
  onResume,
492
326
  onBackButton,
@@ -499,227 +333,18 @@ import {
499
333
  } from "@oasiz/sdk";
500
334
  ```
501
335
 
502
- ### TypeScript types
503
-
504
- ```ts
505
- import type {
506
- BackButtonTestingHandle,
507
- BackButtonTestingOptions,
508
- GameState,
509
- GraphicsPerformanceMetric,
510
- GraphicsPerformanceTier,
511
- HapticType,
512
- LogOverlayEntry,
513
- LogOverlayHandle,
514
- LogOverlayLevel,
515
- LogOverlayOptions,
516
- ShareRequest,
517
- ShareRoomCodeOptions,
518
- Unsubscribe,
519
- } from "@oasiz/sdk";
520
- ```
521
-
522
336
  ---
523
337
 
524
- ## Unity WebGL SDK
525
-
526
- C# API and **WebGL-only** `OasizBridge.jslib` live in this repository at **`packages/OasizSDK/`**. Copy the **`OasizSDK`** folder into your Unity project under **`Assets/`** (for example `Assets/OasizSDK`).
527
-
528
- ### Setup
529
-
530
- 1. Copy `packages/OasizSDK` from this repo into `Assets/OasizSDK`.
531
- 2. Ensure the **WebGL** platform is selected for release builds; the `.jslib` under `Runtime/Plugins/WebGL/` is included automatically for WebGL.
532
- 3. Add an **`OasizSDK`** component to a persistent GameObject early (for example a bootstrap scene), **or** rely on `OasizSDK.Instance` which creates a `DontDestroyOnLoad` object. The component registers listeners for `oasiz:pause`, `oasiz:resume`, `oasiz:back`, and `oasiz:leave` via `SendMessage`.
533
-
534
- ### Quick start
535
-
536
- ```csharp
537
- using Oasiz;
538
- using UnityEngine;
539
-
540
- public class GameManager : MonoBehaviour
541
- {
542
- void Start()
543
- {
544
- // Ensure the singleton is initialized early
545
- _ = OasizSDK.Instance;
546
-
547
- // Subscribe to lifecycle events
548
- OasizSDK.OnPause += OnPause;
549
- OasizSDK.OnResume += OnResume;
550
-
551
- // Offset UI for the host's top safe area (0–100 percent of Screen.height)
552
- float safeTopPct = OasizSDK.SafeAreaTop;
553
- float safeTopPx = safeTopPct / 100f * Screen.height;
554
- Debug.Log($"Safe area top: {safeTopPx}px ({safeTopPct}% of height)");
555
-
556
- // Pick visual settings for the current device
557
- GraphicsPerformanceMetric graphics = OasizSDK.GetGraphicsPerformance();
558
- Debug.Log($"Graphics tier: {graphics.tier} ({graphics.fps} FPS target)");
559
-
560
- // Emit score normalization anchors
561
- OasizSDK.EmitScoreConfig(new ScoreConfig(
562
- new ScoreAnchor(10, 100),
563
- new ScoreAnchor(30, 300),
564
- new ScoreAnchor(75, 600),
565
- new ScoreAnchor(200, 950)
566
- ));
567
- }
568
-
569
- void OnGameOver(int finalScore)
570
- {
571
- OasizSDK.SubmitScore(finalScore);
572
- OasizSDK.FlushGameState();
573
- OasizSDK.SetLeaderboardVisible(true);
574
- }
575
-
576
- void OnGameplayStart()
577
- {
578
- OasizSDK.SetLeaderboardVisible(false);
579
- }
580
-
581
- void OnPause() => Time.timeScale = 0f;
582
- void OnResume() => Time.timeScale = 1f;
583
-
584
- void OnDestroy()
585
- {
586
- OasizSDK.OnPause -= OnPause;
587
- OasizSDK.OnResume -= OnResume;
588
- }
589
- }
590
- ```
591
-
592
- ### API parity (TypeScript → C#)
593
-
594
- | HTML5 (`@oasiz/sdk`) | Unity (`Oasiz` namespace) |
595
- | --- | --- |
596
- | `oasiz.submitScore(n)` | `OasizSDK.SubmitScore(int)` |
597
- | `oasiz.triggerHaptic(type)` | `OasizSDK.TriggerHaptic(HapticType)` |
598
- | `oasiz.loadGameState()` | `OasizSDK.LoadGameState()` → `Dictionary<string, object>` |
599
- | `oasiz.saveGameState(obj)` | `OasizSDK.SaveGameState(Dictionary<string, object>)` |
600
- | `oasiz.flushGameState()` | `OasizSDK.FlushGameState()` |
601
- | `oasiz.getViewportInsets()` / `viewportInsets` | `OasizSDK.GetViewportInsets()` (`ViewportInsets`, each side 0–100, % of matching viewport axis) |
602
- | `oasiz.getSafeAreaTop()` / `safeAreaTop` | `OasizSDK.GetSafeAreaTop()` / `OasizSDK.SafeAreaTop` (`float`, 0–100, % of viewport height; legacy alias for top viewport inset) |
603
- | `oasiz.setLeaderboardVisible(v)` | `OasizSDK.SetLeaderboardVisible(bool)` |
604
- | `oasiz.getGraphicsPerformance()` / `graphicsPerformance` | `OasizSDK.GetGraphicsPerformance()` / `OasizSDK.GraphicsPerformance` (`GraphicsPerformanceMetric`, recommended FPS plus `minimal` / `low` / `medium` / `high`) |
605
- | `oasiz.onPause` / `onResume` | `OasizSDK.OnPause` / `OnResume` static events |
606
- | `oasiz.onBackButton` | `OasizSDK.OnBackButton` or `SubscribeBackButton(Action)` (reference-counts `__oasizSetBackOverride`) |
607
- | `oasiz.enableBackButtonTesting()` | `OasizSDK.EnableBackButtonTesting()` (WebGL local/dev helper for Escape/browser Back testing) |
608
- | `oasiz.onLeaveGame` | `OasizSDK.OnLeaveGame` |
609
- | `oasiz.leaveGame()` | `OasizSDK.LeaveGame()` |
610
- | `oasiz.share(request)` | `OasizSDK.Share(ShareRequest)` |
611
- | `oasiz.shareRoomCode` | `OasizSDK.ShareRoomCode(string, ShareRoomCodeOptions)` |
612
- | `oasiz.openInviteModal()` | `OasizSDK.OpenInviteModal()` |
613
- | `oasiz.gameId` / `roomCode` / ... | `OasizSDK.GameId` / `RoomCode` / `PlayerName` / `PlayerAvatar` |
614
- | -- | `OasizSDK.EmitScoreConfig(ScoreConfig)` → `window.emitScoreConfig` (Unity-only helper for normalized score UI) |
615
- | `oasiz.enableLogOverlay` | `OasizSDK.EnableLogOverlay(LogOverlayOptions)` (see note below) |
616
- | -- | `OasizSDK.AppendLogOverlay(level, message, stackTrace)` (see note below) |
617
-
618
- ### Local back-button testing (Unity WebGL)
619
-
620
- For local WebGL builds outside the Oasiz app, install the dev bridge before testing your subscribed back handler:
621
-
622
- ```csharp
623
- #if DEVELOPMENT_BUILD || UNITY_EDITOR
624
- OasizSDK.EnableBackButtonTesting();
625
- #endif
626
-
627
- Action unsubscribeBack = OasizSDK.SubscribeBackButton(() =>
628
- {
629
- TogglePauseMenu();
630
- });
631
- ```
632
-
633
- While the override is active, Escape and the browser Back button dispatch the same `oasiz:back` event that the app bridge sends. You can disable either input with `OasizSDK.EnableBackButtonTesting(keyboard: false)` or `OasizSDK.EnableBackButtonTesting(browserHistory: false)`.
634
-
635
- ### Share (Unity)
636
-
637
- HTML5 **`oasiz.share`** returns a **Promise** you can `await`. Unity **`OasizSDK.Share(ShareRequest)`** returns **`void`**: C# validation throws **`ArgumentException`** with the same rules as TypeScript (at least one of text, score, or image; non-negative integer score; `http(s)` or `data:image/...;base64,...` image). The call forwards JSON to **`window.__oasizShareRequest`**. If the host promise rejects, the **WebGL `.jslib` logs the error** to the browser console.
638
-
639
- ```csharp
640
- OasizSDK.Share(new ShareRequest
641
- {
642
- Text = "Beat this run!",
643
- Score = 1200,
644
- Image = "https://example.com/card.png",
645
- });
646
- ```
647
-
648
- ### Types
649
-
650
- ```csharp
651
- // Haptic feedback intensity
652
- public enum HapticType { Light, Medium, Heavy, Success, Error }
653
-
654
- // Score normalization (exactly 4 anchors required)
655
- public struct ScoreAnchor { public int raw; public int normalized; }
656
- public struct ScoreConfig { public ScoreAnchor[] anchors; }
657
-
658
- // Host share sheet (text / score / image URL or data URL)
659
- public class ShareRequest
660
- {
661
- public string Text { get; set; }
662
- public int? Score { get; set; }
663
- public string Image { get; set; }
664
- }
665
-
666
- // Multiplayer invite options
667
- public class ShareRoomCodeOptions { public bool InviteOverride { get; set; } }
668
-
669
- // Log overlay configuration
670
- public class LogOverlayOptions
671
- {
672
- public bool Enabled { get; set; } = true;
673
- public bool Collapsed { get; set; } = false;
674
- public int MaxEntries { get; set; } = 200;
675
- public string Title { get; set; } = "SDK Logs";
676
- }
677
-
678
- // Log overlay lifecycle handle
679
- public class LogOverlayHandle
680
- {
681
- public void Clear();
682
- public void Hide();
683
- public void Show();
684
- public bool IsVisible();
685
- public void Destroy();
686
- }
687
- ```
688
-
689
- ### Back button and errors
690
-
691
- Matching the HTML5 SDK: if any **`OnBackButton`** handler throws, **`OasizSDK.LeaveGame()`** is invoked and the **original exception is rethrown** (`throw;` preserves the stack trace). Use **`SubscribeBackButton`** when you want an unsubscribe delegate; you can also use `OnBackButton +=` / `-=` directly.
692
-
693
- ```csharp
694
- // Subscribe with automatic unsubscribe support
695
- var offBack = OasizSDK.SubscribeBackButton(() =>
696
- {
697
- if (isPaused)
698
- Resume();
699
- else
700
- Pause();
701
- });
338
+ ## TypeScript types
702
339
 
703
- // Unsubscribe when no longer needed
704
- offBack();
340
+ ```ts
341
+ import type { HapticType, ScoreConfig, ScoreAnchor, GameState } from "@oasiz/sdk";
705
342
  ```
706
343
 
707
- ### Editor vs WebGL builds
708
-
709
- In the **Unity Editor**, bridge calls are mostly **logged** and return safe defaults (for example safe area `0`, `null` platform IDs). Real host integration applies to **WebGL player** builds running inside Oasiz.
710
-
711
- ### Log overlay (Unity)
712
-
713
- The C# API for the log overlay exists for API compatibility, but the **default `OasizBridge.jslib` in this repo does not inject DOM UI** — `EnableLogOverlay` / `AppendLogOverlay` are no-ops at the JavaScript layer. Use Unity's console and device logs for debugging unless you replace or extend the `.jslib` on your side.
714
-
715
- `AppendLogOverlay(level, message, stackTrace)` lets you pipe `Debug.Log` output into the overlay manually, since many embedded WebViews do not route Unity player logs through `console.log`. Valid levels: `"debug"`, `"log"`, `"info"`, `"warn"`, `"error"`.
716
-
717
344
  ---
718
345
 
719
346
  ## Local development
720
347
 
721
- ### HTML5 / TypeScript
722
-
723
348
  All methods safely no-op when the platform bridges are not injected. In development mode a console warning is logged so you know the call was made:
724
349
 
725
350
  ```
@@ -727,7 +352,3 @@ All methods safely no-op when the platform bridges are not injected. In developm
727
352
  ```
728
353
 
729
354
  No crashes, no special setup required for local dev.
730
-
731
- ### Unity WebGL
732
-
733
- The `.jslib` logs warnings when `window.*` bridges are missing (for example `submitScore`, `__oasizLeaveGame`). The Editor path avoids calling native plugins and prints `Debug.Log` for most operations instead.