版权声明:本文为原创文章,转载请声明http://www.cnblogs.com/unityExplorer/p/8417014.html
unity中提供的TextEditor类,只能在windows环境下使用,对于安卓和iOS,就不得不使用原生代码了
在unity中使用宏区分下平台:
publicvoidCopyToClipboard(string input)
{#ifUNITY_EDITOR TextEditor t =new TextEditor();
t.text = input;
t.OnFocus();
t.Copy();#elifUNITY_IPHONE CopyTextToClipboard_iOS(input); #elifUNITY_ANDROID AndroidJavaClass unityPlayer =newAndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic("currentActivity");
AndroidJavaClass tool =newAndroidJavaClass("com.my.ugcf.Tool");
tool.CallStatic("CopyTextToClipboard", currentActivity, input);#endif }
java:
publicstaticvoidCopyTextToClipboard(finalContext activity,finalString 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:
voidCopyTextToClipboard_iOS(constchar*text)
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string= _CreateNSString(text);
}
网友评论