Skip to content

Commit 4a328ce

Browse files
committed
Change fix command help output and simplify
Add a small helper function `runUnityCommand` to reduce typing overhead
1 parent 9eba000 commit 4a328ce

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

Assets/Plugins/WebGL/WebBridge/WebBridge.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// --------------------------------------------------------------------------------------------------------------------
1010

1111
using System;
12+
using System.Collections.Generic;
1213
using System.Reflection;
1314
using System.Text;
1415
using Supyrb.Attributes;
@@ -23,13 +24,15 @@ namespace Supyrb
2324
/// </summary>
2425
public class WebBridge : WebCommands
2526
{
27+
private const string WebBridgeGameObjectName = "WebBridge";
28+
2629
private static GameObject bridgeInstance;
2730
#if UNITY_WEBGL
2831
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
2932
private static void OnBeforeSceneLoadRuntimeMethod()
3033
{
3134
SetGlobalVariables();
32-
bridgeInstance = new GameObject("Web");
35+
bridgeInstance = new GameObject(WebBridgeGameObjectName);
3336
DontDestroyOnLoad(bridgeInstance);
3437
AddAllWebCommands();
3538
}
@@ -38,18 +41,25 @@ private static void OnBeforeSceneLoadRuntimeMethod()
3841
private static void AddAllWebCommands()
3942
{
4043
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
44+
List<Type> webCommandTypes = new List<Type>();
4145
foreach (var assembly in assemblies)
4246
{
4347
Type[] types = assembly.GetTypes();
4448
foreach (var type in types)
4549
{
4650
if (type.IsSubclassOf(typeof(WebCommands)))
4751
{
48-
bridgeInstance.AddComponent(type);
52+
webCommandTypes.Add(type);
4953
}
5054
}
5155
}
5256

57+
// Sort the commands to make the output deterministic
58+
webCommandTypes.Sort((a, b) => a.Name.CompareTo(b.Name));
59+
foreach (var webCommandType in webCommandTypes)
60+
{
61+
bridgeInstance.AddComponent(webCommandType);
62+
}
5363
}
5464

5565
private static void SetGlobalVariables()
@@ -82,7 +92,7 @@ private static void SetGlobalVariables()
8292

8393
private void Start()
8494
{
85-
Debug.Log("Unity WebGL Bridge ready -> Run 'unityGame.SendMessage(\"WebGL\", \"Help\")' in the browser console to see usage");
95+
Debug.Log($"Unity WebGL Bridge ready -> Run 'runUnityCommand(\"Help\")' in the browser console to see usage");
8696
}
8797

8898
[WebCommand(Description = "Log all available commands")]
@@ -103,7 +113,7 @@ public void Help()
103113
WebCommandAttribute commandAttribute = method.GetCustomAttribute<WebCommandAttribute>();
104114
if (commandAttribute != null)
105115
{
106-
sb.Append($"unityGame.SendMessage(\"WebGL\", \"{method.Name}\"");
116+
sb.Append($"runUnityCommand(\"{method.Name}\"");
107117
ParameterInfo[] parameters = method.GetParameters();
108118
for (int j = 0; j < parameters.Length; j++)
109119
{
@@ -124,7 +134,7 @@ public void Help()
124134

125135
}
126136

127-
sb.AppendLine("\nRun a command with 'unityGame.SendMessage(\"WebGL\", \"COMMAND_NAME\",PARAMETER);'");
137+
sb.AppendLine($"\nRun a command with 'runUnityCommand(\"COMMAND_NAME\",PARAMETER);'");
128138
Debug.Log(sb.ToString());
129139
}
130140
}

Assets/WebGLTemplates/Develop/debug-console.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function initializeDebugConsole() {
5757
var consoleInput = document.createElement("input");
5858
consoleInput.className = 'console-input';
5959
consoleInput.type = "text";
60-
consoleInput.value = 'unityGame.SendMessage("WebGL", "Help");';
60+
consoleInput.value = 'runUnityCommand("Help");';
6161
debugConsole.appendChild(consoleInput);
6262

6363
consoleInput.onkeydown = function (e) {
@@ -70,6 +70,10 @@ function initializeDebugConsole() {
7070
setupConsoleLogPipe();
7171
}
7272

73+
function runUnityCommand(...command) {
74+
unityGame.SendMessage("WebBridge", ...command);
75+
}
76+
7377
function addDebugConsoleTopBar(debugConsole) {
7478
var consoleTopBar = document.createElement('div');
7579
consoleTopBar.className = 'console-top-bar';
@@ -528,15 +532,15 @@ function initializeToggleButton(startActive) {
528532
return;
529533
}
530534
if (event.currentTarget.checked) {
531-
unityGame.SendMessage("WebGL", "DisableCaptureAllKeyboardInput");
535+
runUnityCommand("DisableCaptureAllKeyboardInput");
532536
return;
533537
}
534538

535539
if (typeof unityCaptureAllKeyboardInputDefault !== 'undefined' && unityCaptureAllKeyboardInputDefault === 'false') {
536-
unityGame.SendMessage("WebGL", "DisableCaptureAllKeyboardInput");
540+
runUnityCommand("DisableCaptureAllKeyboardInput");
537541
}
538542
else {
539-
unityGame.SendMessage("WebGL", "EnableCaptureAllKeyboardInput");
543+
runUnityCommand("EnableCaptureAllKeyboardInput");
540544
}
541545
})
542546
}

0 commit comments

Comments
 (0)