File tree Expand file tree Collapse file tree 3 files changed +52
-0
lines changed
Expand file tree Collapse file tree 3 files changed +52
-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 @@ -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>
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