Unity WebGL需要运行在WebGL context下,但是在ASP.NET Core MVC项目中默认没有开启这个选项。要解决这个问题,需要打开Kestrel服务器的WebSocket选项,代码示例如下:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseWebRoot("wwwroot")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel(options =>
{
options.ListenLocalhost(5000);
options.ListenLocalhost(5001, listenOptions =>
{
listenOptions.UseHttps("testCert.pfx", "testPassword");
});
options.ConfigureHttpsDefaults(httpsOptions =>
{
httpsOptions.ServerCertificate =
new X509Certificate2("testCert.pfx", "testPassword");
});
options.ConfigureEndpointDefaults(listenOptions =>
{
listenOptions.Protocols =
HttpProtocols.Http1AndHttp2AndHttps;
});
})
.UseStartup
这个例子是在Kestrel服务器上使用HTTPS,如果不需要HTTPS可以简单地省略相应的代码。