10

I am trying to read the text in the clipboard in C# in Unity and then set it to a variable.

I have seen this article however it doesn't seem to work in Unity: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.clipboard.gettext

I just want to be able to read plain text. No images or anything. I have also found a few other articles on this however none of the code works in Unity.

funie200
  • 3,688
  • 5
  • 21
  • 34
Andrew
  • 357
  • 2
  • 6
  • 13
  • 2
    Can you show the code in your Unity3D C# script? Also, when exactly do you want to access the clipboard data? – Lars Kristensen Mar 08 '16 at 12:44
  • 1
    What code are you using to access the data? Why isn't it working?... – TheLethalCoder Mar 08 '16 at 13:19
  • 1
    I want to access it at the start when the program loads up. – Andrew Mar 08 '16 at 14:04
  • 1
    I was using something along the lines of if Clipboard.ContainsText() but 'clipboard' was not recognized. – Andrew Mar 08 '16 at 14:04
  • 1
    What platform? Windows or Mac or all? – Programmer Mar 08 '16 at 14:05
  • 1
    I am currently working on windows but code that works on both would be great. – Andrew Mar 08 '16 at 14:26
  • 1
    I can confirm in Unity 5.6, this method does not work. That is, if you ACTUALLY have text on clipboard, it'll work. However, if you have an image on clipboard and try to get text (or even CHECK if it's text), clipboard will break for the entire OS until the app+unity is shutdown (both compiled+editor). Head using Clipboard with warning! – dylanh724 Jul 29 '17 at 08:48

1 Answers1

23

I made a quick example to show how to use the Clipboard class from the System.Windows.Forms namespace. It turns out, that the method needed the [STAThread] method attribute to work. I don't know if that is possible to use in a Unity3D C# script.

[STAThread]
static void Main(string[] args)
{
    if (Clipboard.ContainsText(TextDataFormat.Text))
    {
        string clipboardText = Clipboard.GetText(TextDataFormat.Text);
        // Do whatever you need to do with clipboardText
    }
}

To learn more about what the attribute is used for, have a look at this question (and more importantly, its answers): What does [STAThread] do?

EDIT:

I did a little bit of digging, and it looks like Unity3D has a wrapper for the System Clipboard. I haven't tried it yet, but it looks like it should work across different operating systems and not just for Windows: GUIUtility.systemCopyBuffer

Community
  • 1
  • 1
Lars Kristensen
  • 1,410
  • 19
  • 29
  • 1
    Can't use this in Unity3D. Even he is able to import the required DLL, it wont work on other platforms. It would only work on Windows. – Programmer Mar 08 '16 at 14:03
  • 3
    @Programmer That's true. I found a link to the Unity3D docs about `EditorGUIUtility.systemCopyBuffer` which looks like it can be used to access the clipboard of the OS. I've edited my answer to include the link, but I haven't tried it out myself yet. – Lars Kristensen Mar 08 '16 at 14:13
  • 2
    +1. EditorGUIUtility.systemCopyBuffer is for the Unity Editor only. GUIUtility.systemCopyBuffer is the right Class/Method. Update your answer. This will work for all platform except Web/WebGL. – Programmer Mar 08 '16 at 14:15
  • 1
    @Programmer, Thanks, I've updated my answer with the link to the docs for `GUIUtility.systemCopyBuffer`, but the link doesn't have much information, and I couldn't find any examples. I'll have to give it a try in Unity later, to see if / how it works. – Lars Kristensen Mar 08 '16 at 14:22
  • 2
    np. I tested it before commenting on your answer. It works. By the way, EditorGUIUtility inherits from GUIUtility. – Programmer Mar 08 '16 at 14:26
  • 1
    Thanks guys! I will test this later. – Andrew Mar 08 '16 at 14:30