Skip to content

Commit c0ad3cd

Browse files
authored
Merge pull request #298 from immutable/feature/dx-3321-webgl-quickstart
[DX-3321]webgl quick start guide
2 parents 3bbb2c1 + 86c0e3c commit c0ad3cd

27 files changed

+340
-3
lines changed

.github/workflows/linter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
VALIDATE_ALL_CODEBASE: true
5858
DEFAULT_BRANCH: main
5959
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
60-
FILTER_REGEX_EXCLUDE: (.*src/Packages/Passport/Runtime/ThirdParty/.*|.*src/Packages/Passport/Runtime/Resources/.*|.*Plugins/.*|.*src/Packages/Passport/Runtime/Assets/ImmutableAndroid.androidlib/.*|.*src/Packages/Orderbook|.*sample)
60+
FILTER_REGEX_EXCLUDE: (.*src/Packages/Passport/Runtime/ThirdParty/.*|.*src/Packages/Passport/Runtime/Resources/.*|.*Plugins/.*|.*src/Packages/Passport/Runtime/Assets/ImmutableAndroid.androidlib/.*|.*src/Packages/Orderbook|.*sample|.*src/Packages/Passport/WebGLTemplates)
6161
VALIDATE_MARKDOWN: false
6262
VALIDATE_GITLEAKS: false
6363
VALIDATE_JSCPD: false

WebGL.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Implementing Immutable Unity SDK for WebGL
2+
3+
This guide provides focused instructions for implementing the Unity-Immutable-SDK in WebGL projects.
4+
5+
For general information on the SDK, please refer to
6+
the [Immutable Unity SDK documentation](https://docs.immutable.com/sdks/zkEVM/unity).
7+
8+
Live example can be found at https://immutable.github.io/unity-immutable-sdk/sample/webgl
9+
10+
## WebGL Template Setup
11+
12+
WebGL template is a configuration setting that lets you control the appearance of the HTML page, so that you can: test, demonstrate, and preview your WebGL application in a browser.
13+
14+
A WebGL Template is always needed for creating a WebGL application and it will always be stored within **Assets > WebGL Templates** to be used. You can refer to [Unity: WebGL Templates](https://docs.unity3d.com/Manual/webgl-templates.html) for more information.
15+
16+
A Custom WebGL Template is required to implement the Immutable Unity SDK in WebGL projects. The easiest way to create a new custom WebGL template is to make a copy of the built-in Default or Minimal templates, which are stored in corresponding subfolders under <Unity Installation> > PlaybackEngines > WebGLSupport > BuildTools > WebGLTemplates.
17+
18+
Every Unity Project includes these templates by default. Copy a template and place it in your own **Assets > WebGLTemplates** folder, and rename it to something meaningful so you can identify your template later.
19+
20+
Once you have created your own template, copy the following files from Passport package into the **Assets > WebGLTemplates** folder:
21+
- `Packages/Immutable Passport/WebGLTemplates~/unity-webview.js`
22+
- `Packages/Immutable Passport/WebGLTemplates~/callback.html`
23+
- `Packages/Immutable Passport/WebGLTemplates~/logout.html`
24+
- `Packages/Immutable Passport/Runtime/Resources/index.html` > `Passport/index.html`
25+
26+
3. Add the following script tag to the `index.html` in WebGL Templates:
27+
```html
28+
<script src="unity-webview.js"></script>
29+
```
30+
31+
## PKCE Login and Logout Implementation
32+
> [!IMPORTANT]
33+
> WebGL supports only PKCE flow.
34+
35+
Follow these steps for implementation:
36+
> [!NOTE]
37+
> You can rename `callback.html` and `logout.html` to suit your project needs.
38+
1. Define a deep link scheme for your game:
39+
- Redirect URL: https://game.domain.com/callback.html
40+
- Logout URL: https://game.domain.com/logout.html
41+
42+
2. Configure Immutable Hub:
43+
- Login to [Immutable Hub](https://hub.immutable.com)
44+
- Add these deep links to your client's Redirect URLs and Logout URLs
45+
46+
3. Update Passport initialisation:
47+
```csharp
48+
using System.Collections;
49+
using System.Collections.Generic;
50+
using UnityEngine;
51+
using Immutable.Passport;
52+
53+
public class InitPassport : MonoBehaviour
54+
{
55+
private Passport passport;
56+
57+
async void Start()
58+
{
59+
string clientId = "YOUR_IMMUTABLE_CLIENT_ID";
60+
string environment = Immutable.Passport.Model.Environment.SANDBOX;
61+
string redirectUri = "https://game.domain.com/callback.html";
62+
string logoutRedirectUri = "https://game.domain.com/logout.html";
63+
passport = await Passport.Init(clientId, environment, redirectUri, logoutRedirectUri);
64+
}
65+
}
66+
```
67+
68+
## Building for WebGL
69+
1. Go to File > Build Settings
70+
2. Select WebGL as the target platform
71+
3. Click "Build And Run"
72+
4. Choose a directory for the build output
73+
Your WebGL application will open in your default web browser once the build is complete.
74+
75+
For a complete working example, refer to the sample application in the SDK repository.

sample/Assets/WebGLTemplates.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/Passport.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/Passport/index.html.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/TemplateData.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/TemplateData/MemoryProfiler.png.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/TemplateData/favicon.ico.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/TemplateData/fullscreen-button.png.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/TemplateData/progress-bar-empty-dark.png.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/TemplateData/progress-bar-empty-light.png.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/TemplateData/progress-bar-full-dark.png.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/TemplateData/progress-bar-full-light.png.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/TemplateData/style.css.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/TemplateData/unity-logo-dark.png.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/TemplateData/unity-logo-light.png.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/TemplateData/webgl-logo.png.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/TemplateData/webmemd-icon.png.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/callback.html.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/index.html.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/logout.html.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/Assets/WebGLTemplates/unity-webview/unity-webview.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ var unityWebView =
4141

4242
loadURL: function(name, url) {
4343
var baseUrl = window.location.origin + window.location.pathname.replace(/\/[^\/]*$/, "");
44-
console.log(url, window.location.origin, window.location.pathname);
45-
console.log(baseUrl);
4644
const host = window.location.origin.includes('localhost') ? window.location.origin : baseUrl;
4745
this.iframe(name).attr('loaded', 'false')[0].contentWindow.location.replace(host + url);
4846
},

sample/Assets/WebGLTemplates/unity-webview/unity-webview.js.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en-us">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6+
<title>Unity WebGL Player | Immutable SDK Sample App</title>
7+
<link rel="shortcut icon" href="TemplateData/favicon.ico">
8+
<link rel="stylesheet" href="TemplateData/style.css">
9+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
10+
<script src="unity-webview.js"></script>
11+
</head>
12+
<body>
13+
<div id="unity-container" class="unity-desktop">
14+
<div id="unity-build-title">Logging in</div>
15+
</div>
16+
<script>
17+
window.opener.postMessage({
18+
type: 'callback',
19+
url: window.location.href,
20+
});
21+
window.close();
22+
</script>
23+
</body>
24+
</html>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en-us">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6+
<title>Unity WebGL Player | Immutable SDK Sample App</title>
7+
<link rel="shortcut icon" href="TemplateData/favicon.ico">
8+
<link rel="stylesheet" href="TemplateData/style.css">
9+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
10+
<script src="unity-webview.js"></script>
11+
</head>
12+
<body>
13+
<div id="unity-container" class="unity-desktop">
14+
<div id="unity-build-title">Logging out</div>
15+
</div>
16+
<script>
17+
window.opener.postMessage({
18+
type: 'logout',
19+
url: window.location.href,
20+
});
21+
window.close();
22+
</script>
23+
</body>
24+
</html>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var unityWebView =
2+
{
3+
loaded: [],
4+
5+
init: function(name) {
6+
$containers = $('.webviewContainer');
7+
if ($containers.length === 0) {
8+
$('<div style="position: absolute; left: 0px; width: 100%; height: 100%; top: 0px; pointer-events: none;"><div class="webviewContainer" style="overflow: hidden; position: relative; width: 100%; height: 100%; z-index: 1;"></div></div>')
9+
.appendTo($('#unity-container'));
10+
}
11+
var $last = $('.webviewContainer:last');
12+
var clonedTop = parseInt($last.css('top')) - 100;
13+
var $clone = $last.clone().insertAfter($last).css('top', clonedTop + '%');
14+
var $iframe =
15+
$('<iframe style="position:relative; width:100%; height100%; border-style:none; display:none; pointer-events:auto;"></iframe>')
16+
.attr('id', 'webview_' + name)
17+
.appendTo($last)
18+
$iframe.on('load', function () {
19+
$(this).attr('loaded', 'true');
20+
21+
var js = `window.Unity = {
22+
call:function(msg) {
23+
parent.unityWebView.sendMessage('${name}', msg);
24+
}
25+
};`;
26+
27+
this.contentWindow.eval(js);
28+
29+
window.addEventListener('message', function(event) {
30+
if ((event.data.type === 'callback' || event.data.type === 'logout') && event.isTrusted) {
31+
unityInstance.SendMessage(name, 'CallOnAuth', event.data.url);
32+
}
33+
}, false);
34+
});
35+
return $iframe;
36+
},
37+
38+
sendMessage: function (name, message) {
39+
unityInstance.SendMessage(name, "CallFromJS", message);
40+
},
41+
42+
loadURL: function(name, url) {
43+
var baseUrl = window.location.origin + window.location.pathname.replace(/\/[^\/]*$/, "");
44+
const host = window.location.origin.includes('localhost') ? window.location.origin : baseUrl;
45+
this.iframe(name).attr('loaded', 'false')[0].contentWindow.location.replace(host + url);
46+
},
47+
48+
evaluateJS: function (name, js) {
49+
$iframe = this.iframe(name);
50+
if ($iframe.attr('loaded') === 'true') {
51+
$iframe[0].contentWindow.eval(js);
52+
} else {
53+
$iframe.on('load', function(){
54+
$(this)[0].contentWindow.eval(js);
55+
});
56+
}
57+
},
58+
59+
destroy: function (name) {
60+
this.iframe(name).parent().parent().remove();
61+
},
62+
63+
iframe: function (name) {
64+
return $('#webview_' + name);
65+
},
66+
67+
launchAuthURL: function(name, url) {
68+
window.open(url, '_blank', 'width=460,height=660');
69+
},
70+
};

0 commit comments

Comments
 (0)