Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Several fixes and improvements #16

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Conversation

Trisibo
Copy link

@Trisibo Trisibo commented Dec 11, 2023

I made several fixes and improvements, I'm opening a single pull request with all of them, sorry if you preferred one pull request per change. These are the changes:

  • When I tested on Firefox on Windows, pasting didn't work at all. Apparently, setting GUIUtility.systemCopyBuffer to paste doesn't work there (not sure about other browsers, I don't even know why it works on some, seems a weird behaviour to me), so I added a SendKey("v") call, but then in some browsers it would paste the text twice. What seems to work on all browsers is to set GUIUtility.systemCopyBuffer, then call SendKey("v"), and then set GUIUtility.systemCopyBuffer to null, which prevents the second paste (it occurs on the next frame, so at that point it has no text). The only issue would be that other code cannot use GUIUtility.systemCopyBuffer at that point to know what was pasted, but I consider it a very minor issue compared to pasting not working at all in some browsers.

  • Added the forceLabelUpdate parameter to SendKey, used when pasting, since in some browsers the text was pasted but the label wasn't updated visually until another key was pressed. I used ForceLabelUpdate to do the update.

  • When creating a keyboard event for processing, instead of using the base key with added "%" and "^" texts and calling ProcessEvent twice, now the code creates an event with the key unchanged and the control and command properties set to true, which works fine on both Windows and Mac with just one call to ProcessEvent.

  • Substituted the deprecated Window.event property with the event passed to the JavaScript event handlers: https://developer.mozilla.org/en-US/docs/Web/API/Window/event

  • Moved the #define WEBGL_COPY_AND_PASTE_SUPPORT_TEXTMESH_PRO line before the usings, just for convenience so it can be uncommented right away without having to move it due to defines being invalid elsewhere in code.

  • When TextMesh Pro support is enabled, the code will check if the object has a TextMesh Pro field first, instead of first checking for a legacy field; most of the time, when TMP support is added it would be expected that objects use a TMP field, so checking that first would avoid the check for the legacy one in most cases.

  • Added a check to ensure EventSystem.current is not null before using it.

I have confirmed that it works on:

  • Edge 120.0.2210.61 (Chromium) on Windows 10, Unity 2022.3.10, 2021.3.25 and 2020.3.18.
  • Firefox 120.0.1 on Windows 10, Unity 2022.3.10, 2021.3.25 and 2020.3.18.
  • Safari 16.6 on macOS Ventura 13.6, Unity 2022.3.10.
  • Chrome 118.0.5993.70 on macOS Ventura 13.6, Unity 2022.3.10.
  • Firefox 120.0.1 on macOS Ventura 13.6, Unity 2022.3.10.

…LCopyAndPasteAPI.cs" before the "using" directives, so it can be uncommented without errors or having to move it manually.
…rol" and "command" properties set as true, which works on both mac and Windows with only one "ProcessEvent" call. Moved the event creation to a separate method.
…ld, instead of first checking for a legacy input field, to save a check in most situations.
@greggman
Copy link
Owner

Thanks for the PR ❤️

What do you think about you becoming the main repo for this? I'm not actually a Unity developer. I wrote this a just kind of a 1 day hobby thing. I'm not sure I've used Unity in over a year, maybe 2yrs

@Trisibo
Copy link
Author

Trisibo commented Dec 12, 2023

That would be OK. I've never done a repo transfer or similar, how do you want to do it?

@greggman
Copy link
Owner

I think I can just mark this repo as archived after I've changed the README to point to https://github.com/Trisibo/unity-webgl-copy-and-paste/

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants