Skip to content

Commit 87a9d15

Browse files
committed
chore: add webgl template
1 parent e0c61d3 commit 87a9d15

19 files changed

+602
-19
lines changed

sample/Assets/Scripts/SelectAuthMethodScript.cs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@ public class SelectAuthMethodScript : MonoBehaviour
1212
[SerializeField] private Text Output;
1313
[SerializeField] private Button UseDeviceCodeAuthButton;
1414
[SerializeField] private Button UsePKCEButton;
15-
string REDIRECT_URI = null;
16-
string LOGOUT_REIDIRECT_URI = "https://www.immutable.com";
15+
string redirectUri = null;
16+
string logoutRedirectUri = "https://www.immutable.com";
17+
string clientId = "ZJL7JvetcDFBNDlgRs5oJoxuAUUl6uQj";
1718
#pragma warning restore CS8618
1819
void Start()
1920
{
2021
#if UNITY_WEBGL
21-
string url = Application.absoluteURL;
22-
Uri uri = new Uri(url);
23-
string scheme = uri.Scheme;
24-
string hostWithPort = uri.IsDefaultPort ? uri.Host : $"{uri.Host}:{uri.Port}";
25-
string fullPath = uri.AbsolutePath.EndsWith("/") ? uri.AbsolutePath : uri.AbsolutePath.Substring(0, uri.AbsolutePath.LastIndexOf('/') + 1);
22+
string url = Application.absoluteURL;
23+
Uri uri = new Uri(url);
24+
string scheme = uri.Scheme;
25+
string hostWithPort = uri.IsDefaultPort ? uri.Host : $"{uri.Host}:{uri.Port}";
26+
string fullPath = uri.AbsolutePath.EndsWith("/") ? uri.AbsolutePath : uri.AbsolutePath.Substring(0, uri.AbsolutePath.LastIndexOf('/') + 1);
2627

27-
REDIRECT_URI = $"{scheme}://{hostWithPort}{fullPath}callback.html";
28-
LOGOUT_REIDIRECT_URI = $"{scheme}://{hostWithPort}{fullPath}logout.html";
28+
redirectUri = $"{scheme}://{hostWithPort}{fullPath}callback.html";
29+
logoutRedirectUri = $"{scheme}://{hostWithPort}{fullPath}logout.html";
30+
clientId = "UnB98ngnXIZIEJWGJOjVe1BpCx5ix7qc";
2931
#endif
3032

3133
// Determine if PKCE is supported based on the platform
@@ -34,7 +36,7 @@ void Start()
3436
// If PKCE is not supported, initialise Passport to use Device Code Auth
3537
if (!SampleAppManager.SupportsPKCE)
3638
{
37-
InitialisePassport(redirectUri: REDIRECT_URI, logoutRedirectUri: LOGOUT_REIDIRECT_URI);
39+
InitialisePassport();
3840
}
3941
}
4042

@@ -56,7 +58,7 @@ private bool IsPKCESupported()
5658
public void UseDeviceCodeAuth()
5759
{
5860
SampleAppManager.UsePKCE = false;
59-
InitialisePassport(redirectUri: REDIRECT_URI, logoutRedirectUri: LOGOUT_REIDIRECT_URI);
61+
InitialisePassport();
6062
}
6163

6264
/// <summary>
@@ -65,17 +67,13 @@ public void UseDeviceCodeAuth()
6567
public void UsePKCE()
6668
{
6769
SampleAppManager.UsePKCE = true;
68-
InitialisePassport(redirectUri: REDIRECT_URI, logoutRedirectUri: LOGOUT_REIDIRECT_URI);
70+
InitialisePassport();
6971
}
7072

7173
/// <summary>
7274
/// Initialises Passport.
7375
/// </summary>
74-
/// <param name="redirectUri">(Android, iOS and macOS only) The URL to which auth will redirect the browser after
75-
/// authorisation has been granted by the user</param>
76-
/// <param name="logoutRedirectUri">The URL to which auth will redirect the browser
77-
/// after log out is complete</param>
78-
private async void InitialisePassport(string redirectUri = null, string logoutRedirectUri = null)
76+
private async void InitialisePassport()
7977
{
8078
ShowOutput("Initialising Passport...");
8179

@@ -85,7 +83,6 @@ private async void InitialisePassport(string redirectUri = null, string logoutRe
8583
Passport.LogLevel = LogLevel.Info;
8684

8785
// Initialise Passport
88-
string clientId = "ZJL7JvetcDFBNDlgRs5oJoxuAUUl6uQj";
8986
string environment = Immutable.Passport.Model.Environment.SANDBOX;
9087

9188
Passport passport = await Passport.Init(clientId, environment, redirectUri, logoutRedirectUri);

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

Lines changed: 351 additions & 0 deletions
Large diffs are not rendered by default.
Loading
Binary file not shown.
Loading
Loading
Loading
Loading
Loading
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
body { padding: 0; margin: 0 }
2+
#unity-container { position: absolute }
3+
#unity-container.unity-desktop { left: 50%; top: 50%; transform: translate(-50%, -50%) }
4+
#unity-container.unity-mobile { position: fixed; width: 100%; height: 100% }
5+
#unity-canvas { background: {{{ BACKGROUND_COLOR }}} }
6+
.unity-mobile #unity-canvas { width: 100%; height: 100% }
7+
#unity-loading-bar { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); display: none }
8+
#unity-logo { width: 154px; height: 130px; background: url('unity-logo-{{{ SPLASH_SCREEN_STYLE.toLowerCase() }}}.png') no-repeat center }
9+
#unity-progress-bar-empty { width: 141px; height: 18px; margin-top: 10px; margin-left: 6.5px; background: url('progress-bar-empty-{{{ SPLASH_SCREEN_STYLE.toLowerCase() }}}.png') no-repeat center }
10+
#unity-progress-bar-full { width: 0%; height: 18px; margin-top: 10px; background: url('progress-bar-full-{{{ SPLASH_SCREEN_STYLE.toLowerCase() }}}.png') no-repeat center }
11+
#unity-footer { position: relative }
12+
.unity-mobile #unity-footer { display: none }
13+
#unity-webgl-logo { float:left; width: 204px; height: 38px; background: url('webgl-logo.png') no-repeat center }
14+
#unity-build-title { float: right; margin-right: 10px; line-height: 38px; font-family: arial; font-size: 18px }
15+
#unity-fullscreen-button { cursor:pointer; float: right; width: 38px; height: 38px; background: url('fullscreen-button.png') no-repeat center }
16+
#unity-warning { position: absolute; left: 50%; top: 5%; transform: translate(-50%); background: white; padding: 10px; display: none }
Loading
Loading
Loading
Loading
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 Runner</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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 Runner</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+
<canvas id="unity-canvas"></canvas>
15+
<div id="unity-loading-bar">
16+
<div id="unity-logo"></div>
17+
<div id="unity-progress-bar-empty">
18+
<div id="unity-progress-bar-full"></div>
19+
</div>
20+
</div>
21+
<div id="unity-footer">
22+
<div id="unity-webgl-logo"></div>
23+
<div id="unity-fullscreen-button"></div>
24+
<div id="unity-build-title">Immutable Runner</div>
25+
</div>
26+
</div>
27+
<script>
28+
var buildUrl = "Build";
29+
var loaderUrl = buildUrl + "/webgl.loader.js";
30+
var config = {
31+
dataUrl: buildUrl + "/webgl.data",
32+
frameworkUrl: buildUrl + "/webgl.framework.js",
33+
codeUrl: buildUrl + "/webgl.wasm",
34+
streamingAssetsUrl: "StreamingAssets",
35+
companyName: "Immutable",
36+
productName: "Immutable Runner",
37+
productVersion: "0.1.0",
38+
};
39+
40+
var container = document.querySelector("#unity-container");
41+
var canvas = document.querySelector("#unity-canvas");
42+
var loadingBar = document.querySelector("#unity-loading-bar");
43+
var progressBarFull = document.querySelector("#unity-progress-bar-full");
44+
var fullscreenButton = document.querySelector("#unity-fullscreen-button");
45+
var width0 = "960px";
46+
var height0 = "600px";
47+
48+
if (/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)) {
49+
container.className = "unity-mobile";
50+
config.devicePixelRatio = 1;
51+
} else {
52+
canvas.style.width = "960px";
53+
canvas.style.height = "600px";
54+
}
55+
loadingBar.style.display = "block";
56+
57+
document.addEventListener(
58+
'fullscreenchange',
59+
function() {
60+
var p = document.getElementById('unity-container');
61+
var c = document.getElementById('unity-canvas');
62+
if (document.fullscreenElement) {
63+
width0 = c.style.width;
64+
height0 = c.style.height;
65+
setTimeout(
66+
function() {
67+
c.style.width = getComputedStyle(p).width;
68+
c.style.height = getComputedStyle(p).height;
69+
},
70+
250);
71+
} else {
72+
c.style.width = width0;
73+
c.style.height = height0;
74+
}
75+
});
76+
var script = document.createElement("script");
77+
script.src = loaderUrl;
78+
script.onload = () => {
79+
createUnityInstance(canvas, config, (progress) => {
80+
progressBarFull.style.width = 100 * progress + "%";
81+
}).then((unityInstance) => {
82+
window.unityInstance = unityInstance;
83+
loadingBar.style.display = "none";
84+
fullscreenButton.onclick = () => {
85+
var p = document.getElementById('unity-container');
86+
var c = document.getElementById('unity-canvas');
87+
c.requestFullscreen = () => {
88+
p.requestFullscreen();
89+
};
90+
unityInstance.SetFullscreen(1);
91+
};
92+
}).catch((message) => {
93+
alert(message);
94+
});
95+
};
96+
document.body.appendChild(script);
97+
</script>
98+
</body>
99+
</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 Runner</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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
console.log(url, window.location.origin, window.location.pathname);
45+
console.log(baseUrl);
46+
const host = window.location.origin.includes('localhost') ? window.location.origin : baseUrl;
47+
this.iframe(name).attr('loaded', 'false')[0].contentWindow.location.replace(host + url);
48+
},
49+
50+
evaluateJS: function (name, js) {
51+
$iframe = this.iframe(name);
52+
if ($iframe.attr('loaded') === 'true') {
53+
$iframe[0].contentWindow.eval(js);
54+
} else {
55+
$iframe.on('load', function(){
56+
$(this)[0].contentWindow.eval(js);
57+
});
58+
}
59+
},
60+
61+
destroy: function (name) {
62+
this.iframe(name).parent().parent().remove();
63+
},
64+
65+
iframe: function (name) {
66+
return $('#webview_' + name);
67+
},
68+
69+
launchAuthURL: function(name, url) {
70+
window.open(url, '_blank', 'width=460,height=660');
71+
},
72+
};

sample/ProjectSettings/ProjectSettings.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ PlayerSettings:
762762
webGLTemplate: PROJECT:unity-webview
763763
webGLAnalyzeBuildSize: 0
764764
webGLUseEmbeddedResources: 0
765-
webGLCompressionFormat: 2
765+
webGLCompressionFormat: 0
766766
webGLWasmArithmeticExceptions: 0
767767
webGLLinkerTarget: 1
768768
webGLThreadsSupport: 0

0 commit comments

Comments
 (0)