Skip to content

Instantly share code, notes, and snippets.

@dimmduh
Created April 16, 2017 12:45
Show Gist options
  • Save dimmduh/60603d7cdc74a65b98bf17b9474bc09b to your computer and use it in GitHub Desktop.
Save dimmduh/60603d7cdc74a65b98bf17b9474bc09b to your computer and use it in GitHub Desktop.
using System;
using System.Reflection;
using System.Text;
using UnityEditor;
using UnityEngine;
public class ModelExplorerWindow : EditorWindow {
[MenuItem("Window/Example03")]
static void Example3()
{
var window = ModelExplorerWindow.GetWindow<ModelExplorerWindow>();
window.Show();
var thisWindowGuiView = typeof(EditorWindow).GetField("m_Parent", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(window);
Type webViewType = GetTypeFromAllAssemblies("WebView");
var webView = ScriptableObject.CreateInstance(webViewType);
Rect webViewRect = new Rect(0, 0, 1024, window.position.height);
webViewType.GetMethod("InitWebView").Invoke(webView, new object[] { thisWindowGuiView, (int)webViewRect.x, (int)webViewRect.y, (int)webViewRect.width, (int)webViewRect.height, true });
webViewType.GetMethod("LoadURL").Invoke(webView, new object[] { Application.dataPath + "/ModelExplorer/index.html" });
var invokeJSMethod = webViewType.GetMethod("ExecuteJavascript");
if (invokeJSMethod != null)
{
EditorApplication.update += () => invokeJSMethod.Invoke(webView, new object[] { PrepareJSMethod("example", "changeText", Time.realtimeSinceStartup.ToString()) });
}
else
Debug.LogError("No InvokeJSMethod is found.");
}
private static string PrepareJSMethod(string objectName, string name, params object[] args)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(objectName);
stringBuilder.Append('.');
stringBuilder.Append(name);
stringBuilder.Append('(');
bool flag = true;
for (int i = 0; i < args.Length; i++)
{
object obj = args[i];
if (!flag)
{
stringBuilder.Append(',');
}
bool flag2 = obj is string;
if (flag2)
{
stringBuilder.Append('"');
}
stringBuilder.Append(obj);
if (flag2)
{
stringBuilder.Append('"');
}
flag = false;
}
stringBuilder.Append(");");
return stringBuilder.ToString();
}
public static Type GetTypeFromAllAssemblies(string typeName)
{
Assembly[] assemblies = System.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)) //+ check for inline classes
return type;
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment