Read Text from Clipboard

前端 未结 1 772
鱼传尺愫
鱼传尺愫 2021-02-15 12:37

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://

相关标签:
1条回答
  • 2021-02-15 12:42

    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

    0 讨论(0)
提交回复
热议问题