Last active
February 9, 2025 09:22
-
-
Save wotakuro/9e3d0ff4a36961758540a0d99caf7aa9 to your computer and use it in GitHub Desktop.
Unity WebGLUtility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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