unity项目中,需要将文本内容复制到系统剪切板(包含android,ios,unityeditor三部分)

unity中复制文本到剪切板,分为android,IOS和编辑器中三部分.自己实现了下这三部分的做法.代码很少就一个脚本.

using UnityEngine;
using UnityEngine.UI;
using System.Runtime.InteropServices;
using System.Collections;

public class Test : MonoBehaviour
{
    public InputField input;

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

        public void OnClickCopyText()
        {
#if UNITY_ANDROID
        AndroidJavaObject androidObject = new AndroidJavaObject("com.androidclicp.ClipboardTools");
        AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity");
        if (activity == null)
            return;
        // 复制到剪贴板
        androidObject.Call("copyTextToClipboard", activity, input.text);
        
        // 从剪贴板中获取文本
        string text = androidObject.Call("getTextFromClipboard");
#elif UNITY_IOS
        _copyTextToClipboard(input.text);
#elif UNITY_EDITOR
        TextEditor te = new TextEditor();
        te.content = new GUIContent(input.text);
        te.SelectAll();
        te.Copy();
#endif
        }
     
}

demo地址:https://download.csdn.net/download/u011976408/10364252

你可能感兴趣的:(unity,C#)