美文网首页
2020-03-14 Unity复制内容到剪贴板

2020-03-14 Unity复制内容到剪贴板

作者: VECTOR_Y | 来源:发表于2020-03-14 22:03 被阅读0次

unity探索者之复制内容到剪贴板

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

    }

相关文章

网友评论

      本文标题:2020-03-14 Unity复制内容到剪贴板

      本文链接:https://www.haomeiwen.com/subject/qsuoshtx.html