Skip to content

Instantly share code, notes, and snippets.

@ntratcliff
Last active May 19, 2017 15:59
Show Gist options
  • Save ntratcliff/4fa58a8fc62bc38c84552fd9afd842d3 to your computer and use it in GitHub Desktop.
Save ntratcliff/4fa58a8fc62bc38c84552fd9afd842d3 to your computer and use it in GitHub Desktop.
Utility class for displaying a webpage in the Unity Editor. Supports Unity 5.0+
using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;
/// <summary>
/// Displays a webpage in the editor.
/// </summary>
public class WebWindow : ScriptableObject
{
private const BindingFlags ALL_FLAGS = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
public static EditorWindow window;
private static Type webViewEditorWindowTabsType;
private static MethodInfo executeJavascriptMethod;
private static FieldInfo initialOpenUrl;
private static FieldInfo webViewField;
/// <summary>
/// Creates a new instance of the WebViewEditorWindow class
/// </summary>
/// <param name="title">The title of the window</param>
/// <param name="sourcesPath">The initial URL to load when the window opens</param>
/// <param name="minWidth">The minimum width of the window</param>
/// <param name="maxWidth">The maximum width of the window</param>
public static void Create(string title, string sourcesPath, int minWidth, int minHeight)
{
// replace existing instance but warn
if (window != null)
Debug.LogWarning("There is already an instance of WebWindow!");
// get the WebViewEditorWindow type
webViewEditorWindowTabsType = getTypeFromAssemblies("WebViewEditorWindowTabs");
// get the window
window = EditorWindow.GetWindow(webViewEditorWindowTabsType);
// set initialopenurl
initialOpenUrl = webViewEditorWindowTabsType.GetField("m_InitialOpenURL", ALL_FLAGS);
initialOpenUrl.SetValue(window, sourcesPath);
// set title
window.titleContent = new GUIContent(title);
window.minSize = new Vector2(minWidth, minHeight);
// show the window
window.Show();
// get the webview
webViewField = webViewEditorWindowTabsType.GetField("m_WebView", ALL_FLAGS);
// get ExecuteJavascript
Type webViewType = getTypeFromAssemblies("WebView");
executeJavascriptMethod = webViewType.GetMethod("ExecuteJavascript", ALL_FLAGS);
}
/// <summary>
/// Executes javascript on the currently displayed page
/// </summary>
/// <param name="js">The javascript to execute</param>
private static void executeJS(string js)
{
executeJavascriptMethod.Invoke(webViewField.GetValue(window), new object[] { js });
}
/// <summary>
/// Extracts a type from the application's assemblies
/// </summary>
/// <param name="typeName">The name of the type</param>
/// <returns>The extracted type</returns>
private static Type getTypeFromAssemblies(string typeName)
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in assemblies)
{
Type[] types = assembly.GetTypes();
foreach (Type type in types)
{
if (type.Name.Equals(typeName, StringComparison.CurrentCultureIgnoreCase)
|| type.Name.Contains('+' + typeName))
return type;
}
}
Debug.LogWarning("Could not find type \"" + typeName + "\" in assemblies.");
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment