Skip to content

Commit 2ad04a4

Browse files
committed
Change track awake and start times and add generic logic to add arbitrary time tracker events
Also the new global variables unityVersion and webGlVersion are now exposed and new global variables are easy to add
1 parent 7f94088 commit 2ad04a4

10 files changed

Lines changed: 252 additions & 93 deletions

File tree

Assets/Plugins/WebGL/WebGlBridge.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
using System;
1212
using UnityEngine;
13+
using UnityEngine.Rendering;
1314

1415
namespace Supyrb
1516
{
@@ -21,11 +22,32 @@ public class WebGlBridge : MonoBehaviour
2122
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
2223
private static void OnBeforeSceneLoadRuntimeMethod()
2324
{
25+
SetGlobalVariables();
2426
var bridgeInstance = new GameObject("WebGL");
2527
DontDestroyOnLoad(bridgeInstance);
2628
bridgeInstance.AddComponent<WebGlBridge>();
2729
}
2830

31+
private static void SetGlobalVariables()
32+
{
33+
var graphicsDevice = SystemInfo.graphicsDeviceType;
34+
string webGl = string.Empty;
35+
switch (graphicsDevice)
36+
{
37+
case GraphicsDeviceType.OpenGLES2:
38+
webGl = "WebGL 1";
39+
break;
40+
case GraphicsDeviceType.OpenGLES3:
41+
webGl = "WebGL 2";
42+
break;
43+
default:
44+
webGl = graphicsDevice.ToString();
45+
break;
46+
}
47+
WebGlPlugins.SetVariable("webGlVersion", webGl);
48+
WebGlPlugins.SetVariable("unityVersion", Application.unityVersion);
49+
}
50+
2951
private void Start()
3052
{
3153
Debug.Log("Unity WebGL Bridge ready -> Run 'unityGame.SendMessage(\"WebGL\", \"Help\")' in the browser console to see usage");
@@ -39,9 +61,10 @@ public void Help()
3961
"- SetApplicationTargetFrameRate(int targetFrameRate) -> Application.targetFrameRate\n" +
4062
"- SetTimeFixedDeltaTime(float fixedDeltaTime) -> Time.fixedDeltaTime\n" +
4163
"- SetTimeTimeScale(float timeScale) -> Time.timeScale\n" +
64+
"- ToggleInfoPanel() -> Toggle develop ui visibility of InfoPanel\n" +
4265
"\nRun a command through 'unityGame.SendMessage(\"WebGL\", \"COMMAND_NAME\",PARAMETER)'");
4366
}
44-
67+
4568
/// <summary>
4669
/// Logs the current memory usage
4770
/// Browser Usage: <code>unityGame.SendMessage("WebGL","LogMemory");</code>
@@ -92,5 +115,13 @@ public void SetTimeTimeScale(float timeScale)
92115
{
93116
Time.timeScale = timeScale;
94117
}
118+
119+
/// <summary>
120+
/// Toggle the visibility of the info panel in the top right corner
121+
/// </summary>
122+
public void ToggleInfoPanel()
123+
{
124+
WebGlPlugins.ToggleInfoPanel();
125+
}
95126
}
96127
}

Assets/Plugins/WebGL/WebGlLogger.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

Assets/Plugins/WebGL/WebGlPlugins.cs

Lines changed: 80 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ namespace Supyrb
2020
public static class WebGlPlugins
2121
{
2222
[DllImport("__Internal")]
23-
private static extern void _LogStartTime(string startupTime, string unityVersion, string webGlVersion);
23+
private static extern void _SetStringVariable(string variableName, string variableValue);
24+
[DllImport("__Internal")]
25+
private static extern void _AddTimeTrackingEvent(string eventName);
26+
[DllImport("__Internal")]
27+
private static extern void _ShowInfoPanel();
28+
[DllImport("__Internal")]
29+
private static extern void _HideInfoPanel();
2430

2531
[DllImport("__Internal")]
2632
private static extern void _LogMemoryInfo(uint native, uint managed, uint total);
@@ -34,35 +40,82 @@ public static class WebGlPlugins
3440
[DllImport("__Internal")]
3541
private static extern uint _GetDynamicMemorySize();
3642

43+
private static bool _infoPanelVisible = false;
44+
3745
/// <summary>
38-
/// Logs the start time for Unity and the time since the webpage started loading
39-
/// By default triggered by <see cref="WebGlLogger"/> in Awake with execution order -100
40-
/// Needs the Develop WebGL Template to include the webpage time information
46+
/// Sets a global string variable in JavaScript.
47+
/// This variable can be accessed by the console and other javascript functions
4148
/// </summary>
42-
public static void LogStartTime()
49+
/// <param name="variableName">Name of the variable</param>
50+
/// <param name="value">Value of the variable</param>
51+
public static void SetVariable(string variableName, string value)
4352
{
4453
#if UNITY_WEBGL && !UNITY_EDITOR
45-
var graphicsDevice = SystemInfo.graphicsDeviceType;
46-
string webGl = string.Empty;
47-
switch (graphicsDevice)
48-
{
49-
case GraphicsDeviceType.OpenGLES2:
50-
webGl = "WebGL 1";
51-
break;
52-
case GraphicsDeviceType.OpenGLES3:
53-
webGl = "WebGL 2";
54-
break;
55-
default:
56-
webGl = graphicsDevice.ToString();
57-
break;
58-
}
59-
60-
_LogStartTime(Time.realtimeSinceStartup.ToString("0.00"), Application.unityVersion, webGl);
54+
_SetStringVariable(variableName, value);
55+
#else
56+
Debug.Log($"{nameof(WebGlPlugins)}.{nameof(SetVariable)} called with {variableName}: {value}");
57+
#endif
58+
}
59+
60+
/// <summary>
61+
/// Adds a time marker at the call time to the javascript map "unityTimeTrackers"
62+
/// The mapped value is performance.now() at the time of the call
63+
/// </summary>
64+
/// <param name="eventName">Name of the tracking event, e.g. "Awake"</param>
65+
public static void AddTimeTrackingEvent(string eventName)
66+
{
67+
#if UNITY_WEBGL && !UNITY_EDITOR
68+
_AddTimeTrackingEvent(eventName);
69+
#else
70+
Debug.Log($"{nameof(WebGlPlugins)}.{nameof(AddTimeTrackingEvent)} called with {eventName}");
71+
#endif
72+
}
73+
74+
/// <summary>
75+
/// Show the info panel in the top right corner
76+
/// By default triggered by <see cref="WebGlTimeTracker"/> in Awake
77+
/// Needs the Develop WebGL Template to provide the logic
78+
/// </summary>
79+
public static void ShowInfoPanel()
80+
{
81+
_infoPanelVisible = true;
82+
#if UNITY_WEBGL && !UNITY_EDITOR
83+
_ShowInfoPanel();
84+
#else
85+
Debug.Log($"{nameof(WebGlPlugins)}.{nameof(ShowInfoPanel)} called");
86+
#endif
87+
}
88+
89+
/// <summary>
90+
/// Hide the info panel in the top right corner
91+
/// By default triggered by <see cref="WebGlTimeTracker"/> in Awake
92+
/// Needs the Develop WebGL Template to provide the logic
93+
/// </summary>
94+
public static void HideInfoPanel()
95+
{
96+
_infoPanelVisible = false;
97+
#if UNITY_WEBGL && !UNITY_EDITOR
98+
_HideInfoPanel();
6199
#else
62-
Debug.Log("Not supported on this platform");
100+
Debug.Log($"{nameof(WebGlPlugins)}.{nameof(HideInfoPanel)} called");
63101
#endif
64102
}
65103

104+
/// <summary>
105+
/// Toggle the visibility of the info panel
106+
/// </summary>
107+
public static void ToggleInfoPanel()
108+
{
109+
if (_infoPanelVisible)
110+
{
111+
HideInfoPanel();
112+
}
113+
else
114+
{
115+
ShowInfoPanel();
116+
}
117+
}
118+
66119
/// <summary>
67120
/// Log all current memory data in MB
68121
/// </summary>
@@ -74,7 +127,7 @@ public static void LogMemory()
74127
var total = GetTotalMemorySize();
75128
Debug.Log($"Memory stats:\nManaged: {managed:0.00}MB\nNative: {native:0.00}MB\nTotal: {total:0.00}MB");
76129
#else
77-
Debug.Log("Not supported on this platform");
130+
Debug.Log($"{nameof(WebGlPlugins)}.{nameof(LogMemory)} called");
78131
#endif
79132
}
80133

@@ -88,7 +141,7 @@ public static float GetTotalMemorySize()
88141
var bytes = _GetTotalMemorySize();
89142
return GetMegaBytes(bytes);
90143
#else
91-
Debug.Log("Not supported on this platform");
144+
Debug.Log($"{nameof(WebGlPlugins)}.{nameof(GetTotalMemorySize)} called");
92145
return -1f;
93146
#endif
94147
}
@@ -103,7 +156,7 @@ public static float GetStaticMemorySize()
103156
var bytes = _GetStaticMemorySize();
104157
return GetMegaBytes(bytes);
105158
#else
106-
Debug.Log("Not supported on this platform");
159+
Debug.Log($"{nameof(WebGlPlugins)}.{nameof(GetStaticMemorySize)} called");
107160
return -1f;
108161
#endif
109162
}
@@ -118,7 +171,7 @@ public static float GetDynamicMemorySize()
118171
var bytes = _GetStaticMemorySize();
119172
return GetMegaBytes(bytes);
120173
#else
121-
Debug.Log("Not supported on this platform");
174+
Debug.Log($"{nameof(WebGlPlugins)}.{nameof(GetDynamicMemorySize)} called");
122175
return -1f;
123176
#endif
124177
}
@@ -132,7 +185,7 @@ public static float GetNativeMemorySize()
132185
#if UNITY_WEBGL && !UNITY_EDITOR
133186
return GetDynamicMemorySize() + GetStaticMemorySize();
134187
#else
135-
Debug.Log("Not supported on this platform");
188+
Debug.Log($"{nameof(WebGlPlugins)}.{nameof(GetNativeMemorySize)} called");
136189
return -1f;
137190
#endif
138191
}

Assets/Plugins/WebGL/WebGlPlugins.jslib

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var WebGlPlugins =
22
{
3-
_LogStartTime: function (startTime, unityVersion, webGlVersion) {
3+
_SetStringVariable: function(variableName, variableValue) {
44
//convert the string from unity to javascript strings
55
function toString(unityString) {
66
if(typeof UTF8ToString !== 'undefined') {
@@ -14,15 +14,49 @@
1414
return unityString;
1515
}
1616

17-
var startTimeText = toString(startTime);
18-
var unityVersionText = toString(unityVersion);
19-
var webGlVersionText = toString(webGlVersion);
17+
const variableNameText = toString(variableName);
18+
const variableValueText = toString(variableValue);
19+
window[variableNameText] = variableValueText;
20+
},
21+
22+
_AddTimeTrackingEvent: function(eventName) {
23+
//convert the string from unity to javascript strings
24+
function toString(unityString) {
25+
if(typeof UTF8ToString !== 'undefined') {
26+
return UTF8ToString(unityString);
27+
}
28+
29+
if (typeof Pointer_stringify !== 'undefined') {
30+
return Pointer_stringify(unityString);
31+
}
32+
33+
return unityString;
34+
}
35+
36+
if(typeof unityTimeTrackers == 'undefined') {
37+
unityTimeTrackers = new Map();
38+
}
39+
40+
const eventNameText = toString(eventName);
41+
const currentTime = performance.now();
42+
unityTimeTrackers.set(eventNameText, currentTime);
43+
44+
if(typeof onAddTimeTracker !== 'undefined') {
45+
onAddTimeTracker(eventNameText);
46+
}
2047

21-
if(typeof unityLoadingFinished !== 'undefined') {
22-
unityLoadingFinished(startTimeText, unityVersionText, webGlVersionText);
48+
console.log('Time tracker event' +eventNameText +': ' + currentTime);
49+
},
50+
51+
_ShowInfoPanel: function () {
52+
if(typeof setInfoPanelVisible !== 'undefined') {
53+
setInfoPanelVisible(true);
2354
}
24-
else {
25-
console.info("Unity " + unityVersionText + " (" + webGlVersionText + ") real time since startup: " + startTimeText);
55+
},
56+
57+
_HideInfoPanel: function () {
58+
if(typeof setInfoPanelVisible !== 'undefined') {
59+
setInfoPanelVisible(false);
2660
}
2761
},
2862

0 commit comments

Comments
 (0)