我在 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);
}
}
每当我以任何形式使用剪贴板时,即使检查它是否是文本,它也会破坏剪贴板,直到我关闭应用程序。现在,这是一个 Unity 错误吗? VS错误?有什么我不明白的吗?我应该用什么来代替?
Clipboard.ContainsText
来自 System.Windows.Forms
命名空间。 Unity 不支持这些。由于 Unity 使用 Mono,因此能够编译它就很幸运,并且能够正常工作也非常幸运。另外,它不可移植,因此不要在 Unity 中使用此命名空间中的任何内容。
我应该用什么来代替?
写入剪贴板:
GUIUtility.systemCopyBuffer = "Hello";
从剪贴板读取:
string clipBoard = GUIUtility.systemCopyBuffer;
这应该有效。如果没有,您可以使用他们的 C++ API 从头开始实现自己的剪贴板 API。您必须为每个平台执行此操作。
Unity 提供了一种更便携的方式来使用
GUIUtility.systemCopyBuffer
与剪贴板进行交互:
使用以下代码写入剪贴板:
GUIUtility.systemCopyBuffer = "Hello";
并从剪贴板读取:
string clipBoard = GUIUtility.systemCopyBuffer;