Skip to content

Commit 76fb878

Browse files
committed
Cross platform copy utility
1 parent 4400c69 commit 76fb878

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

Assets/Thirdweb/Core/Plugin/thirdweb.jslib

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,26 @@ var plugin = {
507507
dynCall_viii(cb, idPtr, null, buffer);
508508
});
509509
},
510+
ThirdwebCopyBuffer: async function (taskId, text, cb) {
511+
// convert taskId from pointer to str and allocate it to keep in memory
512+
var id = UTF8ToString(taskId);
513+
var idSize = lengthBytesUTF8(id) + 1;
514+
var idPtr = _malloc(idSize);
515+
stringToUTF8(id, idPtr, idSize);
516+
// execute bridge call
517+
window.bridge
518+
.copyBuffer(UTF8ToString(text))
519+
.then(() => {
520+
dynCall_viii(cb, idPtr, idPtr, null);
521+
})
522+
.catch((err) => {
523+
var msg = err.message;
524+
var bufferSize = lengthBytesUTF8(msg) + 1;
525+
var buffer = _malloc(bufferSize);
526+
stringToUTF8(msg, buffer, bufferSize);
527+
dynCall_viii(cb, idPtr, null, buffer);
528+
});
529+
},
510530
};
511531

512532
mergeInto(LibraryManager.library, plugin);

Assets/Thirdweb/Core/Scripts/Bridge.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,22 @@ public static async Task<string> ResolveAddressFromENS(string ens)
450450
return JsonConvert.DeserializeObject<Result<string>>(result).result;
451451
}
452452

453+
public static async Task CopyBuffer(string text)
454+
{
455+
if (!Utils.IsWebGLBuild())
456+
{
457+
ThirdwebDebug.LogWarning("Interacting with the thirdweb SDK is not fully supported in the editor.");
458+
return;
459+
}
460+
string taskId = Guid.NewGuid().ToString();
461+
var task = new TaskCompletionSource<string>();
462+
taskMap[taskId] = task;
463+
#if UNITY_WEBGL
464+
ThirdwebCopyBuffer(taskId, text, jsCallback);
465+
#endif
466+
await task.Task;
467+
}
468+
453469
#if UNITY_WEBGL
454470
[DllImport("__Internal")]
455471
private static extern string ThirdwebInvoke(string taskId, string route, string payload, Action<string, string, string> cb);
@@ -495,6 +511,8 @@ public static async Task<string> ResolveAddressFromENS(string ens)
495511
private static extern string ThirdwebResolveENSFromAddress(string taskId, string address, Action<string, string, string> cb);
496512
[DllImport("__Internal")]
497513
private static extern string ThirdwebResolveAddressFromENS(string taskId, string ens, Action<string, string, string> cb);
514+
[DllImport("__Internal")]
515+
private static extern string ThirdwebCopyBuffer(string taskId, string text, Action<string, string, string> cb);
498516
#endif
499517
}
500518
}

Assets/Thirdweb/Core/Scripts/Utils.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,5 +739,22 @@ public static string GenerateRandomString(int v)
739739
var result = new string(Enumerable.Repeat(chars, v).Select(s => s[random.Next(s.Length)]).ToArray());
740740
return result;
741741
}
742+
743+
public static async Task<bool> CopyToClipboard(string text)
744+
{
745+
try
746+
{
747+
if (IsWebGLBuild())
748+
await Bridge.CopyBuffer(text);
749+
else
750+
GUIUtility.systemCopyBuffer = text;
751+
return true;
752+
}
753+
catch (Exception e)
754+
{
755+
ThirdwebDebug.LogWarning($"Failed to copy to clipboard: {e}");
756+
return false;
757+
}
758+
}
742759
}
743760
}

Assets/Thirdweb/Examples/Scripts/Prefabs/Prefab_ConnectWallet.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ public void ConnectEmail()
128128
email: emailInput.text,
129129
authOptions: new AuthOptions(AuthProvider.EmailOTP)
130130
);
131-
Connect(wc);
132131
}
133132
else
134133
{
@@ -272,13 +271,13 @@ public async void ExportWallet()
272271
{
273272
ThirdwebDebug.Log("Exporting wallet...");
274273
string json = await ThirdwebManager.Instance.SDK.Wallet.Export(_password);
275-
GUIUtility.systemCopyBuffer = json;
274+
await Utils.CopyToClipboard(json);
276275
ThirdwebDebug.Log($"Copied wallet to clipboard: {json}");
277276
}
278277

279-
public void CopyAddress()
278+
public async void CopyAddress()
280279
{
281-
GUIUtility.systemCopyBuffer = _address;
280+
await Utils.CopyToClipboard(_address);
282281
ThirdwebDebug.Log($"Copied address to clipboard: {_address}");
283282
}
284283

Assets/WebGLTemplates/Thirdweb/lib/thirdweb-unity-bridge.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446041,7 +446041,7 @@ Code: ${s}`;
446041446041
}
446042446042
(globalThis.X_SDK_NAME = "UnitySDK_WebGL"),
446043446043
(globalThis.X_SDK_PLATFORM = "unity"),
446044-
(globalThis.X_SDK_VERSION = "4.9.2"),
446044+
(globalThis.X_SDK_VERSION = "4.10.0"),
446045446045
(globalThis.X_SDK_OS = s?.os ?? "unknown");
446046446046
}
446047446047
this.initializedChain = e;
@@ -446528,6 +446528,11 @@ Code: ${s}`;
446528446528
}).resolveName(e);
446529446529
return JSON.stringify({ result: r });
446530446530
}
446531+
async copyBuffer(e) {
446532+
navigator.clipboard.writeText(e).catch(function (t) {
446533+
console.error("Could not copy text: ", t);
446534+
});
446535+
}
446531446536
openPopupWindow() {
446532446537
let e = window.open("", void 0, "width=350, height=500");
446533446538
return (

0 commit comments

Comments
 (0)