最近项目中需要用到Unity调用移动端的剪切板,网上搜了一下发现有很多。但是最后用的时候发现IOS只有将文本复制到剪切板,而没有将剪切板中的内容读取的功能。所以在这里自己手撸的一段。点击这里有惊喜。
1 Android 安卓的就不用多说了(网上也有很多)直接上图片 最后会贴出Unity调用的代码。
IOS 上比较麻烦,因为Unity是无法调用OC代码的需要创建一个.mm和.h文件
//再创建一个clipboard.mm文件 copy以下内容 。完成后将这两个文件拷贝到Unity/Assets/Plugin/IOS文//件夹下面
#import "Clipboard.h"
@implementation Clipboard
- (void)objc_copyTextToClipboard : (NSString*)text
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = text;
}
+(NSString *)objc_getTextFromClipboard
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString* pNsStr = pasteboard.string;
if(pNsStr == NULL)
{
pNsStr = @"";
}
return pNsStr;
}
@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];
}
char* _getTextFromClipboard()
{
return MakeStringCopy([[Clipboard objc_getTextFromClipboard] UTF8String]);
}
char* MakeStringCopy (const char* string)
{
if (string == NULL)
return NULL;
char* res = (char*)malloc(strlen(string) + 1);
strcpy(res, string);
return res;
}
}
//再xcode中创建一个clipboard.h文件,copy以下内容。
#import <Foundation/Foundation.h>
@interface Clipboard : NSObject
extern "C"
{
/* compare the namelist with system processes */
void _copyTextToClipboard(const char *textList);
char* _getTextFromClipboard();
char* MakeStringCopy (const char* string);
}
@end
这就IOS中需要用到的配置。最后是Unity调用的代码。
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class ClipboardManager : MonoBehaviour
{
#if UNITY_IPHONE
/* Interface to native implementation */
[DllImport ("__Internal")]
private static extern void _copyTextToClipboard(string text);
[DllImport ("__Internal")]
private static extern string _getTextFromClipboard();
[DllImport ("__Internal")]
private static extern string MakeStringCopy(string text);
#endif
public static void CopyToClipboard(string input)
{
#if UNITY_ANDROID
//AndroidJavaObject androidObject = new AndroidJavaObject("com.shengwei.qipai.ClipboardTools");
//AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass javaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject javaObject = javaClass.GetStatic<AndroidJavaObject>("currentActivity");
if (javaClass == null)
{
HotFix_Bind.DBG.Log("activity为null");
return;
}
// 复制到剪贴板
javaObject.Call("copyTextToClipboard", javaObject, input);
// 从剪贴板中获取文本
#elif UNITY_IPHONE
_copyTextToClipboard(input); //IOS
#endif
}
public static string GetTextFromClipboard()
{
#if UNITY_ANDROID
AndroidJavaClass javaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject javaObject = javaClass.GetStatic<AndroidJavaObject>("currentActivity");
string text = javaObject.Call<string>("getTextFromClipboard", javaObject, "");
//button.gameObject.transform.GetChild(0).GetComponent<Text>().text = text;
return text;
#elif UNITY_IPHONE
return _getTextFromClipboard();
#else
return "";
#endif
}
如果有问题,可以在下面留言。