WebGLCopyAndPaste.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2020, Gregg Tavares.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Gregg Tavares. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. using UnityEngine;
  32. using UnityEngine.EventSystems;
  33. using System.Runtime.InteropServices;
  34. // #define WEBGL_COPY_AND_PASTE_SUPPORT_TEXTMESH_PRO
  35. public class WebGLCopyAndPasteAPI {
  36. #if UNITY_WEBGL
  37. [DllImport("__Internal")]
  38. private static extern void initWebGLCopyAndPaste(string objectName, string cutCopyCallbackFuncName, string pasteCallbackFuncName);
  39. [DllImport("__Internal")]
  40. private static extern void passCopyToBrowser(string str);
  41. #endif
  42. static public void Init(string objectName, string cutCopyCallbackFuncName, string pasteCallbackFuncName)
  43. {
  44. #if UNITY_WEBGL
  45. initWebGLCopyAndPaste(objectName, cutCopyCallbackFuncName, pasteCallbackFuncName);
  46. #endif
  47. }
  48. static public void PassCopyToBrowser(string str)
  49. {
  50. #if UNITY_WEBGL
  51. passCopyToBrowser(str);
  52. #endif
  53. }
  54. }
  55. public class WebGLCopyAndPaste : MonoBehaviour {
  56. void Start()
  57. {
  58. if (!Application.isEditor) {
  59. WebGLCopyAndPasteAPI.Init(this.name, "GetClipboard", "ReceivePaste");
  60. }
  61. }
  62. private void SendKey(string baseKey)
  63. {
  64. string appleKey = "%" + baseKey;
  65. string naturalKey = "^" + baseKey;
  66. var currentObj = EventSystem.current.currentSelectedGameObject;
  67. if (currentObj == null) {
  68. return;
  69. }
  70. {
  71. var input = currentObj.GetComponent<UnityEngine.UI.InputField>();
  72. if (input != null) {
  73. // I don't know what's going on here. The code in InputField
  74. // is looking for ctrl-c but that fails on Mac Chrome/Firefox
  75. input.ProcessEvent(Event.KeyboardEvent(naturalKey));
  76. input.ProcessEvent(Event.KeyboardEvent(appleKey));
  77. // so let's hope one of these is basically a noop
  78. return;
  79. }
  80. }
  81. #if WEBGL_COPY_AND_PASTE_SUPPORT_TEXTMESH_PRO
  82. {
  83. var input = currentObj.GetComponent<TMPro.TMP_InputField>();
  84. if (input != null) {
  85. // I don't know what's going on here. The code in InputField
  86. // is looking for ctrl-c but that fails on Mac Chrome/Firefox
  87. // so let's hope one of these is basically a noop
  88. input.ProcessEvent(Event.KeyboardEvent(naturalKey));
  89. input.ProcessEvent(Event.KeyboardEvent(appleKey));
  90. return;
  91. }
  92. }
  93. #endif
  94. }
  95. public void GetClipboard(string key)
  96. {
  97. SendKey(key);
  98. WebGLCopyAndPasteAPI.PassCopyToBrowser(GUIUtility.systemCopyBuffer);
  99. }
  100. public void ReceivePaste(string str)
  101. {
  102. GUIUtility.systemCopyBuffer = str;
  103. }
  104. }