I am having trouble copying specific text to the clipboard in a game I developed in Unity that I am hosting directly in the browser on Itch.io.
The code I implemented works perfectly in the editor and when I run the application using Visual Studio Code (Live Server), but never in the application hosted on the Itch.io website.
I want to emphasize that I am not violating any Itch.io guidelines. In fact, I was able to copy texts from other games, most of which were made in Python (mainly visual novels) and one in Scratch. I searched for several games developed in Unity, but none of those I found had any visible input fields or text chat, and given the sampling, I found that sufficient.
Below are two screenshots proving that it is possible to copy:
Initially, I tried passing the text that I wanted to be sent to the clipboard in a method that was called upon clicking a button.
Code.cs:
#if UNITY_WEBGL && !UNITY_EDITOR
using System.Runtime.InteropServices;
#endif
//usings and class declaration...
#if UNITY_WEBGL && !UNITY_EDITOR
[DllImport("__Internal")]
static extern void CopyToClipboard(string copyText);
#endif
public void WordToGepeto()
{
string _word = "";
//logic...
_word = _word.TrimEnd();
CopiarParaClipboard(_word);
}
public void CopiarParaClipboard(string _texto)
{
#if UNITY_WEBGL && !UNITY_EDITOR
CopyToClipboard(_texto);
#else
GUIUtility.systemCopyBuffer = _texto;
#endif
}
Code.js (inside the Plugins folder located in Assets):
mergeInto(LibraryManager.library, {
CopyToClipboard: function(copyText) {
var text = UTF8ToString(copyText);
navigator.clipboard.writeText(text).then(function() {
console.log('Texto copiado para a área de transferência com sucesso!');
}).catch(function(error) {
console.error('Falha ao copiar texto: ', error);
});
}
});
This was the error that appeared:
[Violation] Permissions policy violation: The Clipboard API has been blocked because of a permissions policy applied to the current document. See https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-permissions-in-cross-origin-iframes for more details.
Forca.framework.js:3 Falha ao copiar texto: NotAllowedError: Failed to execute 'writeText' on 'Clipboard': The Clipboard API has been blocked because of a permissions policy applied to the current document. See https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-permissions-in-cross-origin-iframes for more details.
The link does not lead anywhere (Error 404).
After that, I tried to modify the iframe, but it's only possible to read it.
Later, I gave up using a button and created an InputField to manually copy the text (using control + c). To my complete surprise, even that didn’t work. No error appears, it just doesn’t copy.
Below are other things I tried before coming to ask for help:
I changed the Unity version (from 2020.3.37f1 to 2021.3.18f1).
I tried adding this code (
<meta http-equiv="Permissions-Policy" content="clipboard-write=(self)">) in the header of the index.html file in the build.I switched browsers (I tried Edge, as I was previously using Chrome), and it presented the same error that was appearing before.
In a final attempt, I tried changing the Chrome settings, trying to force access to the clipboard (see image below), and even disabling its default protection, but nothing worked.
I will be immensely grateful for any help provided.





