@@ -35,9 +35,14 @@ public static class WebToolPlugins
3535 [ DllImport ( "__Internal" ) ]
3636 private static extern uint _GetTotalMemorySize ( ) ;
3737 [ DllImport ( "__Internal" ) ]
38- private static extern uint _GetStaticMemorySize ( ) ;
38+ private static extern bool _CopyToClipboard ( string text ) ;
3939 [ DllImport ( "__Internal" ) ]
40- private static extern uint _GetDynamicMemorySize ( ) ;
40+ private static extern int _IsOnline ( ) ;
41+ [ DllImport ( "__Internal" ) ]
42+ private static extern void _DownloadFile ( string filename , string content ) ;
43+ [ DllImport ( "__Internal" ) ]
44+ private static extern void _DownloadBlob ( string filename , byte [ ] byteArray , int byteLength , string mimeType ) ;
45+
4146#endif
4247
4348 private static bool _infoPanelVisible = false ;
@@ -154,106 +159,125 @@ public static bool IsMobileDevice()
154159 userAgent . Contains ( "Android" ) ;
155160 }
156161
157- /// <summary>
158- /// Get the total memory size used by the application in MB
159- /// </summary>
160- /// <returns>Size in MB</returns>
161- public static float GetTotalMemorySize ( )
162- {
163- #if UNITY_WEBGL && ! UNITY_EDITOR
164- var bytes = _GetTotalMemorySize ( ) ;
165- return GetMegaBytes ( bytes ) ;
166- #elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
167- Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( GetTotalMemorySize ) } called") ;
168- return - 1f ;
169- #else
170- return - 1f ;
171- #endif
172- }
173-
174162 /// <summary>
175163 /// Log all current memory data in MB
176164 /// </summary>
177165 public static void LogMemory ( )
178166 {
179167#if UNITY_WEBGL && ! UNITY_EDITOR
180168 var managed = GetManagedMemorySize ( ) ;
181- var native = GetNativeMemorySize ( ) ;
182169 var total = GetTotalMemorySize ( ) ;
183- Debug . Log ( $ "Memory stats:\n Managed: { managed : 0.00} MB\n Native: { native : 0.00 } MB \ n Total: { total : 0.00} MB") ;
170+ Debug . Log ( $ "Memory stats:\n Managed: { managed : 0.00} MB\n Total: { total : 0.00} MB") ;
184171#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
185172 Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( LogMemory ) } called") ;
186173#endif
187174 }
188175
189176 /// <summary>
190- /// Get the static memory size used by the application in MB
177+ /// Get the total memory size used by the application in MB
191178 /// </summary>
192179 /// <returns>Size in MB</returns>
193- public static float GetStaticMemorySize ( )
180+ public static float GetTotalMemorySize ( )
194181 {
195182#if UNITY_WEBGL && ! UNITY_EDITOR
196- var bytes = _GetStaticMemorySize ( ) ;
183+ var bytes = _GetTotalMemorySize ( ) ;
197184 return GetMegaBytes ( bytes ) ;
198185#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
199- Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( GetStaticMemorySize ) } called") ;
186+ Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( GetTotalMemorySize ) } called") ;
200187 return - 1f ;
201188#else
202189 return - 1f ;
203190#endif
204191 }
205192
206193 /// <summary>
207- /// Get the dynamic memory size used by the application in MB
194+ /// Get the managed memory size used by the application in MB
208195 /// </summary>
209196 /// <returns>Size in MB</returns>
210- public static float GetDynamicMemorySize ( )
197+ public static float GetManagedMemorySize ( )
211198 {
212- #if UNITY_WEBGL && ! UNITY_EDITOR
213- var bytes = _GetStaticMemorySize ( ) ;
199+ var bytes = ( uint ) GC . GetTotalMemory ( false ) ;
214200 return GetMegaBytes ( bytes ) ;
215- #elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
216- Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( GetDynamicMemorySize ) } called") ;
217- return - 1f ;
218- #else
219- return - 1f ;
220- #endif
221201 }
222202
223203 /// <summary>
224- /// Get the native memory size used by the application in MB (Static + Dynamic memory )
204+ /// Converts bytes (B) to mega bytes (MB )
225205 /// </summary>
226- /// <returns>Size in MB</returns>
227- public static float GetNativeMemorySize ( )
206+ /// <param name="bytes">bytes to convert</param>
207+ /// <returns>bytes / (1024 * 1024)</returns>
208+ private static float GetMegaBytes ( uint bytes )
228209 {
229- #if UNITY_WEBGL && ! UNITY_EDITOR
230- return GetDynamicMemorySize ( ) + GetStaticMemorySize ( ) ;
231- #elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
232- Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( GetNativeMemorySize ) } called") ;
233- return - 1f ;
234- #else
235- return - 1f ;
236- #endif
210+ return ( float ) bytes / ( 1024 * 1024 ) ;
237211 }
238212
239213 /// <summary>
240- /// Get the managed memory size used by the application in MB
214+ /// Copies the specified text to the system clipboard using the browser's clipboard API.
215+ /// Only works in WebGL builds and requires clipboard-write permission in modern browsers.
241216 /// </summary>
242- /// <returns>Size in MB</returns>
243- public static float GetManagedMemorySize ( )
217+ /// <param name="text">The text to copy to the clipboard</param>
218+ /// <returns>True if the copy operation was successful, false otherwise</returns>
219+ public static void CopyToClipboard ( string text )
244220 {
245- var bytes = ( uint ) GC . GetTotalMemory ( false ) ;
246- return GetMegaBytes ( bytes ) ;
221+ #if UNITY_WEBGL && ! UNITY_EDITOR
222+ _CopyToClipboard ( text ) ;
223+ #elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
224+ Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( CopyToClipboard ) } called with: { text } ") ;
225+ #endif
247226 }
248227
249228 /// <summary>
250- /// Converts bytes (B) to mega bytes (MB)
229+ /// Checks if the browser currently has an internet connection using the navigator.onLine property.
251230 /// </summary>
252- /// <param name="bytes">bytes to convert</param>
253- /// <returns>bytes / (1024 * 1024)</returns>
254- private static float GetMegaBytes ( uint bytes )
231+ /// <returns>True if the browser is online, false if it's offline</returns>
232+ public static bool IsOnline ( )
255233 {
256- return ( float ) bytes / ( 1024 * 1024 ) ;
234+ #if UNITY_WEBGL && ! UNITY_EDITOR
235+ return _IsOnline ( ) == 1 ;
236+ #elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
237+ Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( IsOnline ) } called") ;
238+ return true ;
239+ #else
240+ return true ;
241+ #endif
242+ }
243+
244+ /// <summary>
245+ /// Downloads a text file through the browser with the specified filename and content.
246+ /// Creates a temporary anchor element to trigger the download.
247+ /// </summary>
248+ /// <param name="filename">The name of the file to be downloaded</param>
249+ /// <param name="content">The text content to be saved in the file</param>
250+ public static void DownloadTextFile ( string filename , string content )
251+ {
252+ #if UNITY_WEBGL && ! UNITY_EDITOR
253+ _DownloadFile ( filename , content ) ;
254+ #elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
255+ Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( DownloadTextFile ) } called with filename: { filename } ") ;
256+ #endif
257+ }
258+
259+ /// <summary>
260+ /// Downloads a binary file through the browser with the specified filename and data.
261+ /// Creates a Blob with the specified MIME type and triggers the download.
262+ /// </summary>
263+ /// <param name="filename">The name of the file to be downloaded</param>
264+ /// <param name="data">The binary data to be saved in the file</param>
265+ /// <param name="mimeType">The MIME type of the file (defaults to "application/octet-stream")</param>
266+ /// <example>
267+ /// <code>
268+ /// // Example: Save a Texture2D as PNG
269+ /// Texture2D texture;
270+ /// byte[] pngData = texture.EncodeToPNG();
271+ /// WebToolPlugins.DownloadBinaryFile("texture.png", pngData, "image/png");
272+ /// </code>
273+ /// </example>
274+ public static void DownloadBinaryFile ( string filename , byte [ ] data , string mimeType = "application/octet-stream" )
275+ {
276+ #if UNITY_WEBGL && ! UNITY_EDITOR
277+ _DownloadBlob ( filename , data , data . Length , mimeType ) ;
278+ #elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
279+ Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( DownloadBinaryFile ) } called with filename: { filename } ") ;
280+ #endif
257281 }
258282 }
259283}
0 commit comments