Unity gets mobile clipboard and write clipboard operations

        Because unity is developed on the PC side, it cannot directly call some libraries of the ios or android system. Generally, when the unity application needs to obtain or write to the clipboard of the mobile phone, it is basically done through the Xcode or andriod studio compile the corresponding method library, and then import it into the ios or android folder corresponding to the plugins folder of the unity application project and then reference it.

        Take unity to obtain the clipboard information of the mobile phone as an example. Although the codes of ios and android are different, the principle is the same-both obtain the permissions and information of the corresponding clipboard from the bottom through the native code, and read it The operation of fetching or writing.

android

For Android, you need to create an empty application in android studio, create a clipboard tool class, and finally generate a jar package. There are many tutorials for generating jar packages on the Internet. If you don’t know the jar package output by as, you can Baidu it yourself.

The Android tool code is attached below

package com.example.getclipboard;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;

public class ClipboardTools {

    public   static ClipboardManager clipboard =  null ;

    // 向剪贴板中添加文本
    public void copyTextToClipboard(final Context activity, final String str) throws Exception  {
        clipboard = (ClipboardManager) activity.getSystemService(Activity.CLIPBOARD_SERVICE);
        ClipData textCd = ClipData.newPlainText( "data" , str);
        clipboard.setPrimaryClip(textCd);
    }

    // 从剪贴板中获取文本
    public String getTextFromClipboard()  {
    clipboard = (ClipboardManager) activity.getSystemService(Activity.CLIPBOARD_SERVICE);
        if  (clipboard.hasPrimaryClip()
                && clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
            ClipData cdText = clipboard.getPrimaryClip();
            ClipData.Item item = cdText.getItemAt(0);
            return  item.getText().toString();
        }
        return   "" ;
    }
}

If you open the generated jar with decompression software, you can see that the corresponding file level format will correspond to the package structure, which is basically the format of com.xx.xxx.xxxx. This format should correspond to the characters of the jar class we obtained in Unity String correspondence will be explained in detail below.

Put the generated jar package into the andriod folder of plugins in the root directory of the unity project, if not, you can create it yourself.

In unity, the classes in the generated jar package are obtained through the AndroidJavaObject class. The code is as follows

                #if UNITY_ANDROID
                AndroidJavaObject androidObject = new AndroidJavaObject("com.zengqi.clipboardapp.ClipboardTools");
                AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
                if (activity != null)
                {
                    //获取剪切板
                    str = androidObject.Call<String>("getTextFromClipboard",activity);
                    //写入剪切板
                    androidObject.Call("copyTextToClipboard", activity, str);
                    
                }
                #endif

The string of com.zengqi.clipboardapp.ClipboardTools in the code above, where com.zengqi.clipboardapp refers to the package name, and ClipboardTools refers to the tool class of the clipboard. The activity in the next line is the activty instantiated by unity in Android, and Android and unity exchange data through this activity.

 

IOS end

The ios side is a bit more convenient than Android. You don’t need to operate on xcode, just add the corresponding class and header files to the ios folder under the plugins folder.

First, create the corresponding .h file and .mm file under the plugins/ios/ folder. This can be directly written in a text file, and the last one can be modified to the corresponding .h and .mm files.

.h file

#import <Foundation/Foundation.h>
@ interface Clipboard : NSObject
extern "C"
{
void _copyTextToClipboard(const char *textList);
const char* _GetClipboardText();
}
@end

.mm file

#import "Clipboard.h"
@implementation Clipboard
- (void)objc_copyTextToClipboard : (NSString*)text
{
     UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
     pasteboard.string = text; 
}

  - (NSString*)GetClipboardText
  {
      UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
      return pasteboard.string;
  }

@end

extern "C" {
     static Clipboard *iosClipboard;
   
    char* _MakeStringCopy(const char* str)
    {
        if(str==NULL){ return NULL;}
        char* res=(char*)malloc(strlen(str)+1);
        strcpy(res,str);
        return res;
    }
    
     void _copyTextToClipboard(const char *textList) 
    {   
        NSString *text = [NSString stringWithUTF8String: textList] ;        
        if(iosClipboard == NULL)
        {
            iosClipboard = [[Clipboard alloc] init];
        }     
        [iosClipboard objc_copyTextToClipboard: text]; 
    }

const char* _GetClipboardText()
{
    if(iosClipboard == NULL)
        {
            iosClipboard = [[Clipboard alloc] init];
        }  
       return _MakeStringCopy([[iosClipboard GetClipboardText] UTF8String]);
}    
}

After the above two classes are created, they can be referenced in the c# class that needs to be called.

#if UNITY_IPHONE
   
        [DllImport("__Internal")]
        private static extern void _copyTextToClipboard(string text);

        [DllImport("__Internal")]
        private static extern string _GetClipboardText();
#endif

After defining the above reference, you can directly reference the clipboard method

#if UNITY_IPHONE
		    //获取剪切板的内容
            _GetClipboardText();
            //将字符串写入剪切板
		    _copyTextToClipboard (input);
	        
#endif

 

Guess you like

Origin blog.csdn.net/ssssssilver/article/details/109730514