Skip to content

Commit fcabe72

Browse files
committed
Change improve commands to be allowed to exist in all assemblies
This is quite a bit more flexible, but it also is more performance-heavy in the beginning, since all assemblies need to be filtered through for the matching class
1 parent 0b8db7e commit fcabe72

16 files changed

+138
-55
lines changed
Lines changed: 1 addition & 1 deletion
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/WebTools/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/WebTools/Attributes/WebGlCommandAttribute.cs.meta renamed to Assets/Plugins/WebGL/WebBridge/Attributes/WebCommandAttribute.cs.meta

File renamed without changes.

Assets/Plugins/WebGL/WebTools/WebBridge.Commands.cs renamed to Assets/Plugins/WebGL/WebBridge/CommonCommands.cs

Lines changed: 17 additions & 17 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 WebBridge
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,7 +51,7 @@ 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
{
@@ -62,7 +62,7 @@ public void LogMemory()
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,7 +119,7 @@ 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
{
125125
WebToolPlugins.ToggleInfoPanel();
@@ -129,7 +129,7 @@ public void ToggleInfoPanel()
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
{
135135
string userAgent = WebToolPlugins.GetUserAgent();
@@ -140,7 +140,7 @@ public void LogUserAgent()
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/WebTools/WebBridge.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.

Assets/Plugins/WebGL/WebTools/WebBridge.cs renamed to Assets/Plugins/WebGL/WebBridge/WebBridge.cs

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// </author>
99
// --------------------------------------------------------------------------------------------------------------------
1010

11+
using System;
1112
using System.Reflection;
1213
using System.Text;
1314
using Supyrb.Attributes;
@@ -20,19 +21,37 @@ namespace Supyrb
2021
/// Bridge to Unity to access unity logic through the browser console
2122
/// You can extend your commands by creating a partial class for WebBridge, see WebBridge.Commands as an example
2223
/// </summary>
23-
public partial class WebBridge : MonoBehaviour
24+
public class WebBridge : WebCommands
2425
{
26+
private static GameObject bridgeInstance;
2527
#if UNITY_WEBGL
2628
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
2729
private static void OnBeforeSceneLoadRuntimeMethod()
2830
{
2931
SetGlobalVariables();
30-
var bridgeInstance = new GameObject("Web");
32+
bridgeInstance = new GameObject("Web");
3133
DontDestroyOnLoad(bridgeInstance);
32-
bridgeInstance.AddComponent<WebBridge>();
34+
AddAllWebCommands();
3335
}
3436
#endif
3537

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+
3655
private static void SetGlobalVariables()
3756
{
3857
var graphicsDevice = SystemInfo.graphicsDeviceType;
@@ -66,37 +85,43 @@ private void Start()
6685
Debug.Log("Unity WebGL Bridge ready -> Run 'unityGame.SendMessage(\"WebGL\", \"Help\")' in the browser console to see usage");
6786
}
6887

69-
[WebGlCommand(Description = "Log all available commands")]
88+
[WebCommand(Description = "Log all available commands")]
7089
[ContextMenu(nameof(Help))]
7190
public void Help()
7291
{
7392
StringBuilder sb = new StringBuilder();
74-
MethodInfo[] methods = GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
93+
sb.AppendLine("Available commands:");
7594

76-
sb.AppendLine("Available unity interfaces:");
77-
for (int i = 0; i < methods.Length; i++)
95+
foreach (var webCommand in gameObject.GetComponents<WebCommands>())
7896
{
79-
var method = methods[i];
80-
WebGlCommandAttribute commandAttribute = method.GetCustomAttribute<WebGlCommandAttribute>();
81-
if (commandAttribute != null)
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++)
82101
{
83-
sb.Append($"unityGame.SendMessage(\"WebGL\", \"{method.Name}\"");
84-
ParameterInfo[] parameters = method.GetParameters();
85-
for (int j = 0; j < parameters.Length; j++)
102+
var method = methods[i];
103+
WebCommandAttribute commandAttribute = method.GetCustomAttribute<WebCommandAttribute>();
104+
if (commandAttribute != null)
86105
{
87-
var parameter = parameters[j];
88-
if (parameter.ParameterType == typeof(string))
89-
{
90-
sb.Append($", \"{parameter.ParameterType} {parameter.Name}\"");
91-
}
92-
else
106+
sb.Append($"unityGame.SendMessage(\"WebGL\", \"{method.Name}\"");
107+
ParameterInfo[] parameters = method.GetParameters();
108+
for (int j = 0; j < parameters.Length; j++)
93109
{
94-
sb.Append($", {parameter.ParameterType} {parameter.Name}");
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+
}
95119
}
96-
}
97120

98-
sb.AppendLine($"); <color=#555555FF>-> {commandAttribute.Description}</color>");
121+
sb.AppendLine($"); <color=#555555FF>-> {commandAttribute.Description}</color>");
122+
}
99123
}
124+
100125
}
101126

102127
sb.AppendLine("\nRun a command with 'unityGame.SendMessage(\"WebGL\", \"COMMAND_NAME\",PARAMETER);'");
File renamed without changes.

0 commit comments

Comments
 (0)