Skip to content

Commit eeebb97

Browse files
committed
Merge branch 'master' of https://github.com/JohannesDeml/UnityWebGL-LoadingTest into 2022.3
2 parents 68fbdf8 + 9eba000 commit eeebb97

28 files changed

+374
-216
lines changed

Assets/Plugins/WebGL/WebBridge.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.
File renamed without changes.

Assets/Plugins/WebGL/WebGLTools/Attributes/WebGlCommandAttribute.cs renamed to Assets/Plugins/WebGL/WebBridge/Attributes/WebCommandAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Supyrb.Attributes
1515
{
1616
[AttributeUsage(AttributeTargets.Method)]
1717
[MeansImplicitUse]
18-
public class WebGlCommandAttribute : Attribute
18+
public class WebCommandAttribute : Attribute
1919
{
2020
public string Description { get; set; }
2121
}

Assets/Plugins/WebGL/WebGLTools/Attributes/WebGlCommandAttribute.cs.meta renamed to Assets/Plugins/WebGL/WebBridge/Attributes/WebCommandAttribute.cs.meta

File renamed without changes.

Assets/Plugins/WebGL/WebGLTools/WebGlBridge.Commands.cs renamed to Assets/Plugins/WebGL/WebBridge/CommonCommands.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ namespace Supyrb
1919
/// <summary>
2020
/// Add commands to the WebGL bridge to expose them to the browser console
2121
/// </summary>
22-
public partial class WebGlBridge
22+
public class CommonCommands : WebCommands
2323
{
2424
/// <summary>
2525
/// Disable capturing all keyboard input, e.g. for using native html input fields
2626
/// Browser Usage: <code>unityGame.SendMessage("WebGL","DisableCaptureAllKeyboardInput");</code>
2727
/// </summary>
28-
[WebGlCommand(Description = "Disable unity from consuming all keyboard input")]
28+
[WebCommand(Description = "Disable unity from consuming all keyboard input")]
2929
public void DisableCaptureAllKeyboardInput()
3030
{
3131
#if !UNITY_EDITOR && UNITY_WEBGL
@@ -38,7 +38,7 @@ public void DisableCaptureAllKeyboardInput()
3838
/// Enable capturing all keyboard input, to make sure the game does not miss any key strokes
3939
/// Browser Usage: <code>unityGame.SendMessage("WebGL","EnableCaptureAllKeyboardInput");</code>
4040
/// </summary>
41-
[WebGlCommand(Description = "Enable unity from consuming all keyboard input")]
41+
[WebCommand(Description = "Enable unity from consuming all keyboard input")]
4242
public void EnableCaptureAllKeyboardInput()
4343
{
4444
#if !UNITY_EDITOR && UNITY_WEBGL
@@ -51,18 +51,18 @@ public void EnableCaptureAllKeyboardInput()
5151
/// Logs the current memory usage
5252
/// Browser Usage: <code>unityGame.SendMessage("WebGL","LogMemory");</code>
5353
/// </summary>
54-
[WebGlCommand(Description = "Logs the current memory")]
54+
[WebCommand(Description = "Logs the current memory")]
5555
[ContextMenu(nameof(LogMemory))]
5656
public void LogMemory()
5757
{
58-
WebGlPlugins.LogMemory();
58+
WebToolPlugins.LogMemory();
5959
}
6060

6161
/// <summary>
6262
/// Unloads all unused assets <see cref="Resources.UnloadUnusedAssets"/>
6363
/// Browser Usage: <code>unityGame.SendMessage("WebGL","UnloadUnusedAssets");</code>
6464
/// </summary>
65-
[WebGlCommand(Description = "Resources.UnloadUnusedAssets")]
65+
[WebCommand(Description = "Resources.UnloadUnusedAssets")]
6666
[ContextMenu(nameof(UnloadUnusedAssets))]
6767
public void UnloadUnusedAssets()
6868
{
@@ -75,7 +75,7 @@ public void UnloadUnusedAssets()
7575
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetApplicationRunInBackground", 1);</code>
7676
/// </summary>
7777
/// <param name="runInBackground">1 if it should run in background</param>
78-
[WebGlCommand(Description = "Application.runInBackground")]
78+
[WebCommand(Description = "Application.runInBackground")]
7979
public void SetApplicationRunInBackground(int runInBackground)
8080
{
8181
Application.runInBackground = runInBackground == 1;
@@ -86,7 +86,7 @@ public void SetApplicationRunInBackground(int runInBackground)
8686
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetApplicationTargetFrameRate", 15);</code>
8787
/// </summary>
8888
/// <param name="targetFrameRate">frame rate to render in</param>
89-
[WebGlCommand(Description = "Application.targetFrameRate")]
89+
[WebCommand(Description = "Application.targetFrameRate")]
9090
public void SetApplicationTargetFrameRate(int targetFrameRate)
9191
{
9292
Application.targetFrameRate = targetFrameRate;
@@ -97,7 +97,7 @@ public void SetApplicationTargetFrameRate(int targetFrameRate)
9797
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetTimeFixedDeltaTime", 0.02);</code>
9898
/// </summary>
9999
/// <param name="fixedDeltaTime"></param>
100-
[WebGlCommand(Description = "Time.fixedDeltaTime")]
100+
[WebCommand(Description = "Time.fixedDeltaTime")]
101101
public void SetTimeFixedDeltaTime(float fixedDeltaTime)
102102
{
103103
Time.fixedDeltaTime = fixedDeltaTime;
@@ -109,7 +109,7 @@ public void SetTimeFixedDeltaTime(float fixedDeltaTime)
109109
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetTimeTimeScale", 0.2);</code>
110110
/// </summary>
111111
/// <param name="timeScale">new timescale value</param>
112-
[WebGlCommand(Description = "Time.timeScale")]
112+
[WebCommand(Description = "Time.timeScale")]
113113
public void SetTimeTimeScale(float timeScale)
114114
{
115115
Time.timeScale = timeScale;
@@ -119,28 +119,28 @@ public void SetTimeTimeScale(float timeScale)
119119
/// Toggle the visibility of the info panel in the top right corner
120120
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "ToggleInfoPanel");</code>
121121
/// </summary>
122-
[WebGlCommand(Description = "Toggle develop ui visibility of InfoPanel")]
122+
[WebCommand(Description = "Toggle develop ui visibility of InfoPanel")]
123123
public void ToggleInfoPanel()
124124
{
125-
WebGlPlugins.ToggleInfoPanel();
125+
WebToolPlugins.ToggleInfoPanel();
126126
}
127127

128128
/// <summary>
129129
/// Log the user agent of the browser and if this agent is classified as mobile
130130
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "LogUserAgent");</code>
131131
/// </summary>
132-
[WebGlCommand(Description = "Log User Agent and isMobileDevice")]
132+
[WebCommand(Description = "Log User Agent and isMobileDevice")]
133133
public void LogUserAgent()
134134
{
135-
string userAgent = WebGlPlugins.GetUserAgent();
136-
bool isMobileDevice = WebGlPlugins.IsMobileDevice();
135+
string userAgent = WebToolPlugins.GetUserAgent();
136+
bool isMobileDevice = WebToolPlugins.IsMobileDevice();
137137
Debug.Log($"<color=#4D65A4>User Agent:</color> '{userAgent}', <color=#4D65A4>IsMobileDevice:</color> '{isMobileDevice}'");
138138
}
139139

140140
/// <summary>
141141
/// Log example messages to show off unity rich text parsing to html & console styling
142142
/// </summary>
143-
[WebGlCommand(Description = "Log example messages for Log, warning and error")]
143+
[WebCommand(Description = "Log example messages for Log, warning and error")]
144144
[ContextMenu(nameof(LogExampleMessages))]
145145
public void LogExampleMessages()
146146
{
@@ -153,7 +153,7 @@ public void LogExampleMessages()
153153
/// Log a custom message to test Debug.Log in general
154154
/// </summary>
155155
/// <param name="message">Message that will be logged</param>
156-
[WebGlCommand(Description = "Log a custom message")]
156+
[WebCommand(Description = "Log a custom message")]
157157
public void LogMessage(string message)
158158
{
159159
Debug.Log(message);
@@ -162,7 +162,7 @@ public void LogMessage(string message)
162162
/// <summary>
163163
/// Throw an exception from System namespace to see how stack traces look for that
164164
/// </summary>
165-
[WebGlCommand(Description = "Throw a dictionary key not found exception")]
165+
[WebCommand(Description = "Throw a dictionary key not found exception")]
166166
[ContextMenu(nameof(ThrowDictionaryException))]
167167
public void ThrowDictionaryException()
168168
{
@@ -175,7 +175,7 @@ public void ThrowDictionaryException()
175175
/// Log information of all texture formats that Unity supports, which ones are supported by
176176
/// the current platform and browser, and which ones are not supported
177177
/// </summary>
178-
[WebGlCommand(Description = "Log supported and unsupported texture formats")]
178+
[WebCommand(Description = "Log supported and unsupported texture formats")]
179179
[ContextMenu(nameof(LogTextureSupport))]
180180
public void LogTextureSupport()
181181
{
@@ -209,7 +209,7 @@ public void LogTextureSupport()
209209
/// <summary>
210210
/// Deletes all player prefs <see cref="PlayerPrefs.DeleteAll"/>
211211
/// </summary>
212-
[WebGlCommand(Description = "PlayerPrefs.DeleteAll")]
212+
[WebCommand(Description = "PlayerPrefs.DeleteAll")]
213213
[ContextMenu(nameof(DeleteAllPlayerPrefs))]
214214
public void DeleteAllPlayerPrefs()
215215
{
@@ -221,7 +221,7 @@ public void DeleteAllPlayerPrefs()
221221
/// <see cref="GraphicsSettings.logWhenShaderIsCompiled "/>
222222
/// </summary>
223223
/// <param name="runInBackground">1 if it should run in background</param>
224-
[WebGlCommand(Description = "GraphicsSettings.logWhenShaderIsCompiled")]
224+
[WebCommand(Description = "GraphicsSettings.logWhenShaderIsCompiled")]
225225
[ContextMenu(nameof(LogShaderCompilation))]
226226
public void LogShaderCompilation(int enabled)
227227
{

Assets/Plugins/WebGL/WebGLTools/WebGlBridge.Commands.cs.meta renamed to Assets/Plugins/WebGL/WebBridge/CommonCommands.cs.meta

File renamed without changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "Supyrb.WebBridge",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:e6be6bd2cac63ce49a95472c9d6e313a"
6+
],
7+
"includePlatforms": [
8+
"Editor",
9+
"WebGL"
10+
],
11+
"excludePlatforms": [],
12+
"allowUnsafeCode": false,
13+
"overrideReferences": false,
14+
"precompiledReferences": [],
15+
"autoReferenced": true,
16+
"defineConstraints": [],
17+
"versionDefines": [],
18+
"noEngineReferences": false
19+
}

Assets/Plugins/WebGL/WebBridge/Supyrb.WebBridge.asmdef.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: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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 System.Reflection;
13+
using System.Text;
14+
using Supyrb.Attributes;
15+
using UnityEngine;
16+
using UnityEngine.Rendering;
17+
18+
namespace Supyrb
19+
{
20+
/// <summary>
21+
/// Bridge to Unity to access unity logic through the browser console
22+
/// You can extend your commands by creating a partial class for WebBridge, see WebBridge.Commands as an example
23+
/// </summary>
24+
public class WebBridge : WebCommands
25+
{
26+
private static GameObject bridgeInstance;
27+
#if UNITY_WEBGL
28+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
29+
private static void OnBeforeSceneLoadRuntimeMethod()
30+
{
31+
SetGlobalVariables();
32+
bridgeInstance = new GameObject("Web");
33+
DontDestroyOnLoad(bridgeInstance);
34+
AddAllWebCommands();
35+
}
36+
#endif
37+
38+
private static void AddAllWebCommands()
39+
{
40+
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
41+
foreach (var assembly in assemblies)
42+
{
43+
Type[] types = assembly.GetTypes();
44+
foreach (var type in types)
45+
{
46+
if (type.IsSubclassOf(typeof(WebCommands)))
47+
{
48+
bridgeInstance.AddComponent(type);
49+
}
50+
}
51+
}
52+
53+
}
54+
55+
private static void SetGlobalVariables()
56+
{
57+
var graphicsDevice = SystemInfo.graphicsDeviceType;
58+
string webGraphics = string.Empty;
59+
switch (graphicsDevice)
60+
{
61+
case GraphicsDeviceType.OpenGLES2:
62+
webGraphics = "WebGL 1";
63+
break;
64+
case GraphicsDeviceType.OpenGLES3:
65+
webGraphics = "WebGL 2";
66+
break;
67+
#if UNITY_2023_2_OR_NEWER
68+
case GraphicsDeviceType.WebGPU:
69+
webGraphics = "WebGPU";
70+
break;
71+
#endif
72+
default:
73+
webGraphics = graphicsDevice.ToString();
74+
break;
75+
}
76+
WebToolPlugins.SetVariable("webGlVersion", webGraphics);
77+
WebToolPlugins.SetVariable("unityVersion", Application.unityVersion);
78+
#if !UNITY_EDITOR && UNITY_WEBGL
79+
WebToolPlugins.SetVariable("unityCaptureAllKeyboardInputDefault", WebGLInput.captureAllKeyboardInput?"true":"false");
80+
#endif
81+
}
82+
83+
private void Start()
84+
{
85+
Debug.Log("Unity WebGL Bridge ready -> Run 'unityGame.SendMessage(\"WebGL\", \"Help\")' in the browser console to see usage");
86+
}
87+
88+
[WebCommand(Description = "Log all available commands")]
89+
[ContextMenu(nameof(Help))]
90+
public void Help()
91+
{
92+
StringBuilder sb = new StringBuilder();
93+
sb.AppendLine("Available commands:");
94+
95+
foreach (var webCommand in gameObject.GetComponents<WebCommands>())
96+
{
97+
sb.AppendLine($"<b>---{webCommand.GetType().Name}---</b>");
98+
MethodInfo[] methods = webCommand.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
99+
100+
for (int i = 0; i < methods.Length; i++)
101+
{
102+
var method = methods[i];
103+
WebCommandAttribute commandAttribute = method.GetCustomAttribute<WebCommandAttribute>();
104+
if (commandAttribute != null)
105+
{
106+
sb.Append($"unityGame.SendMessage(\"WebGL\", \"{method.Name}\"");
107+
ParameterInfo[] parameters = method.GetParameters();
108+
for (int j = 0; j < parameters.Length; j++)
109+
{
110+
var parameter = parameters[j];
111+
if (parameter.ParameterType == typeof(string))
112+
{
113+
sb.Append($", \"{parameter.ParameterType} {parameter.Name}\"");
114+
}
115+
else
116+
{
117+
sb.Append($", {parameter.ParameterType} {parameter.Name}");
118+
}
119+
}
120+
121+
sb.AppendLine($"); <color=#555555FF>-> {commandAttribute.Description}</color>");
122+
}
123+
}
124+
125+
}
126+
127+
sb.AppendLine("\nRun a command with 'unityGame.SendMessage(\"WebGL\", \"COMMAND_NAME\",PARAMETER);'");
128+
Debug.Log(sb.ToString());
129+
}
130+
}
131+
}
File renamed without changes.

0 commit comments

Comments
 (0)