using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace TriLibCore.Samples
{
/// Represents a Class used to add paste capabilities to WebGL projects.
public class PasteManager : MonoBehaviour
{
#if UNITY_WEBGL && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern void PasteManagerSetup();
#endif
/// The Paste Manager Singleton instance.
public static PasteManager Instance { get; private set; }
/// Checks if the Singleton instance exists.
public static void CheckInstance()
{
if (Instance == null)
{
Instance = new GameObject("PasteManager").AddComponent();
}
}
#if UNITY_WEBGL && !UNITY_EDITOR
private void Start()
{
PasteManagerSetup();
}
#endif
/// Called when the user pastes the given value in the Web-Browser.
/// The pasted value.
public void Paste(string value)
{
var currentCurrentSelectedGameObject = EventSystem.current.currentSelectedGameObject;
if (currentCurrentSelectedGameObject != null)
{
var inputField = currentCurrentSelectedGameObject.GetComponentInChildren();
if (inputField != null)
{
var newText = $"{inputField.text.Substring(0, inputField.selectionAnchorPosition)}{value}{inputField.text.Substring(inputField.selectionFocusPosition)}";
inputField.text = newText;
}
}
}
}
}