Skip to content

Commit d97c3d6

Browse files
committed
Try to fix CopyToClipboard and SaveScreenshot
1 parent eb449ae commit d97c3d6

File tree

3 files changed

+12
-55
lines changed

3 files changed

+12
-55
lines changed

Assets/Plugins/WebGL/WebBridge/CommonCommands.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// --------------------------------------------------------------------------------------------------------------------
1010

1111
using System;
12+
using System.Collections;
1213
using System.Collections.Generic;
1314
using Supyrb.Attributes;
1415
using UnityEngine;
@@ -282,19 +283,7 @@ public void LogShaderCompilation(int enabled)
282283
[WebCommand(Description = "Copy text to clipboard")]
283284
public void CopyToClipboard(string text)
284285
{
285-
bool success = WebToolPlugins.CopyToClipboard(text);
286-
Debug.Log($"Copy to clipboard {(success ? "successful" : "failed")}: {text}");
287-
}
288-
289-
/// <summary>
290-
/// Get current clipboard content using the browser's clipboard API
291-
/// Note: Requires clipboard-read permission in modern browsers
292-
/// </summary>
293-
[WebCommand(Description = "Get current clipboard content")]
294-
public void GetClipboardContent()
295-
{
296-
string content = WebToolPlugins.GetClipboardContent();
297-
Debug.Log($"Clipboard content: {content}");
286+
WebToolPlugins.CopyToClipboard(text);
298287
}
299288

300289
/// <summary>
@@ -323,6 +312,14 @@ public void SaveScreenshot()
323312
[WebCommand(Description = "Save current screen as PNG with variable super size")]
324313
public void SaveScreenshot(int superSize)
325314
{
315+
StartCoroutine(CaptureScreenshot(superSize));
316+
}
317+
318+
private IEnumerator CaptureScreenshot(int superSize)
319+
{
320+
// Wait for the end of frame to ensure everything is rendered
321+
yield return new WaitForEndOfFrame();
322+
326323
string filename = "screenshot.png";
327324
try
328325
{

Assets/Plugins/WebGL/WebTools/WebToolPlugins.cs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public static class WebToolPlugins
3737
[DllImport("__Internal")]
3838
private static extern bool _CopyToClipboard(string text);
3939
[DllImport("__Internal")]
40-
private static extern string _GetClipboardContent();
41-
[DllImport("__Internal")]
4240
private static extern int _IsOnline();
4341
[DllImport("__Internal")]
4442
private static extern void _DownloadFile(string filename, string content);
@@ -218,32 +216,12 @@ private static float GetMegaBytes(uint bytes)
218216
/// </summary>
219217
/// <param name="text">The text to copy to the clipboard</param>
220218
/// <returns>True if the copy operation was successful, false otherwise</returns>
221-
public static bool CopyToClipboard(string text)
219+
public static void CopyToClipboard(string text)
222220
{
223221
#if UNITY_WEBGL && !UNITY_EDITOR
224-
return _CopyToClipboard(text);
222+
_CopyToClipboard(text);
225223
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
226224
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;
247225
#endif
248226
}
249227

Assets/Plugins/WebGL/WebTools/WebToolPlugins.jslib

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,9 @@ var WebGlPlugins =
103103
var str = UTF8ToString(text);
104104
navigator.clipboard.writeText(str)
105105
.then(function() {
106-
console.log('Text copied to clipboard');
107-
return true;
108106
})
109107
.catch(function(err) {
110108
console.error('Failed to copy text: ', err);
111-
return false;
112-
});
113-
},
114-
115-
_GetClipboardContent: function() {
116-
// Note: This requires clipboard-read permission
117-
navigator.clipboard.readText()
118-
.then(function(text) {
119-
var bufferSize = lengthBytesUTF8(text) + 1;
120-
var buffer = _malloc(bufferSize);
121-
stringToUTF8(text, buffer, bufferSize);
122-
return buffer;
123-
})
124-
.catch(function(err) {
125-
console.error('Failed to read clipboard: ', err);
126-
return null;
127109
});
128110
},
129111

0 commit comments

Comments
 (0)