Cute_demon_attacks/meng_yao/Assets/Config/Android/Clipboard.cs

41 lines
1.5 KiB
C#
Raw Normal View History

2024-12-23 14:26:13 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2024-12-23 17:53:25 +08:00
public class Clipboard
2024-12-23 14:26:13 +08:00
{
// <20><><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public static void CopyToClipboard(string text)
{
if (Application.platform == RuntimePlatform.Android)
{
2024-12-23 17:53:25 +08:00
Debug.Log("<22><><EFBFBD><EFBFBD><EBB8B4>ճ<EFBFBD><D5B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
2024-12-23 14:26:13 +08:00
// <20><>ȡ Android <20><> ClipboardManager <20><>
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0> Android Activity
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
// <20><>ȡ Android <20><> ClipboardManager
using (AndroidJavaObject clipboardManager = currentActivity.Call<AndroidJavaObject>("getSystemService", "clipboard"))
{
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>µ<EFBFBD> ClipData <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ClipData <20><> Android <20>еļ<D0B5><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
using (AndroidJavaClass clipDataClass = new AndroidJavaClass("android.content.ClipData"))
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>ClipData<74><61><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>Ƶ<EFBFBD><C6B5>ı<EFBFBD>
AndroidJavaObject clipData = clipDataClass.CallStatic<AndroidJavaObject>("newPlainText", "text", text);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
clipboardManager.Call("setPrimaryClip", clipData);
}
}
}
}
else
{
Debug.Log("This feature is only available on Android.");
}
}
}