版权声明:本文为原创文章,转载请声明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); }