40 lines
1.5 KiB
C#
40 lines
1.5 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
public class Clipboard : MonoBehaviour
|
|||
|
{
|
|||
|
|
|||
|
// <20><><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
public static void CopyToClipboard(string text)
|
|||
|
{
|
|||
|
if (Application.platform == RuntimePlatform.Android)
|
|||
|
{
|
|||
|
// <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.");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|