Skip to content

Commit 88daecd

Browse files
committed
Merge branch 'master' of https://github.com/JohannesDeml/UnityWebGL-LoadingTest into 6000.0
2 parents a5e21a9 + 041182e commit 88daecd

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

.github/templates/upgrade-unity-pr-body.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
### Built version demos
66

77
* https://deml.io/experiments/unity-webgl/{{ .unityversion }}-webgl2
8-
* https://deml.io/experiments/unity-webgl/{{ .unityversion }}-webgl1
8+
* https://deml.io/experiments/unity-webgl/{{ .unityversion }}-webgl2-debug
9+
* https://deml.io/experiments/unity-webgl/{{ .unityversion }}-minsize-webgl2
910

1011
### Other links
1112

Assets/Plugins/WebGL/WebBridge/CommonCommands.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ public void LogMemory()
6666
WebToolPlugins.LogMemory();
6767
}
6868

69+
/// <summary>
70+
/// Logs the rough device memory capped between 0.25 and 8 GB
71+
/// Browser Usage: <code>unityGame.SendMessage("WebGL","LogDeviceMemory");</code>
72+
/// </summary>
73+
[WebCommand(Description = "Logs the device memory")]
74+
[ContextMenu(nameof(LogDeviceMemory))]
75+
public void LogDeviceMemory()
76+
{
77+
float deviceMemoryInMb = WebToolPlugins.GetDeviceMemory();
78+
if (deviceMemoryInMb > 0)
79+
{
80+
Debug.Log($"Device Memory: {deviceMemoryInMb} GB");
81+
}
82+
else
83+
{
84+
Debug.Log("Device Memory information is not available on this device or browser.");
85+
}
86+
}
87+
6988
/// <summary>
7089
/// Allocate memory to test memory usage and limits
7190
/// The memory will be stored in a list to prevent it from being garbage collected
@@ -192,7 +211,11 @@ public void LogInitializationTime()
192211
[WebCommand(Description = "Find GameObject by name and log its components")]
193212
public void FindGameObjectByName(string name)
194213
{
214+
#if UNITY_6000_0_OR_NEWER
215+
var gameObjects = GameObject.FindObjectsByType<GameObject>(FindObjectsSortMode.None).Where(go => go.name == name).ToArray();
216+
#else
195217
var gameObjects = GameObject.FindObjectsOfType<GameObject>().Where(go => go.name == name).ToArray();
218+
#endif
196219
if (gameObjects.Length == 0)
197220
{
198221
Debug.Log($"No GameObject found with the name: {name}");

Assets/Plugins/WebGL/WebBridge/WebBridge.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ private static void AddAllWebCommands()
7070
private static void SetGlobalVariables()
7171
{
7272
var graphicsDevice = SystemInfo.graphicsDeviceType;
73-
string webGraphics = string.Empty;
73+
string webGraphics;
7474
switch (graphicsDevice)
7575
{
76+
#if !UNITY_2023_1_OR_NEWER
7677
case GraphicsDeviceType.OpenGLES2:
7778
webGraphics = "WebGL 1";
7879
break;
80+
#endif
7981
case GraphicsDeviceType.OpenGLES3:
8082
webGraphics = "WebGL 2";
8183
break;

Assets/Plugins/WebGL/WebTools/WebToolPlugins.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public static class WebToolPlugins
3535
[DllImport("__Internal")]
3636
private static extern uint _GetTotalMemorySize();
3737
[DllImport("__Internal")]
38+
private static extern uint _GetDeviceMemorySize();
39+
[DllImport("__Internal")]
3840
private static extern bool _CopyToClipboard(string text);
3941
[DllImport("__Internal")]
4042
private static extern int _IsOnline();
@@ -191,6 +193,29 @@ public static float GetTotalMemorySize()
191193
#endif
192194
}
193195

196+
/// <summary>
197+
/// Get the device memory size in MB if supported by the browser
198+
/// Uses navigator.deviceMemory which is supported by chromium based browsers
199+
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator/deviceMemory"/>
200+
/// </summary>
201+
/// <returns>Size in MB or -1 if not supported</returns>
202+
public static float GetDeviceMemory()
203+
{
204+
#if UNITY_WEBGL && !UNITY_EDITOR
205+
var gb = _GetDeviceMemorySize();
206+
if (gb > 0)
207+
{
208+
return gb * 1024f; // convert to MB
209+
}
210+
return -1f;
211+
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
212+
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(GetDeviceMemory)} called");
213+
return -1f;
214+
#else
215+
return -1f;
216+
#endif
217+
}
218+
194219
/// <summary>
195220
/// Get the managed memory size used by the application in MB
196221
/// </summary>

Assets/Plugins/WebGL/WebTools/WebToolPlugins.jslib

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ var WebGlPlugins =
9999
return -1;
100100
},
101101

102+
_GetDeviceMemorySize: function()
103+
{
104+
if(navigator.deviceMemory) {
105+
return navigator.deviceMemory;
106+
}
107+
return -1;
108+
},
109+
102110
_CopyToClipboard: function(text) {
103111
var str = UTF8ToString(text);
104112
navigator.clipboard.writeText(str)

0 commit comments

Comments
 (0)