File tree Expand file tree Collapse file tree 3 files changed +50
-0
lines changed
Expand file tree Collapse file tree 3 files changed +50
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -191,6 +191,29 @@ public static float GetTotalMemorySize()
191191#endif
192192 }
193193
194+ /// <summary>
195+ /// Get the device memory size in MB if supported by the browser
196+ /// Uses navigator.deviceMemory which is supported by chromium based browsers
197+ /// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator/deviceMemory"/>
198+ /// </summary>
199+ /// <returns>Size in MB or -1 if not supported</returns>
200+ public static float GetDeviceMemory ( )
201+ {
202+ #if UNITY_WEBGL && ! UNITY_EDITOR
203+ var gb = _GetDeviceMemorySize ( ) ;
204+ if ( gb > 0 )
205+ {
206+ return gb * 1024f ; // convert to MB
207+ }
208+ return - 1f ;
209+ #elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
210+ Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( GetDeviceMemory ) } called") ;
211+ return - 1f ;
212+ #else
213+ return - 1f ;
214+ #endif
215+ }
216+
194217 /// <summary>
195218 /// Get the managed memory size used by the application in MB
196219 /// </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