Skip to content

Commit e951f67

Browse files
committed
Change use attributes to log all available bridge methods
1 parent 1175878 commit e951f67

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

Assets/Plugins/WebGL/Attributes.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="WebGlCommandAttribute.cs">
3+
// Copyright (c) 2022 Johannes Deml. All rights reserved.
4+
// </copyright>
5+
// <author>
6+
// Johannes Deml
7+
8+
// </author>
9+
// --------------------------------------------------------------------------------------------------------------------
10+
11+
using System;
12+
using JetBrains.Annotations;
13+
14+
namespace Supyrb.Attributes
15+
{
16+
[AttributeUsage(AttributeTargets.Method)]
17+
[MeansImplicitUse]
18+
public class WebGlCommandAttribute : Attribute
19+
{
20+
public string Description { get; set; }
21+
}
22+
}

Assets/Plugins/WebGL/Attributes/WebGlCommandAttribute.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Plugins/WebGL/WebGlBridge.cs

+39-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
// --------------------------------------------------------------------------------------------------------------------
1010

1111
using System;
12+
using System.Reflection;
13+
using System.Text;
14+
using Supyrb.Attributes;
1215
using UnityEngine;
1316
using UnityEngine.Rendering;
1417

@@ -53,22 +56,45 @@ private void Start()
5356
Debug.Log("Unity WebGL Bridge ready -> Run 'unityGame.SendMessage(\"WebGL\", \"Help\")' in the browser console to see usage");
5457
}
5558

59+
[ContextMenu("Log all commands")]
60+
[WebGlCommand(Description = "Log all available commands")]
5661
public void Help()
5762
{
58-
Debug.Log("Available unity interfaces:\n" +
59-
"- LogMemory() -> logs current memory\n" +
60-
"- SetApplicationRunInBackground(int runInBackground) -> Application.runInBackground\n" +
61-
"- SetApplicationTargetFrameRate(int targetFrameRate) -> Application.targetFrameRate\n" +
62-
"- SetTimeFixedDeltaTime(float fixedDeltaTime) -> Time.fixedDeltaTime\n" +
63-
"- SetTimeTimeScale(float timeScale) -> Time.timeScale\n" +
64-
"- ToggleInfoPanel() -> Toggle develop ui visibility of InfoPanel\n" +
65-
"\nRun a command through 'unityGame.SendMessage(\"WebGL\", \"COMMAND_NAME\",PARAMETER)'");
63+
StringBuilder sb = new StringBuilder();
64+
MethodInfo[] methods = GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
65+
66+
sb.AppendLine("Available unity interfaces:");
67+
for (int i = 0; i < methods.Length; i++)
68+
{
69+
var method = methods[i];
70+
WebGlCommandAttribute commandAttribute = method.GetCustomAttribute<WebGlCommandAttribute>();
71+
if (commandAttribute != null)
72+
{
73+
sb.Append($"- {method.Name}(");
74+
ParameterInfo[] parameters = method.GetParameters();
75+
for (int j = 0; j < parameters.Length; j++)
76+
{
77+
var parameter = parameters[j];
78+
sb.Append($"{parameter.ParameterType} {parameter.Name}");
79+
if (j < parameters.Length - 1)
80+
{
81+
sb.Append(", ");
82+
}
83+
}
84+
85+
sb.AppendLine($") -> {commandAttribute.Description}");
86+
}
87+
}
88+
89+
sb.AppendLine("\nRun a command with 'unityGame.SendMessage(\"WebGL\", \"COMMAND_NAME\",PARAMETER)'");
90+
Debug.Log(sb.ToString());
6691
}
6792

6893
/// <summary>
6994
/// Logs the current memory usage
7095
/// Browser Usage: <code>unityGame.SendMessage("WebGL","LogMemory");</code>
7196
/// </summary>
97+
[WebGlCommand(Description = "Logs the current memory")]
7298
public void LogMemory()
7399
{
74100
WebGlPlugins.LogMemory();
@@ -80,6 +106,7 @@ public void LogMemory()
80106
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetApplicationRunInBackground", 1);</code>
81107
/// </summary>
82108
/// <param name="runInBackground">1 if it should run in background</param>
109+
[WebGlCommand(Description = "Application.runInBackground")]
83110
public void SetApplicationRunInBackground(int runInBackground)
84111
{
85112
Application.runInBackground = runInBackground == 1;
@@ -90,6 +117,7 @@ public void SetApplicationRunInBackground(int runInBackground)
90117
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetApplicationTargetFrameRate", 15);</code>
91118
/// </summary>
92119
/// <param name="targetFrameRate">frame rate to render in</param>
120+
[WebGlCommand(Description = "Application.targetFrameRate")]
93121
public void SetApplicationTargetFrameRate(int targetFrameRate)
94122
{
95123
Application.targetFrameRate = targetFrameRate;
@@ -100,6 +128,7 @@ public void SetApplicationTargetFrameRate(int targetFrameRate)
100128
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetTimeFixedDeltaTime", 0.02);</code>
101129
/// </summary>
102130
/// <param name="fixedDeltaTime"></param>
131+
[WebGlCommand(Description = "Time.fixedDeltaTime")]
103132
public void SetTimeFixedDeltaTime(float fixedDeltaTime)
104133
{
105134
Time.fixedDeltaTime = fixedDeltaTime;
@@ -111,6 +140,7 @@ public void SetTimeFixedDeltaTime(float fixedDeltaTime)
111140
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetTimeTimeScale", 0.2);</code>
112141
/// </summary>
113142
/// <param name="timeScale">new timescale value</param>
143+
[WebGlCommand(Description = "Time.timeScale")]
114144
public void SetTimeTimeScale(float timeScale)
115145
{
116146
Time.timeScale = timeScale;
@@ -119,6 +149,7 @@ public void SetTimeTimeScale(float timeScale)
119149
/// <summary>
120150
/// Toggle the visibility of the info panel in the top right corner
121151
/// </summary>
152+
[WebGlCommand(Description = "Toggle develop ui visibility of InfoPanel")]
122153
public void ToggleInfoPanel()
123154
{
124155
WebGlPlugins.ToggleInfoPanel();

0 commit comments

Comments
 (0)