Mecanim 发表于 2022-7-18 09:54

Unity Android&iOS 复制到粘贴板

Android
@TargetApi(11)@SuppressLint({"NewApi"})// 保存至粘贴板public static void copyTextToClipboard(final Context activity, final String str) throws Exception{    ClipboardManager clipboard =(ClipboardManager)context.getSystemService(context.CLIPBOARD_SRVICE);    ClipData textCd = ClipData.newPlainText("data" , str);    clipboard.setPrimaryClip(textCd);}@TargetApi(11)@SuppressLint({"NewApi"})// 获取粘贴板内容public static String GetClipboard(Activity context) {    ClipboardManager clipboard =(ClipboardManager)context.getSystemService(context.CLIPBOARD_SRVICE);    String result = "";    if (clipboard.hasPrimaryClip() &&clipboard.getPrimaryClipDescription().hasMimeType(ClipDescripton.MIMETYPE_TEXT_PLAIN)) {      ClipData cdText = clipboard.getPrimaryClip();      ClipData.Item item = cdText.getItemAt(0);      result = item.getText().toString();    }    return result;}

iOS
#define MakeStringCopy( _x_ ) ( _x_ != NULL && ] ) ? strdup( ) : NULLextern "C"{    // 保存至粘贴板    void _SaveToClipboard(char* text)    {      UIPasteboard* pasteboard = ;      pasteboard.string = ;    }      // 获取粘贴板内容    char* _GetClipboardText()    {      NSString* content = [ string];      return MakeStringCopy(content);    }}
页: [1]
查看完整版本: Unity Android&iOS 复制到粘贴板