- Home /
Copy text in TextField to windows clipboard
I have a TextField and tow buttons.
One button for copying text of TextField to clipboard and one for paste from clipboard.
I have this code for copy but it doesn't work.
How can I do it in JS script?
Thanks.
import System.Collections;
var x : TextEditor;
x.content.text=GUI.TextField(...);
x.Copy();
Answer by Ryuuguu · Oct 16, 2015 at 07:54 AM
On OS X editor and OS X standalone GUIUtility.systemCopyBuffer is the clipboard. I believe this works on all platforms but I have not tested it yet. http://docs.unity3d.com/ScriptReference/GUIUtility-systemCopyBuffer.html so
GUIUtility.systemCopyBuffer = x.content.text
is all you need.
I can verify that this also works in a Windows environment.
Answer by Fornoreason1000 · May 26, 2015 at 08:37 AM
check this thread: http://stackoverflow.com/questions/899350/how-to-copy-the-contents-of-a-string-to-the-clipboard-in-c
Before you say it, I'm going to tell you that it doesn't matter that this in in C#. you can still use it in UnityScript (also known as Javascript) due to Unity using Mono which is pretty much .NET anyway.
well it doesn't matter what kinda of text you have as long as you can get a string value from it you can use. you can pretty much use the Clipboard class provided in the System.Windows namespace.
Here is a small example.
import System.Collections;
import System.Windows;
function OnGUI() {
if(GUI.Button(new Rect(50,50,50,50), "Copy") {
Clipboard.SetText( x.content.text);
}
if(GUI.Button(new Rect(120,50,50,50), "Paste") {
x.content.text = Clipboard.GetText();
}
}
For further reference on the clipboard class see here
Link: https://msdn.microsoft.com/en-us/library/System.Windows.Clipboard(v=vs.110).aspx
Hope it helps
Thanks for your answer. It seems that System.Windows isn't supported in unity and I can't use it.
then the only other options is to use System.Windows.Forms, but if the namespace is "not supported"(which is pretty strange). then you literally cannot copy the text to the windows clipboard but there is a thread that has solves this.
http://answers.unity3d.com/questions/266244/how-can-i-add-copypaste-clipboard-support-to-my-ga.html
In this thread the answer ruses the Internal GUI clipboard, but the the second Answer has a possible solution in the asset store. i the asset store
I still find it very strange that System,Windows isn't supported, (probably because $$anonymous$$AC, Linux and all the other platforms can't use it).