Skip to content

Commit d3f348e

Browse files
committed
Merge branch 'master' into 2021.3-urp
2 parents aebe343 + 4a328ce commit d3f348e

29 files changed

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

0 commit comments

Comments
 (0)