File tree Expand file tree Collapse file tree 5 files changed +61
-2
lines changed Expand file tree Collapse file tree 5 files changed +61
-2
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change @@ -66,6 +66,25 @@ public void LogMemory()
6666WebToolPlugins . 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" ) ]
193212public 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
195217var gameObjects = GameObject . FindObjectsOfType < GameObject > ( ) . Where ( go => go . name == name ) . ToArray ( ) ;
218+ #endif
196219if ( gameObjects . Length == 0 )
197220{
198221Debug . Log ( $ "No GameObject found with the name: { name } ") ;
Original file line number Diff line number Diff line change @@ -70,12 +70,14 @@ private static void AddAllWebCommands()
7070private static void SetGlobalVariables ( )
7171{
7272var graphicsDevice = SystemInfo . graphicsDeviceType ;
73- string webGraphics = string . Empty ;
73+ string webGraphics ;
7474switch ( graphicsDevice )
7575{
76+ #if ! UNITY_2023_1_OR_NEWER
7677case GraphicsDeviceType . OpenGLES2:
7778webGraphics = "WebGL 1" ;
7879break ;
80+ #endif
7981case GraphicsDeviceType . OpenGLES3:
8082webGraphics = "WebGL 2" ;
8183break ;
Original file line number Diff line number Diff line change @@ -35,6 +35,8 @@ public static class WebToolPlugins
3535[ DllImport ( "__Internal" ) ]
3636private static extern uint _GetTotalMemorySize ( ) ;
3737[ DllImport ( "__Internal" ) ]
38+ private static extern uint _GetDeviceMemorySize ( ) ;
39+ [ DllImport ( "__Internal" ) ]
3840private static extern bool _CopyToClipboard ( string text ) ;
3941[ DllImport ( "__Internal" ) ]
4042private 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>
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments