This repository has been archived by the owner on Dec 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathWebData.cs
110 lines (89 loc) · 2.45 KB
/
WebData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using UnityEditor;
using UnityEngine;
// So I want to communicate two-way between WebView and Editor
// But how? Socket Port?
// Jeez. I'm crazy. Let's do it!
class WebData : EditorWindow
{
WebViewHook webView;
public string data;
WebSocketHook socket;
WebSocketHook.Item hook;
[MenuItem("Tools/Web Data Binding %#d")]
static void Load()
{
WebData window = GetWindow<WebData>();
window.Show();
}
void OnEnable()
{
if (!webView)
{
// Create webView
webView = CreateInstance<WebViewHook>();
}
// Make the server.
// The server is not serializable so there's
// no need to check if this exist or not.
socket = new WebSocketHook(9369, webView);
// Hook window.data to javascript
hook = socket.Add("data", () => data, (x) =>
{
data = x;
EditorApplication.delayCall += () => Repaint();
});
}
void OnDisable()
{
// IMPORTANT: Kill our server
socket.Dispose();
}
public void OnBecameInvisible()
{
if (webView)
{
// signal the browser to unhook
webView.Detach();
}
}
// HTML for demonstration
string Compose()
{
return @"<html><body>
<textarea wrap='soft' rows=20 id='input'></textarea>
<script>
var i = document.getElementById('input');
function update() { i.value = window.data; }
i.oninput = function() { window.data = i.value };
</script></body></html>";
}
void OnGUI()
{
// hook to this window
if (webView.Hook(this))
{
// do the first thing to do
webView.LoadHTML(Compose());
}
var half = new Rect(0, 0, position.width / 2, position.height);
// html text field
EditorGUI.BeginChangeCheck();
data = EditorGUI.TextArea(half, data);
if (EditorGUI.EndChangeCheck())
{
// push changes to webView and notify our input
hook.Update();
webView.ExecuteJavascript("update()");
}
half.x += half.width;
if (Event.current.type == EventType.Repaint)
{
// keep the browser aware with resize
webView.OnGUI(half);
}
else if (half.Contains(Event.current.mousePosition))
{
GUIUtility.keyboardControl = 0;
}
}
}