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 pathWebEditor.cs
128 lines (109 loc) · 3.53 KB
/
WebEditor.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using UnityEditor;
using UnityEngine;
public class WebEditor : EditorWindow
{
WebViewHook webView;
string head = @"<!-- Use this to make custom html placed on head -->";
string html = @"<h1>Hello World</h1>";
string css = "body {\n background-color: white;\n}";
string js = "/* Javascript Goes Here */";
int panel = 0;
class Styles
{
public static string template = "<html>\n<head>\n{3}\n<style>\n{1}\n</style>\n<script>\n{2}\n</script>\n</head>\n<body>\n{0}\n</body>\n</html>";
public static GUIContent[] heads = new GUIContent[] { new GUIContent("HTML"), new GUIContent("CSS"), new GUIContent("JS"), new GUIContent("Head") };
public static GUIStyle[] headStyles = new GUIStyle[] { EditorStyles.miniButtonLeft, EditorStyles.miniButtonMid, EditorStyles.miniButtonMid, EditorStyles.miniButtonRight };
public static GUIStyle textArea = new GUIStyle(EditorStyles.textArea) {
font = Font.CreateDynamicFontFromOSFont("Courier New", 12),
wordWrap = true
};
}
public string this[int idx]
{
get
{
switch (idx)
{
case 0: return html;
case 1: return css;
case 2: return js;
case 3: return head;
default: return "";
}
}
set
{
switch (idx)
{
case 0: html = value; break;
case 1: css = value; break;
case 2: js = value; break;
case 3: head = value; break;
}
}
}
[MenuItem("Tools/Web Editor %#e")]
static public void Load()
{
WebEditor window = GetWindow<WebEditor>();
window.Show();
}
void OnEnable()
{
if (!webView)
{
// create webView
webView = CreateInstance<WebViewHook>();
}
}
public void OnBecameInvisible()
{
if (webView)
{
// signal the browser to unhook
webView.Detach();
}
}
void OnDestroy()
{
//Destroy web view
DestroyImmediate(webView);
}
string Compose()
{
return string.Format(Styles.template, html, css, js, head);
}
void OnGUI()
{
// hook to this window
if (webView.Hook(this))
// do the first thing to do
webView.LoadHTML(Compose());
var half = position.width / 2;
// head/body/css/js
var rect = new Rect(0, 0, (half - 50) / 4, 30);
EditorGUI.BeginChangeCheck();
for (int i = 0; i < 4; i++)
{
if (GUI.Toggle(rect, i == panel, Styles.heads[i], Styles.headStyles[i]))
panel = i;
rect.x += rect.width;
}
if (GUI.Button(new Rect(half - 45, 0, 45, 30), "Copy"))
GUIUtility.systemCopyBuffer = Compose().Replace("\n", Environment.NewLine);
if (EditorGUI.EndChangeCheck())
// need this so text field can be updated
GUIUtility.keyboardControl = -1;
// html text field
EditorGUI.BeginChangeCheck();
this[panel] = EditorGUI.TextArea(new Rect(0, 30, half, position.height - 30), this[panel], Styles.textArea);
if (EditorGUI.EndChangeCheck())
webView.LoadHTML(Compose());
if (Event.current.type == EventType.Repaint)
{
// keep the browser aware with resize
webView.OnGUI(new Rect(half, 0, half, position.height));
}
}
}