Cute_demon_attacks/meng_yao/Assets/Config/Android/Clipboard.cs
2024-12-23 17:53:25 +08:00

41 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.");
}
}
}