原博:https://blog.csdn.net/YE_wolf/article/details/76831446
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
"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
"currentActivity"
);
// 复制到剪贴板
androidObject.CallStatic(
"copyTextToClipboard"
, activity, copyText);
#elif UNITY_IPHONE
_copyTextToClipboard(copyText);
#endif
}
|