Skip to content

Commit 056f436

Browse files
committed
CopyToClipboard Cross-Platform Utility/Extension
1 parent 368e5a7 commit 056f436

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

Assets/Thirdweb/Examples/Scripts/PlaygroundManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ private async void ConnectWallet(WalletOptions options)
125125
currentPanel.Action1Button.onClick.AddListener(async () =>
126126
{
127127
var address = await wallet.GetAddress();
128+
address.CopyToClipboard();
128129
Log(currentPanel.LogText, $"Address: {address}");
129130
});
130131

Assets/Thirdweb/Runtime/Unity/ThirdwebUnityExtensions.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
1+
using System;
12
using System.Numerics;
23
using System.Threading.Tasks;
34
using UnityEngine;
45
using ZXing;
56
using ZXing.QrCode;
67

8+
#if UNITY_WEBGL
9+
using System.Runtime.InteropServices;
10+
#endif
11+
712
namespace Thirdweb.Unity
813
{
914
public static class ThirdwebUnityExtensions
1015
{
16+
#if UNITY_WEBGL
17+
[DllImport("__Internal")]
18+
private static extern string ThirdwebCopyBuffer(string text);
19+
#endif
20+
21+
public static void CopyToClipboard(this string text)
22+
{
23+
try
24+
{
25+
if (Application.platform == RuntimePlatform.WebGLPlayer)
26+
{
27+
#if UNITY_WEBGL
28+
ThirdwebCopyBuffer(text);
29+
#endif
30+
}
31+
else
32+
{
33+
GUIUtility.systemCopyBuffer = text;
34+
}
35+
}
36+
catch (Exception e)
37+
{
38+
ThirdwebDebug.LogWarning($"Failed to copy to clipboard: {e}");
39+
}
40+
}
41+
1142
public static async Task<Sprite> GetNFTSprite(this NFT nft, ThirdwebClient client)
1243
{
1344
var bytes = await nft.GetNFTImageBytes(client);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
mergeInto(LibraryManager.library, {
2+
ThirdwebCopyBuffer: function (textPtr) {
3+
var text = UTF8ToString(textPtr);
4+
5+
if (navigator.clipboard && navigator.clipboard.writeText) {
6+
navigator.clipboard
7+
.writeText(text)
8+
.then(function () {
9+
console.log("Copied to clipboard:", text);
10+
})
11+
.catch(function (err) {
12+
console.warn("Failed to copy text with navigator.clipboard:", err);
13+
fallbackCopyText(text);
14+
});
15+
} else {
16+
fallbackCopyText(text);
17+
}
18+
19+
function fallbackCopyText(textToCopy) {
20+
var input = document.createElement("textarea");
21+
input.value = textToCopy;
22+
input.style.position = "absolute";
23+
input.style.left = "-9999px";
24+
document.body.appendChild(input);
25+
input.select();
26+
document.execCommand("copy");
27+
document.body.removeChild(input);
28+
console.log("Copied to clipboard using fallback:", textToCopy);
29+
}
30+
},
31+
});

Assets/Thirdweb/Runtime/Unity/WebGL/WebGLCopyBuffer.jslib.meta

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)