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

Commit 097bb53

Browse files
authored
Merge pull request #3 from prostolyubo/basic-improvements
fixed jslib to use callbacks, cleaned up
2 parents d9e91fd + 34a55bb commit 097bb53

File tree

4 files changed

+140
-176
lines changed

4 files changed

+140
-176
lines changed

Assets/WebGLCopyAndPaste/Plugins/WebGLCopyAndPaste.jslib

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,69 +28,81 @@
2828
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2929
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
31-
mergeInto(LibraryManager.library, {
32-
initWebGLCopyAndPaste__postset: '_initWebGLCopyAndPaste();',
33-
initWebGLCopyAndPaste: function() {
34-
// for some reason only on Safari does Unity call
35-
// preventDefault so let's prevent preventDefault
36-
// so the browser will generate copy and paste events
37-
window.addEventListener = function(origFn) {
38-
function noop() {}
31+
var WebGLCopyAndPaste = {
32+
$WebGLCopyAndPaste: {},
3933

40-
// I hope c,x,v are universal
41-
const keys = {'c': true, 'x': true, 'v': true};
34+
initWebGLCopyAndPaste__postset: '_initWebGLCopyAndPaste();',
4235

43-
// Emscripten doesn't support the spread operator or at
44-
// least the one used by Unity 2019.4.1
45-
return function(name, fn) {
46-
const args = Array.prototype.slice.call(arguments);
47-
if (name !== 'keypress') {
48-
return origFn.apply(this, args);
49-
}
50-
args[1] = function(event) {
51-
const hArgs = Array.prototype.slice.call(arguments);
52-
if (keys[event.key.toLowerCase()] &&
53-
((event.metaKey ? 1 : 0) + (event.ctrlKey ? 1 : 0)) === 1) {
54-
event.preventDefault = noop;
55-
}
56-
return fn.apply(this, hArgs);
57-
};
58-
return origFn.apply(this, args);
59-
};
60-
}(window.addEventListener);
61-
_initWebGLCopyAndPaste = function(objectNamePtr, cutCopyFuncNamePtr, pasteFuncNamePtr) {
62-
window.becauseUnityIsBadWithJavascript_webglCopyAndPaste =
63-
window.becauseUnityIsBadWithJavascript_webglCopyAndPaste || {
64-
initialized: false,
65-
objectName: Pointer_stringify(objectNamePtr),
66-
cutCopyFuncName: Pointer_stringify(cutCopyFuncNamePtr),
67-
pasteFuncName: Pointer_stringify(pasteFuncNamePtr),
68-
};
69-
const g = window.becauseUnityIsBadWithJavascript_webglCopyAndPaste;
36+
initWebGLCopyAndPaste: function () {
37+
// for some reason only on Safari does Unity call
38+
// preventDefault so let's prevent preventDefault
39+
// so the browser will generate copy and paste events
40+
window.addEventListener = function (origFn) {
41+
function noop() {
42+
}
43+
44+
// I hope c,x,v are universal
45+
const keys = {'c': true, 'x': true, 'v': true};
7046

71-
if (!g.initialized) {
72-
window.addEventListener('cut', function(e) {
73-
e.preventDefault();
74-
SendMessage(g.objectName, g.cutCopyFuncName, 'x');
75-
event.clipboardData.setData('text/plain', g.clipboardStr);
76-
});
77-
window.addEventListener('copy', function(e) {
78-
e.preventDefault();
79-
SendMessage(g.objectName, g.cutCopyFuncName, 'c');
80-
event.clipboardData.setData('text/plain', g.clipboardStr);
81-
});
82-
window.addEventListener('paste', function(e) {
83-
const str = e.clipboardData.getData('text');
84-
SendMessage(g.objectName, g.pasteFuncName, str);
85-
});
47+
// Emscripten doesn't support the spread operator or at
48+
// least the one used by Unity 2019.4.1
49+
return function (name, fn) {
50+
const args = Array.prototype.slice.call(arguments);
51+
if (name !== 'keypress') {
52+
return origFn.apply(this, args);
8653
}
54+
args[1] = function (event) {
55+
const hArgs = Array.prototype.slice.call(arguments);
56+
if (keys[event.key.toLowerCase()] &&
57+
((event.metaKey ? 1 : 0) + (event.ctrlKey ? 1 : 0)) === 1) {
58+
event.preventDefault = noop;
59+
}
60+
return fn.apply(this, hArgs);
61+
};
62+
return origFn.apply(this, args);
8763
};
88-
},
89-
passCopyToBrowser: function(stringPtr) {
90-
const g = window.becauseUnityIsBadWithJavascript_webglCopyAndPaste;
91-
const str = Pointer_stringify(stringPtr);
92-
g.clipboardStr = str;
93-
},
94-
});
64+
}(window.addEventListener);
65+
66+
_initWebGLCopyAndPaste = function (cutCopyFuncPtr, pasteFuncPtr) {
67+
68+
function sendStringCallback (callback, str) {
69+
var bufferSize = lengthBytesUTF8(str) + 1;
70+
var buffer = _malloc(bufferSize);
71+
stringToUTF8(str, buffer, bufferSize);
72+
Runtime.dynCall('vi', callback, [buffer]);
73+
}
74+
75+
WebGLCopyAndPaste.data =
76+
WebGLCopyAndPaste.data || {
77+
initialized: false,
78+
cutCopyFunc: cutCopyFuncPtr,
79+
pasteFunc: pasteFuncPtr,
80+
};
81+
const g = WebGLCopyAndPaste.data;
82+
83+
if (!g.initialized) {
84+
window.addEventListener('cut', function (e) {
85+
e.preventDefault();
86+
sendStringCallback(g.cutCopyFunc, 'x');
87+
event.clipboardData.setData('text/plain', g.clipboardStr);
88+
});
89+
window.addEventListener('copy', function (e) {
90+
e.preventDefault();
91+
sendStringCallback(g.cutCopyFunc, 'c');
92+
event.clipboardData.setData('text/plain', g.clipboardStr);
93+
});
94+
window.addEventListener('paste', function (e) {
95+
const str = e.clipboardData.getData('text');
96+
sendStringCallback(g.pasteFunc, str);
97+
});
98+
}
99+
};
100+
},
95101

102+
passCopyToBrowser: function (stringPtr) {
103+
WebGLCopyAndPaste.data.clipboardStr = Pointer_stringify(stringPtr);
104+
},
105+
};
96106

107+
autoAddDeps(WebGLCopyAndPaste, '$WebGLCopyAndPaste');
108+
mergeInto(LibraryManager.library, WebGLCopyAndPaste);

Assets/WebGLCopyAndPaste/Scenes/WebGLCopyAnPasteSampleScene.unity

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,49 +1166,6 @@ CanvasRenderer:
11661166
m_PrefabAsset: {fileID: 0}
11671167
m_GameObject: {fileID: 972896129}
11681168
m_CullTransparentMesh: 0
1169-
--- !u!1 &1267957137
1170-
GameObject:
1171-
m_ObjectHideFlags: 0
1172-
m_CorrespondingSourceObject: {fileID: 0}
1173-
m_PrefabInstance: {fileID: 0}
1174-
m_PrefabAsset: {fileID: 0}
1175-
serializedVersion: 6
1176-
m_Component:
1177-
- component: {fileID: 1267957138}
1178-
- component: {fileID: 1267957139}
1179-
m_Layer: 0
1180-
m_Name: WebGLCopyAndPasteHelper
1181-
m_TagString: Untagged
1182-
m_Icon: {fileID: 0}
1183-
m_NavMeshLayer: 0
1184-
m_StaticEditorFlags: 0
1185-
m_IsActive: 1
1186-
--- !u!4 &1267957138
1187-
Transform:
1188-
m_ObjectHideFlags: 0
1189-
m_CorrespondingSourceObject: {fileID: 0}
1190-
m_PrefabInstance: {fileID: 0}
1191-
m_PrefabAsset: {fileID: 0}
1192-
m_GameObject: {fileID: 1267957137}
1193-
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
1194-
m_LocalPosition: {x: 0, y: 0, z: 0}
1195-
m_LocalScale: {x: 1, y: 1, z: 1}
1196-
m_Children: []
1197-
m_Father: {fileID: 0}
1198-
m_RootOrder: 3
1199-
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
1200-
--- !u!114 &1267957139
1201-
MonoBehaviour:
1202-
m_ObjectHideFlags: 0
1203-
m_CorrespondingSourceObject: {fileID: 0}
1204-
m_PrefabInstance: {fileID: 0}
1205-
m_PrefabAsset: {fileID: 0}
1206-
m_GameObject: {fileID: 1267957137}
1207-
m_Enabled: 1
1208-
m_EditorHideFlags: 0
1209-
m_Script: {fileID: 11500000, guid: 9e56c2066eff9f34c812865afcc473c9, type: 3}
1210-
m_Name:
1211-
m_EditorClassIdentifier:
12121169
--- !u!1 &1404080528
12131170
GameObject:
12141171
m_ObjectHideFlags: 0

Assets/WebGLCopyAndPaste/Scripts/WebGLCopyAndPaste.cs

Lines changed: 60 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -34,87 +34,76 @@
3434

3535
// #define WEBGL_COPY_AND_PASTE_SUPPORT_TEXTMESH_PRO
3636

37-
public class WebGLCopyAndPasteAPI {
37+
public class WebGLCopyAndPasteAPI
38+
{
3839

39-
#if UNITY_WEBGL
40+
#if UNITY_WEBGL
4041

41-
[DllImport("__Internal")]
42-
private static extern void initWebGLCopyAndPaste(string objectName, string cutCopyCallbackFuncName, string pasteCallbackFuncName);
43-
[DllImport("__Internal")]
44-
private static extern void passCopyToBrowser(string str);
42+
[DllImport("__Internal")]
43+
private static extern void initWebGLCopyAndPaste(StringCallback cutCopyCallback, StringCallback pasteCallback);
44+
[DllImport("__Internal")]
45+
private static extern void passCopyToBrowser(string str);
4546

46-
#endif
47+
delegate void StringCallback( string content );
4748

48-
static public void Init(string objectName, string cutCopyCallbackFuncName, string pasteCallbackFuncName)
49-
{
50-
#if UNITY_WEBGL
51-
52-
initWebGLCopyAndPaste(objectName, cutCopyCallbackFuncName, pasteCallbackFuncName);
5349

54-
#endif
55-
}
56-
57-
static public void PassCopyToBrowser(string str)
50+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
51+
private static void Init()
5852
{
59-
#if UNITY_WEBGL
60-
61-
passCopyToBrowser(str);
62-
63-
#endif
64-
}
65-
}
66-
67-
public class WebGLCopyAndPaste : MonoBehaviour {
68-
void Start()
69-
{
70-
if (!Application.isEditor) {
71-
WebGLCopyAndPasteAPI.Init(this.name, "GetClipboard", "ReceivePaste");
53+
if ( !Application.isEditor )
54+
{
55+
initWebGLCopyAndPaste(GetClipboard, ReceivePaste );
56+
}
7257
}
73-
}
7458

75-
private void SendKey(string baseKey)
76-
{
77-
string appleKey = "%" + baseKey;
78-
string naturalKey = "^" + baseKey;
59+
private static void SendKey(string baseKey)
60+
{
61+
string appleKey = "%" + baseKey;
62+
string naturalKey = "^" + baseKey;
63+
64+
var currentObj = EventSystem.current.currentSelectedGameObject;
65+
if (currentObj == null) {
66+
return;
67+
}
68+
{
69+
var input = currentObj.GetComponent<UnityEngine.UI.InputField>();
70+
if (input != null) {
71+
// I don't know what's going on here. The code in InputField
72+
// is looking for ctrl-c but that fails on Mac Chrome/Firefox
73+
input.ProcessEvent(Event.KeyboardEvent(naturalKey));
74+
input.ProcessEvent(Event.KeyboardEvent(appleKey));
75+
// so let's hope one of these is basically a noop
76+
return;
77+
}
78+
}
79+
#if WEBGL_COPY_AND_PASTE_SUPPORT_TEXTMESH_PRO
80+
{
81+
var input = currentObj.GetComponent<TMPro.TMP_InputField>();
82+
if (input != null) {
83+
// I don't know what's going on here. The code in InputField
84+
// is looking for ctrl-c but that fails on Mac Chrome/Firefox
85+
// so let's hope one of these is basically a noop
86+
input.ProcessEvent(Event.KeyboardEvent(naturalKey));
87+
input.ProcessEvent(Event.KeyboardEvent(appleKey));
88+
return;
89+
}
90+
}
91+
#endif
92+
}
7993

80-
var currentObj = EventSystem.current.currentSelectedGameObject;
81-
if (currentObj == null) {
82-
return;
83-
}
84-
{
85-
var input = currentObj.GetComponent<UnityEngine.UI.InputField>();
86-
if (input != null) {
87-
// I don't know what's going on here. The code in InputField
88-
// is looking for ctrl-c but that fails on Mac Chrome/Firefox
89-
input.ProcessEvent(Event.KeyboardEvent(naturalKey));
90-
input.ProcessEvent(Event.KeyboardEvent(appleKey));
91-
// so let's hope one of these is basically a noop
92-
return;
94+
[AOT.MonoPInvokeCallback( typeof(StringCallback) )]
95+
private static void GetClipboard(string key)
96+
{
97+
SendKey(key);
98+
passCopyToBrowser(GUIUtility.systemCopyBuffer);
9399
}
94-
}
95-
#if WEBGL_COPY_AND_PASTE_SUPPORT_TEXTMESH_PRO
96-
{
97-
var input = currentObj.GetComponent<TMPro.TMP_InputField>();
98-
if (input != null) {
99-
// I don't know what's going on here. The code in InputField
100-
// is looking for ctrl-c but that fails on Mac Chrome/Firefox
101-
// so let's hope one of these is basically a noop
102-
input.ProcessEvent(Event.KeyboardEvent(naturalKey));
103-
input.ProcessEvent(Event.KeyboardEvent(appleKey));
104-
return;
100+
101+
[AOT.MonoPInvokeCallback( typeof(StringCallback) )]
102+
private static void ReceivePaste(string str)
103+
{
104+
GUIUtility.systemCopyBuffer = str;
105105
}
106-
}
107-
#endif
108-
}
109106

110-
public void GetClipboard(string key)
111-
{
112-
SendKey(key);
113-
WebGLCopyAndPasteAPI.PassCopyToBrowser(GUIUtility.systemCopyBuffer);
114-
}
107+
#endif
115108

116-
public void ReceivePaste(string str)
117-
{
118-
GUIUtility.systemCopyBuffer = str;
119-
}
120-
}
109+
}

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ Example: https://greggman.github.io/unity-webgl-copy-and-paste/
1414

1515
1. Download and add in [this unity package](https://github.com/greggman/unity-webgl-copy-and-paste/releases/latest) into your project.
1616

17-
2. Make a `GameObject` and add in the `WebGLCopyAndPaste` script.
18-
19-
3. If you are using [`TMPro.TMP_InputField`](https://docs.unity3d.com/Packages/com.unity.textmeshpro@2.1/api/TMPro.TMP_InputField.html) then edit `Assets/WebGLCopyAndPaste/Scripts/WebGLCopyAndPaste.cs`
17+
2. If you are using [`TMPro.TMP_InputField`](https://docs.unity3d.com/Packages/com.unity.textmeshpro@2.1/api/TMPro.TMP_InputField.html) then edit `Assets/WebGLCopyAndPaste/Scripts/WebGLCopyAndPaste.cs`
2018
and uncomment this line
2119

2220
```
@@ -48,6 +46,14 @@ https://github.com/kou-yeung/WebGLInput
4846

4947
## ChangeList
5048

49+
* 0.1.0
50+
51+
* Removed the need for MonoBehaviours
52+
53+
* Replaced messages with proper callbacks
54+
55+
* Fixed data storing in `window`
56+
5157
* 0.0.2
5258

5359
* Support cut

0 commit comments

Comments
 (0)