如何在WebGL构建中将Unity复制的文本粘贴到Unity中的输入字段中

3

我尝试将Unity外部复制的文本粘贴到WebGL构建中的InputField中,但文本未被粘贴到输入字段中。 有什么方法可以实现这一点?请提出任何想法。 谢谢提前。

例如,在Windows上打开记事本或Mac上的TextEdit,选择一些文本,复制,然后在运行在浏览器中的Unity WebGL应用程序中的InputField中单击。 粘贴(ctrl-v/cmd-v),但没有任何反应。


你的问题不够清晰。你想从哪里复制文本到哪里呢? - Programmer
谢谢回复,我已经从文档中复制了文本,并尝试将复制的文本粘贴到Unity Web GL构建中的输入字段中,但是文本没有被复制。 - Deepak
复制和粘贴代码在哪里? - Programmer
我刚刚使用 Ctrl + C 键从文档中复制了文本,现在正在尝试使用 Ctrl + V 键将复制的文本粘贴到输入字段中。 - Deepak
4个回答

3
我使用了这个解决方案,它似乎有效。
基本上,它钩入了浏览器的剪切、复制和粘贴事件。在粘贴时,它获取浏览器的剪贴板并将其传递到 Unity 中以附加到当前的InputField。在剪切/复制时,它调用 Unity 来告诉当前的InputField执行剪切/复制操作,然后获取 Unity 的剪贴板并将其放入浏览器的剪贴板中。
我在 Firefox 77 Windows、Chrome 83 Windows 和 Chrome 83 Mac、Firefox 77 Mac 和 Safari 13.1 Mac 上进行了测试。
此外,当我发布这篇文章时,我发现这个解决方案可能更好?https://github.com/kou-yeung/WebGLInput

嗨,我尝试了你的第一个解决方案。复制、剪切和粘贴只在浏览器内部工作。然而,我需要从Notepad++或Excel中复制文本并粘贴到WebGL应用程序中,目前还无法实现。 - Yerbol
哦,我应该删除面板才能使它工作。在场景中,我删除了面板并添加了游戏对象。现在它可以工作了!非常感谢! - Yerbol
第二个解决方案对我非常有效。https://github.com/kou-yeung/WebGLInput - Cohensius

0

目前,使用Unity的WebGL时,您无法复制和粘贴。

这是由于浏览器安全限制造成的。尽管Unity一直在努力工作通过使用键盘快捷键来执行复制和粘贴操作来开发WebGL的复制和粘贴机制,现在已经在某些Web浏览器上得到支持,但尚未发布。它还没有完成,所以您必须等待。


感谢您的回复。您能否提供任何实现此类功能的替代方案? - Deepak

0

0
I was facing the same issue while developing paste functionality into my Unity project.
The requirement was user can paste the copied data into the InputFiled.

官方文件链接:https://docs.unity3d.com/2017.3/Documentation/Manual/webgl-interactingwithbrowserscripting.html
I follow these steps: -
 1. Create a javascript plugin for the same with the .jslib file extension.
 2. Paste this code into the created file.

    mergeInto(LibraryManager.library, {
        CopyPasteReader: function(gObj, vName) {
                var gameObjectName = UTF8ToString(gObj);
                var voidName = UTF8ToString(vName);
                console.log(gameObjectName, "gameObjectName");
                console.log(voidName, "voidName");
                navigator.clipboard.readText().then(function(data) {
                    SendMessage(gameObjectName,"Paste", data);
                }, function() {
                    SendMessage(gameObjectName, "Paste", "no text available in clipboard");
                })
            }
            });

 3. Import this plugin into the script top.

    [DllImport("__Internal")]
    public static extern void CopyPasteReader(string gObj, string vName);

 2. Use the Update function so it will call automatically after the frame change.

    void Update()
        {
            if (Input.GetKeyDown(KeyCode.V) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)))
            {
                CopyPasteReader("KYCDigilockerurlPanel","Paste");
            }
        }
    // here KYCDigilockerurlPanel is a top parent of inputfield and Pase is a function


 4. This function will check if the ctrl+v action has performed the run the "CopyPasteReader("KYCDigilockerurlPanel", "Paste");" function. 

 5. Crate a Paste function in your script file

    private void Paste(string pasteValue)
    {
        Debug.Log(pasteValue + " pasteValue");
        enterDigiLockerCode.text = pasteValue;
    }
    // here enterDigiLockerCode is an input field name

6. Attach the script on the panel "KYCDigilockerurlPanel"  and create a GameObject of that Input field where you want to paste.  
 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接