@@ -34,6 +34,17 @@ public static class WebToolPlugins
34
34
private static extern string _GetUserAgent ( ) ;
35
35
[ DllImport ( "__Internal" ) ]
36
36
private static extern uint _GetTotalMemorySize ( ) ;
37
+ [ DllImport ( "__Internal" ) ]
38
+ private static extern bool _CopyToClipboard ( string text ) ;
39
+ [ DllImport ( "__Internal" ) ]
40
+ private static extern string _GetClipboardContent ( ) ;
41
+ [ DllImport ( "__Internal" ) ]
42
+ private static extern int _IsOnline ( ) ;
43
+ [ DllImport ( "__Internal" ) ]
44
+ private static extern void _DownloadFile ( string filename , string content ) ;
45
+ [ DllImport ( "__Internal" ) ]
46
+ private static extern void _DownloadBlob ( string filename , byte [ ] byteArray , int byteLength , string mimeType ) ;
47
+
37
48
#endif
38
49
39
50
private static bool _infoPanelVisible = false ;
@@ -200,5 +211,95 @@ private static float GetMegaBytes(uint bytes)
200
211
{
201
212
return ( float ) bytes / ( 1024 * 1024 ) ;
202
213
}
214
+
215
+ /// <summary>
216
+ /// Copies the specified text to the system clipboard using the browser's clipboard API.
217
+ /// Only works in WebGL builds and requires clipboard-write permission in modern browsers.
218
+ /// </summary>
219
+ /// <param name="text">The text to copy to the clipboard</param>
220
+ /// <returns>True if the copy operation was successful, false otherwise</returns>
221
+ public static bool CopyToClipboard ( string text )
222
+ {
223
+ #if UNITY_WEBGL && ! UNITY_EDITOR
224
+ return _CopyToClipboard ( text ) ;
225
+ #elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
226
+ Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( CopyToClipboard ) } called with: { text } ") ;
227
+ return false ;
228
+ #else
229
+ return false ;
230
+ #endif
231
+ }
232
+
233
+ /// <summary>
234
+ /// Retrieves the current content from the system clipboard using the browser's clipboard API.
235
+ /// Only works in WebGL builds and requires clipboard-read permission in modern browsers.
236
+ /// </summary>
237
+ /// <returns>The clipboard content as string, or empty string if clipboard is empty or access was denied</returns>
238
+ public static string GetClipboardContent ( )
239
+ {
240
+ #if UNITY_WEBGL && ! UNITY_EDITOR
241
+ return _GetClipboardContent ( ) ;
242
+ #elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
243
+ Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( GetClipboardContent ) } called") ;
244
+ return string . Empty ;
245
+ #else
246
+ return string . Empty ;
247
+ #endif
248
+ }
249
+
250
+ /// <summary>
251
+ /// Checks if the browser currently has an internet connection using the navigator.onLine property.
252
+ /// </summary>
253
+ /// <returns>True if the browser is online, false if it's offline</returns>
254
+ public static bool IsOnline ( )
255
+ {
256
+ #if UNITY_WEBGL && ! UNITY_EDITOR
257
+ return _IsOnline ( ) == 1 ;
258
+ #elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
259
+ Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( IsOnline ) } called") ;
260
+ return true ;
261
+ #else
262
+ return true ;
263
+ #endif
264
+ }
265
+
266
+ /// <summary>
267
+ /// Downloads a text file through the browser with the specified filename and content.
268
+ /// Creates a temporary anchor element to trigger the download.
269
+ /// </summary>
270
+ /// <param name="filename">The name of the file to be downloaded</param>
271
+ /// <param name="content">The text content to be saved in the file</param>
272
+ public static void DownloadTextFile ( string filename , string content )
273
+ {
274
+ #if UNITY_WEBGL && ! UNITY_EDITOR
275
+ _DownloadFile ( filename , content ) ;
276
+ #elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
277
+ Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( DownloadTextFile ) } called with filename: { filename } ") ;
278
+ #endif
279
+ }
280
+
281
+ /// <summary>
282
+ /// Downloads a binary file through the browser with the specified filename and data.
283
+ /// Creates a Blob with the specified MIME type and triggers the download.
284
+ /// </summary>
285
+ /// <param name="filename">The name of the file to be downloaded</param>
286
+ /// <param name="data">The binary data to be saved in the file</param>
287
+ /// <param name="mimeType">The MIME type of the file (defaults to "application/octet-stream")</param>
288
+ /// <example>
289
+ /// <code>
290
+ /// // Example: Save a Texture2D as PNG
291
+ /// Texture2D texture;
292
+ /// byte[] pngData = texture.EncodeToPNG();
293
+ /// WebToolPlugins.DownloadBinaryFile("texture.png", pngData, "image/png");
294
+ /// </code>
295
+ /// </example>
296
+ public static void DownloadBinaryFile ( string filename , byte [ ] data , string mimeType = "application/octet-stream" )
297
+ {
298
+ #if UNITY_WEBGL && ! UNITY_EDITOR
299
+ _DownloadBlob ( filename , data , data . Length , mimeType ) ;
300
+ #elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
301
+ Debug . Log ( $ "{ nameof ( WebToolPlugins ) } .{ nameof ( DownloadBinaryFile ) } called with filename: { filename } ") ;
302
+ #endif
303
+ }
203
304
}
204
305
}
0 commit comments