Skip to content

Commit 18d9fd5

Browse files
committed
Add a WebGL bridge gameobject that makes it easy to access unity functionality through the browser console
1 parent 1fc31df commit 18d9fd5

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="WebGlBridge.cs">
3+
// Copyright (c) 2021 Johannes Deml. All rights reserved.
4+
// </copyright>
5+
// <author>
6+
// Johannes Deml
7+
// public@deml.io
8+
// </author>
9+
// --------------------------------------------------------------------------------------------------------------------
10+
11+
using System;
12+
using UnityEngine;
13+
14+
namespace Supyrb
15+
{
16+
/// <summary>
17+
/// Bridge to Unity to access unity logic through the browser console
18+
/// </summary>
19+
public class WebGlBridge : MonoBehaviour
20+
{
21+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
22+
private static void OnBeforeSceneLoadRuntimeMethod()
23+
{
24+
var bridgeInstance = new GameObject("WebGL");
25+
DontDestroyOnLoad(bridgeInstance);
26+
bridgeInstance.AddComponent<WebGlBridge>();
27+
}
28+
29+
private void Start()
30+
{
31+
Debug.Log("Unity WebGL Bridge ready -> Run 'unityGame.SendMessage(\"WebGL\", \"Help\")' in the browser console to see usage");
32+
}
33+
34+
public void Help()
35+
{
36+
Debug.Log("Available unity interfaces:\n" +
37+
"- LogMemory() -> logs current memory\n" +
38+
"- SetApplicationRunInBackground(int runInBackground) -> Application.runInBackground\n" +
39+
"- SetApplicationTargetFrameRate(int targetFrameRate) -> Application.targetFrameRate\n" +
40+
"- SetTimeFixedDeltaTime(float fixedDeltaTime) -> Time.fixedDeltaTime\n" +
41+
"- SetTimeTimeScale(float timeScale) -> Time.timeScale\n" +
42+
"\nRun a command through 'unityGame.SendMessage(\"WebGL\", \"COMMAND_NAME\",PARAMETER)'");
43+
}
44+
45+
/// <summary>
46+
/// Logs the current memory usage
47+
/// Browser Usage: <code>unityGame.SendMessage("WebGL","LogMemory");</code>
48+
/// </summary>
49+
public void LogMemory()
50+
{
51+
WebGlPlugins.LogMemory();
52+
}
53+
54+
/// <summary>
55+
/// Sets if the application should run while not focused.
56+
/// It is in background while another tab is focused or the console is focused
57+
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetApplicationRunInBackground", 1);</code>
58+
/// </summary>
59+
/// <param name="runInBackground">1 if it should run in background</param>
60+
public void SetApplicationRunInBackground(int runInBackground)
61+
{
62+
Application.runInBackground = runInBackground == 1;
63+
}
64+
65+
/// <summary>
66+
/// Sets the rendering frame rate, see <see cref="Application.targetFrameRate"/>
67+
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetApplicationTargetFrameRate", 15);</code>
68+
/// </summary>
69+
/// <param name="targetFrameRate">frame rate to render in</param>
70+
public void SetApplicationTargetFrameRate(int targetFrameRate)
71+
{
72+
Application.targetFrameRate = targetFrameRate;
73+
}
74+
75+
/// <summary>
76+
/// Sets the interval for fixed time updates, see <see cref="Time.fixedDeltaTime"/>
77+
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetTimeFixedDeltaTime", 0.02);</code>
78+
/// </summary>
79+
/// <param name="fixedDeltaTime"></param>
80+
public void SetTimeFixedDeltaTime(float fixedDeltaTime)
81+
{
82+
Time.fixedDeltaTime = fixedDeltaTime;
83+
}
84+
85+
/// <summary>
86+
/// Sets the global timeScale, see <see cref="Time.timeScale"/>
87+
/// Somehow the timescale also affects the physics refresh rate on WebGL
88+
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetTimeTimeScale", 0.2);</code>
89+
/// </summary>
90+
/// <param name="timeScale">new timescale value</param>
91+
public void SetTimeTimeScale(float timeScale)
92+
{
93+
Time.timeScale = timeScale;
94+
}
95+
}
96+
}

Assets/Plugins/WebGL/WebGlBridge.cs.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.

Assets/WebGLTemplates/Develop/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,14 @@
8989
var loadingBar = document.querySelector("#unity-loading-bar");
9090
var progressBar = document.querySelector("#unity-progress-bar");
9191

92+
var unityGame; // This can be used to access the application with .SendMessage() commands
9293
var script = document.createElement("script");
9394
script.src = loaderUrl;
9495
script.onload = function() {
9596
createUnityInstance(canvas, config, function(progress) {
9697
progressBar.style.width = 100 * progress + "%";
9798
}).then(function(unityInstance) {
99+
unityGame = unityInstance;
98100
loadingBar.style.display = "none";
99101
}).catch(function(message) {
100102
alert(message);

0 commit comments

Comments
 (0)