读写剪贴板

dyl*_*724 6 c# clipboard unity-game-engine clipboarddata

我在Unity 5.6上的Windows(VS2017社区)上有这个片段:

public static void setClipboardStr(string str)
{
    try
    {
        if (Clipboard.ContainsText())
        {
            // ...doesn't matter if true or false - 
            // from here on, I can't copy+paste inside 
            // the game or outside until I close the app. 
            // If I had an error instead of try/catch or 
            // check if it contains text, the error will 
            // remain UNTIL I REBOOT (beyond the app closing).
        }
    }
    catch(Exception ex)
    {
        Debug.LogError(ex);
    }
}
Run Code Online (Sandbox Code Playgroud)

每当我以任何形式使用剪贴板时,即使检查它是否是文本,它都会破坏剪贴板,直到我关闭应用程序.现在,这是一个Unity错误吗?VS bug?有什么我不理解的吗?我应该用什么呢?

Pro*_*mer 6

Clipboard.ContainsText来自System.Windows.Forms命名空间.Unity不支持这些.因为Unity使用Mono,所以很幸运能够编译它并且非常幸运能够正常工作.此外,这不是可移植的,因此不要在Unity中使用此命名空间中的任何内容.

我应该用什么呢?

写入剪贴板:

GUIUtility.systemCopyBuffer = "Hello";
Run Code Online (Sandbox Code Playgroud)

从剪贴板中读取:

string clipBoard = GUIUtility.systemCopyBuffer;
Run Code Online (Sandbox Code Playgroud)

应该工作.如果没有,您可以使用他们的C++ API从头开始实现自己的剪贴板API.您必须为每个平台执行此操作.

  • 欢迎你,不要气馁因为投票不足而要问未来的问题.代码和其他一切似乎都很好.有些人总是会投票.快乐的编码! (2认同)