PC、Android、IOS將內容複製到剪切板問題總結

1.PC平臺下

1
2
//Window剪切板處理
GUIUtility.systemCopyBuffer = copyText;


2.Android平臺下

先添加jar包,放置在Plugins->Android->bin目錄下

jar包中代碼(反編譯出來的):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.apowo.clipboard;
 
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Looper;
 
public class ClipboardTools
{
  public static ClipboardManager clipboard = null;
 
  public static void copyTextToClipboard(Context activity, String str) throws Exception
  {
    if (Looper.myLooper() == null) {
      Looper.prepare();
    }
    clipboard = (ClipboardManager)activity.getSystemService("clipboard");
 
    ClipData textCd = ClipData.newPlainText("data", str);
    clipboard.setPrimaryClip(textCd);
  }
}

引入jar包中的內容:

1
2
3
4
5
//Android剪切板處理
AndroidJavaObject androidObject = new AndroidJavaObject("com.apowo.clipboard.ClipboardTools");
AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
// 複製到剪貼板
androidObject.CallStatic("copyTextToClipboard", activity, copyText);


3.IOS平臺下

先添加.h和.mm文件,.mm文件是對.h文件中定義方法的具體實現

.h文件:

1
2
3
4
5
6
7
8
9
interface Clipboard : NSObject
 
extern "C"
{
                 /*  compare the namelist with system processes  */
                 void _copyTextToClipboard(const char *textList);
}
 
@end

.mm文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#import "Clipboard.h"
@implementation Clipboard
//將文本複製到IOS剪貼板
- (void)objc_copyTextToClipboard : (NSString*)text
{
     UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
     pasteboard.string = text;
}
@end
 
extern "C" {
     static Clipboard *iosClipboard;
 
     void _copyTextToClipboard(const char *textList)
    {  
        NSString *text = [NSString stringWithUTF8String: textList] ;
 
        if(iosClipboard == NULL)
        {
            iosClipboard = [[Clipboard alloc] init];
        }
 
        [iosClipboard objc_copyTextToClipboard: text];
    }
 
}

然後,在代碼裏引用:

引入頭DllImport:

1
using System.Runtime.InteropServices;

引入方法:

1
2
3
4
5
#if UNITY_IPHONE
            /* Interface to native implementation */
            [DllImport ("__Internal")]
            private static extern void _copyTextToClipboard(string text);
#endif

調用方法:

1
_copyTextToClipboard(copyText);


完整代碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    public void OnButtonClick() {
        string copyText = input.text.ToString();
 
#if UNITY_IPHONE
            /* Interface to native implementation */
            [DllImport ("__Internal")]
            private static extern void _copyTextToClipboard(string text);
#endif
 
#if UNITY_STANDALONE_WIN
        //Window剪切板處理
        GUIUtility.systemCopyBuffer = copyText;
#elif UNITY_ANDROID
        //Android剪切板處理
        AndroidJavaObject androidObject = new AndroidJavaObject("com.apowo.clipboard.ClipboardTools");
        AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
        // 複製到剪貼板
        androidObject.CallStatic("copyTextToClipboard", activity, copyText);
 
#elif UNITY_IPHONE
         _copyTextToClipboard(copyText);
 
#endif
    }

發佈了40 篇原創文章 · 獲贊 12 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章