41 lines
1.5 KiB
C#
41 lines
1.5 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
public class Clipboard
|
||
{
|
||
|
||
// 将文本复制到剪贴板
|
||
public static void CopyToClipboard(string text)
|
||
{
|
||
if (Application.platform == RuntimePlatform.Android)
|
||
{
|
||
Debug.Log("进入复制粘贴方法");
|
||
// 获取 Android 的 ClipboardManager 类
|
||
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
|
||
{
|
||
// 获取当前的 Android Activity
|
||
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
|
||
|
||
// 获取 Android 的 ClipboardManager
|
||
using (AndroidJavaObject clipboardManager = currentActivity.Call<AndroidJavaObject>("getSystemService", "clipboard"))
|
||
{
|
||
// 创建一个新的 ClipData 对象,ClipData 是 Android 中的剪贴板数据
|
||
using (AndroidJavaClass clipDataClass = new AndroidJavaClass("android.content.ClipData"))
|
||
{
|
||
// 创建剪贴板数据(ClipData),并添加要复制的文本
|
||
AndroidJavaObject clipData = clipDataClass.CallStatic<AndroidJavaObject>("newPlainText", "text", text);
|
||
|
||
// 将数据设置到剪贴板
|
||
clipboardManager.Call("setPrimaryClip", clipData);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("This feature is only available on Android.");
|
||
}
|
||
}
|
||
}
|