unity探索者之复制内容到剪贴板
Posted 半颗星辰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity探索者之复制内容到剪贴板相关的知识,希望对你有一定的参考价值。
版权声明:本文为原创文章,转载请声明http://www.cnblogs.com/unityExplorer/p/8417014.html
unity中提供的TextEditor类,只能在windows环境下使用,对于安卓和ios,就不得不使用原生代码了
在unity中使用宏区分下平台:
public void CopyToClipboard(string input) { #if UNITY_EDITOR TextEditor t = new TextEditor(); t.text = input; t.OnFocus(); t.Copy(); #elif UNITY_IPHONE CopyTextToClipboard_iOS(input); #elif UNITY_android AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); AndroidJavaClass tool = new AndroidJavaClass("com.my.ugcf.Tool"); tool.CallStatic("CopyTextToClipboard", currentActivity, input); #endif }
java:
public static void CopyTextToClipboard(final Context activity, final String str) throws Exception { if (Looper.myLooper() == null){ Looper.prepare(); } clipboard = (ClipboardManager) activity.getSystemService(Activity.CLIPBOARD_SERVICE); ClipData textCd = ClipData.newPlainText("data", str); clipboard.setPrimaryClip(textCd); }
objective-c:
void CopyTextToClipboard_iOS(const char *text) { UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = _CreateNSString(text); }
以上是关于unity探索者之复制内容到剪贴板的主要内容,如果未能解决你的问题,请参考以下文章