("Example", path, 200, 530, 800, 600);
14 |
15 | // Call javascript function within of the HTML file.
16 | EditorApplication.update = () =>
17 | {
18 | w.InvokeJSMethod("example", "changeText", Time.realtimeSinceStartup.ToString());
19 | };
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/Assets/Demo02/Editor/Demo02.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 36b6190ec3cc1d140911f26d7013d597
3 | timeCreated: 1487237941
4 | licenseType: Pro
5 | MonoImporter:
6 | serializedVersion: 2
7 | defaultReferences: []
8 | executionOrder: 0
9 | icon: {instanceID: 0}
10 | userData:
11 | assetBundleName:
12 | assetBundleVariant:
13 |
--------------------------------------------------------------------------------
/Assets/Demo02/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Time sent from Unity editor put on here.
8 |
9 |
16 |
17 |
--------------------------------------------------------------------------------
/Assets/Demo02/index.html.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: c0efd2c67db95bc4db296be7e537f288
3 | timeCreated: 1487237716
4 | licenseType: Pro
5 | TextScriptImporter:
6 | userData:
7 | assetBundleName:
8 | assetBundleVariant:
9 |
--------------------------------------------------------------------------------
/Assets/Editor.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 08e14e0441756ac45afc973ce77d68f9
3 | folderAsset: yes
4 | timeCreated: 1487217908
5 | licenseType: Pro
6 | DefaultImporter:
7 | userData:
8 | assetBundleName:
9 | assetBundleVariant:
10 |
--------------------------------------------------------------------------------
/Assets/Editor/CustomWebViewEditorWindow.cs:
--------------------------------------------------------------------------------
1 | using UnityEngine;
2 | using System;
3 | using UnityEditor;
4 | using System.Reflection;
5 | using System.Collections.Generic;
6 | using System.IO;
7 | using System.Text;
8 |
9 | ///
10 | /// Open a WebViewEditorWindowTabs like AssetStore editor window.
11 | ///
12 | public class CustomWebViewEditorWindow
13 | {
14 | private object webViewEditorWindow = null;
15 |
16 | static object webView;
17 |
18 | static Type webViewEditorWindowType {
19 | get {
20 | /*
21 | //return Types.GetType ("UnityEditor.Web.WebViewEditorWindowTabs", "UnityEditor.dll");
22 | // "UnityEditor.Web.WebViewEditorWindow" does not work with Unity 5.5.x. Obsolete?
23 | string typeName = "UnityEditor.Web.WebViewEditorWindowTabs";
24 |
25 | // With Unity 5.5.x, calling UnityEngine.Types.GetType cause the following error:
26 | // error CS0619 : 'UnityEngine.Types.GetType (string, string)'is obsolete :
27 | // `This was an internal method which is no longer used'
28 | Type type = Assembly.Load("UnityEditor.dll").GetType(typeName);
29 | return type;
30 | */
31 | #if UNITY_5_4_OR_NEWER
32 | return (typeof(Editor).Assembly).GetType("UnityEditor.Web.WebViewEditorWindowTabs");
33 | //var type = Types.GetType("UnityEditor.Web.WebViewEditorWindowTabs", "UnityEditor.dll");
34 | #else
35 | return Types.GetType("UnityEditor.Web.WebViewEditorWindow", "UnityEditor.dll");
36 | #endif
37 | }
38 | }
39 |
40 | static Type GetType(string typeName, string assemblyName)
41 | {
42 | #if UNITY_5_4_OR_NEWER
43 | return Assembly.Load(assemblyName).GetType(typeName);
44 | #else
45 | return Types.GetType(typeName, assemblyName);
46 | #endif
47 | }
48 |
49 | const string PATH = "Temp/webViewEditorWindowNames.txt";
50 |
51 | [InitializeOnLoadMethod]
52 | static void AddGlobalObjects ()
53 | {
54 | if (File.Exists (PATH)) {
55 |
56 | foreach (var globalObjectName in File.ReadAllLines(PATH)) {
57 | var type = Type.GetType (globalObjectName);
58 |
59 | if (type == null)
60 | continue;
61 | AddGlobalObject (type);
62 | }
63 | }
64 |
65 | }
66 |
67 | public static T CreateWebViewEditorWindow (string title, string sourcesPath, int minWidth, int minHeight, int maxWidth, int maxHeight) where T : CustomWebViewEditorWindow, new()
68 | {
69 | var createMethod = webViewEditorWindowType.GetMethod ("Create", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy).MakeGenericMethod (webViewEditorWindowType);
70 |
71 | var window = createMethod.Invoke (null, new object[] {
72 | title,
73 | sourcesPath,
74 | minWidth,
75 | minHeight,
76 | maxWidth,
77 | maxHeight
78 | });
79 |
80 | var customWebEditorWindow = new T { webViewEditorWindow = window };
81 |
82 | EditorApplication.delayCall += () => {
83 | EditorApplication.delayCall += () => {
84 | webView = webViewEditorWindowType.GetField ("m_WebView", BindingFlags.NonPublic | BindingFlags.Instance).GetValue (customWebEditorWindow.webViewEditorWindow);
85 | AddGlobalObject ();
86 | };
87 | };
88 |
89 | return customWebEditorWindow;
90 | }
91 |
92 | private static void AddGlobalObject () where T : CustomWebViewEditorWindow
93 | {
94 | File.AppendAllText ("Temp/webViewEditorWindowNames.txt", typeof(T).Name + "\n", System.Text.Encoding.UTF8);
95 | AddGlobalObject (typeof(T));
96 | }
97 |
98 | private static void AddGlobalObject (Type type)
99 | {
100 | var jsproxyMgrType = GetType("UnityEditor.Web.JSProxyMgr", "UnityEditor.dll");
101 | var instance = jsproxyMgrType.GetMethod ("GetInstance").Invoke (null, new object[0]);
102 |
103 | if (jsproxyMgrType != null && instance != null)
104 | {
105 | jsproxyMgrType.GetMethod("AddGlobalObject").Invoke(instance, new object[] {type.Name, Activator.CreateInstance (type)});
106 | }
107 | }
108 |
109 | ///
110 | /// InvokeJSMethod can not be called on Unity 5.5.x (seems to same on Unity 5.4.x)
111 | /// See the issue for the reason.
112 | ///
113 | public void InvokeJSMethod (string objectName, string funcName, params object[] args)
114 | {
115 | #if UNITY_5_4_OR_NEWER
116 | try
117 | {
118 | if (webViewEditorWindow != null && webView != null)
119 | {
120 | MethodInfo invokeJSMethod = webView.GetType().GetMethod("ExecuteJavascript");
121 | if (invokeJSMethod != null)
122 | {
123 | object[] param = new object[] {PrepareJSMethod(objectName, funcName, args)};
124 | invokeJSMethod.Invoke(webView, param);
125 | }
126 | }
127 | }
128 | catch (TargetInvocationException ex)
129 | {
130 | //FIXME: exception raise whenever the WebViewEditorWindowTabs window is closed.
131 |
132 | // should be set as null to open the editor window again.
133 | webView = null;
134 | // force stop calling the delegate even after closing the window.
135 | EditorApplication.update = null;
136 | Debug.LogFormat("{0}", ex.Message);
137 | return;
138 | }
139 | #else
140 | var invokeJSMethodMethod = webViewEditorWindowType.GetMethod ("InvokeJSMethod", BindingFlags.NonPublic | BindingFlags.Instance);
141 | if (invokeJSMethodMethod != null)
142 | {
143 | invokeJSMethodMethod.Invoke(webViewEditorWindow, new object[] { objectName, funcName, args });
144 | }
145 | else
146 | Debug.LogErrorFormat("No {0}.{1} is found.", objectName, funcName);
147 | #endif
148 |
149 | }
150 |
151 | public static string PrepareJSMethod(string objectName, string name, params object[] args)
152 | {
153 | StringBuilder stringBuilder = new StringBuilder();
154 | stringBuilder.Append(objectName);
155 | stringBuilder.Append('.');
156 | stringBuilder.Append(name);
157 | stringBuilder.Append('(');
158 | bool flag = true;
159 | for (int i = 0; i < args.Length; i++)
160 | {
161 | object obj = args[i];
162 | if (!flag)
163 | {
164 | stringBuilder.Append(',');
165 | }
166 | bool flag2 = obj is string;
167 | if (flag2)
168 | {
169 | stringBuilder.Append('"');
170 | }
171 | stringBuilder.Append(obj);
172 | if (flag2)
173 | {
174 | stringBuilder.Append('"');
175 | }
176 | flag = false;
177 | }
178 | stringBuilder.Append(");");
179 | return stringBuilder.ToString();
180 |
181 | }
182 |
183 | public static Type GetTypeFromAllAssemblies(string typeName)
184 | {
185 | Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
186 | foreach (Assembly assembly in assemblies)
187 | {
188 | Type[] types = assembly.GetTypes();
189 | foreach (Type type in types)
190 | {
191 | if (type.Name.Equals(typeName, StringComparison.CurrentCultureIgnoreCase) ||
192 | type.Name.Contains('+' + typeName)) //+ check for inline classes
193 | return type;
194 | }
195 | }
196 | return null;
197 |
198 | }
199 |
200 | }
201 |
--------------------------------------------------------------------------------
/Assets/Editor/CustomWebViewEditorWindow.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 9ac1391fb837ddd43b342e757ac86406
3 | timeCreated: 1487224896
4 | licenseType: Pro
5 | MonoImporter:
6 | serializedVersion: 2
7 | defaultReferences: []
8 | executionOrder: 0
9 | icon: {instanceID: 0}
10 | userData:
11 | assetBundleName:
12 | assetBundleVariant:
13 |
--------------------------------------------------------------------------------
/Assets/Editor/WebViewEditorWindow.cs:
--------------------------------------------------------------------------------
1 | using UnityEngine;
2 | using UnityEditor;
3 | using System;
4 | using System.Reflection;
5 |
6 | ///
7 | /// Open a webview within Unity editor. (similar as AssetStore window)
8 | ///
9 | public class WebViewEditorWindow : EditorWindow//ScriptableObject
10 | {
11 | static string Url = "http://google.com";
12 |
13 | [MenuItem("Window/WebViewWindow")]
14 | static void Open()
15 | {
16 | // "UnityEditor.Web.WebViewEditorWindow" does not work with Unity 5.5.x. Obsolete?
17 | string typeName = "UnityEditor.Web.WebViewEditorWindowTabs";
18 |
19 | // With Unity 5.5.x, calling UnityEngine.Types.GetType cause the following error:
20 | // error CS0619 : 'UnityEngine.Types.GetType (string, string)'is obsolete :
21 | // `This was an internal method which is no longer used'
22 | Type type = Assembly.Load("UnityEditor.dll").GetType(typeName);
23 |
24 | BindingFlags Flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
25 | var methodInfo = type.GetMethod("Create", Flags);
26 | methodInfo = methodInfo.MakeGenericMethod(type);
27 |
28 | methodInfo.Invoke(null, new object[] { "WebView", Url, 200, 530, 800, 600 });
29 | }
30 | }
--------------------------------------------------------------------------------
/Assets/Editor/WebViewEditorWindow.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 8a2b5edc1d36c6743ac5f323dd864294
3 | timeCreated: 1487221409
4 | licenseType: Pro
5 | MonoImporter:
6 | serializedVersion: 2
7 | defaultReferences: []
8 | executionOrder: 0
9 | icon: {instanceID: 0}
10 | userData:
11 | assetBundleName:
12 | assetBundleVariant:
13 |
--------------------------------------------------------------------------------
/Assets/HTML.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: d1354ab5bb6c9cd44ab38c832cfc6f33
3 | folderAsset: yes
4 | timeCreated: 1487228435
5 | licenseType: Pro
6 | DefaultImporter:
7 | userData:
8 | assetBundleName:
9 | assetBundleVariant:
10 |
--------------------------------------------------------------------------------
/Assets/HTML/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Play
7 | Pause
8 | Step
9 |
31 |
32 |
--------------------------------------------------------------------------------
/Assets/HTML/index.html.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: d75211e5393dabe489c38e9cc96ec1b6
3 | timeCreated: 1487225161
4 | licenseType: Pro
5 | TextScriptImporter:
6 | userData:
7 | assetBundleName:
8 | assetBundleVariant:
9 |
--------------------------------------------------------------------------------
/ProjectSettings/AudioManager.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/AudioManager.asset
--------------------------------------------------------------------------------
/ProjectSettings/ClusterInputManager.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/ClusterInputManager.asset
--------------------------------------------------------------------------------
/ProjectSettings/DynamicsManager.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/DynamicsManager.asset
--------------------------------------------------------------------------------
/ProjectSettings/EditorBuildSettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/EditorBuildSettings.asset
--------------------------------------------------------------------------------
/ProjectSettings/EditorSettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/EditorSettings.asset
--------------------------------------------------------------------------------
/ProjectSettings/GraphicsSettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/GraphicsSettings.asset
--------------------------------------------------------------------------------
/ProjectSettings/InputManager.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/InputManager.asset
--------------------------------------------------------------------------------
/ProjectSettings/NavMeshAreas.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/NavMeshAreas.asset
--------------------------------------------------------------------------------
/ProjectSettings/NetworkManager.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/NetworkManager.asset
--------------------------------------------------------------------------------
/ProjectSettings/Physics2DSettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/Physics2DSettings.asset
--------------------------------------------------------------------------------
/ProjectSettings/ProjectSettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/ProjectSettings.asset
--------------------------------------------------------------------------------
/ProjectSettings/ProjectVersion.txt:
--------------------------------------------------------------------------------
1 | m_EditorVersion: 5.5.1f1
2 |
--------------------------------------------------------------------------------
/ProjectSettings/QualitySettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/QualitySettings.asset
--------------------------------------------------------------------------------
/ProjectSettings/TagManager.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/TagManager.asset
--------------------------------------------------------------------------------
/ProjectSettings/TimeManager.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/TimeManager.asset
--------------------------------------------------------------------------------
/ProjectSettings/UnityConnectSettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/ProjectSettings/UnityConnectSettings.asset
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Unity-WebViewEditorWindow
2 |
3 | A simple demonstration of WebView within Unity editor.
4 |
5 |
6 |
7 |
8 |
9 | Original post can be found on [here](http://qiita.com/kyusyukeigo/items/71db22676c6f4743913e), a blog page of @anchan828
10 |
11 | **Note**: This is not a way to open webview within an running applicationis. (only for within Unity editor) If you want to open a webview within any running application, should consider other solutions. See [unity-webview](https://github.com/gree/unity-webview) on its github page for that purpose.
12 |
--------------------------------------------------------------------------------
/images/screenshot01.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kimsama/Unity-WebViewEditorWindow/2d6968aa108114bb6c3eaeeeef6992002802d407/images/screenshot01.png
--------------------------------------------------------------------------------