Skip to content

Instantly share code, notes, and snippets.

@wotakuro
Last active January 4, 2023 08:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wotakuro/9e3d0ff4a36961758540a0d99caf7aa9 to your computer and use it in GitHub Desktop.
Save wotakuro/9e3d0ff4a36961758540a0d99caf7aa9 to your computer and use it in GitHub Desktop.
Unity WebGLUtility
using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;
namespace UTJ
{
public class WebGLUtility
{
private static Type httpServerEditorWrapperType;
private static Type websockifyEditorWrapperType;
[MenuItem("Tools/WebGL/CreateWebSock")]
public static void CreateWebsockifyEditorWrapper()
{
if(httpServerEditorWrapperType == null)
{
httpServerEditorWrapperType = GetType("UnityEditor.WebGL.WebsockifyEditorWrapper");
}
var method = httpServerEditorWrapperType.GetMethod("CreateIfNeeded", BindingFlags.Public | BindingFlags.Static);
}
public static void KillWebsockifyEditorWrapper()
{
if (httpServerEditorWrapperType == null)
{
httpServerEditorWrapperType = GetType("UnityEditor.WebGL.WebsockifyEditorWrapper");
}
var method = httpServerEditorWrapperType.GetMethod("Kill", BindingFlags.Public | BindingFlags.Static);
method.Invoke(null, null);
}
[MenuItem("Tools/WebGL/CreateWebServer")]
public static void CreateWebServer()
{
string path = EditorUtility.OpenFolderPanel("select webServerFolder", "", "");
if (!string.IsNullOrEmpty(path))
{
int port = CreateHttpServerEditorWrapper(path);
Application.OpenURL("http://localhost:" + port + "/");
}
}
public static int CreateHttpServerEditorWrapper(string path)
{
if (websockifyEditorWrapperType == null)
{
websockifyEditorWrapperType = GetType("UnityEditor.WebGL.HttpServerEditorWrapper");
}
var method = websockifyEditorWrapperType.GetMethod("CreateIfNeeded", BindingFlags.Public | BindingFlags.Static);
var newArgs = new object[] { path, 0 };
method.Invoke(null, newArgs);
return (int)newArgs[1];
}
public static void KillHttpServerEditorWrapper()
{
if (websockifyEditorWrapperType == null)
{
websockifyEditorWrapperType = GetType("UnityEditor.WebGL.HttpServerEditorWrapper");
}
var method = websockifyEditorWrapperType.GetMethod("Kill", BindingFlags.Public | BindingFlags.Static);
method.Invoke(null, null);
}
private static Type GetType(string name)
{
var asms = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in asms)
{
var type = assembly.GetType(name);
if(type != null)
{
return type;
}
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment