Servir archivos estáticos precomprimidos (brotli) (archivos Unity webgl) desde la aplicación web local .NET
– UnityAssets3Free
hola , por aqui Daniel y para hoy os traigo
esta nueva pregunta
Estoy tratando de hacer una aplicación web para mostrar unity juegos webgl que tienen archivos comprimidos de Brotli, pero no puedo hacerlo funcionar. Los archivos están en una carpeta en el directorio wwwroot. Intenté construir y servir los archivos del juego descomprimidos y el juego funciona muy bien. El archivo html para es el predeterminado proporcionado por Unity. El error que me sale es el siguiente:
Uncaught SyntaxError: Unexpected token '< ' gameFile.loader.js:1
Uncaught ReferenceError: unityFramework is not defined at HTMLScriptElement.r.onload (gameFile.loader.js:1)
Cuando miro la cadena del iniciador de la solicitud, tiene lo siguiente:
https://localhost:5001/Game/Play/Index/5
https://localhost:5001/builds/gameName/gameFile.loader.js
https://localhost:5001/builds/gameName/gameFile.framework.js.br
https://localhost:5001/User/Home/NotFound
Pero el archivo está en la misma carpeta que se ve aquí:
No entiendo por qué no encuentra el archivo.
Esto es lo que he probado hasta ahora, el controlador de pantalla para los juegos:
public IActionResult Index(int id)
var webRootPath = _hostEnvironment.WebRootPath;
// Get game from DB
var gameFromDb = _unitOfWork.Game.GetFirstOrDefault(filter: c => c.Id == id, includeProperties: "Unity");
// Get gamefile path
var buildFilesUploadFolder = Path.Combine(webRootPath, gameFromDb.buildPathUrl);
// If game dir exists..
if (Directory.Exists(buildFilesUploadFolder))
// Get filenames in directory
var filesInDirectory = Directory.GetFiles(buildFilesUploadFolder);
// Find the loader "gameFile.loader.js" (gameFile.loader.js.br if compressed)
var loaderUrl = filesInDirectory.FirstOrDefault(x => x.Contains("loader"));
if (loaderUrl != null) loaderUrl = loaderUrl.Replace(webRootPath, "").Replace("", "/");
ViewBag.loaderUrl = loaderUrl;
// Find the Framework file "gameFile.framework.js" (gameFile.framework.js.br if compressed)
var frameworkUrl = filesInDirectory.FirstOrDefault(x => x.Contains("framework"));
if (frameworkUrl != null) frameworkUrl = frameworkUrl.Replace(webRootPath, "").Replace("", "/");
ViewBag.frameworkUrl = frameworkUrl;
// Find the code(wasm) file "gameFile.wasm" (gameFile.wasm.br if compressed)
var codeUrl = filesInDirectory.FirstOrDefault(x => x.Contains("wasm"));
if (codeUrl != null) codeUrl = codeUrl.Replace(webRootPath, "").Replace("", "/");
ViewBag.codeUrl = codeUrl;
// Find the mem file (legacy file)
var memoryUrl = filesInDirectory.FirstOrDefault(x => x.Contains("mem"));
if (memoryUrl != null) memoryUrl = memoryUrl.Replace(webRootPath, "").Replace("", "/");
ViewBag.memoryUrl = memoryUrl;
else return NotFound();
return View(gameFromDb);
Inicio.cs:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env... etc)
Lines removed for brevity...
app.UseResponseCompression();
app.UseStaticFiles();
// WebGL
StaticFileOptions option = new StaticFileOptions();
FileExtensionContentTypeProvider contentTypeProvider = (FileExtensionContentTypeProvider)option.ContentTypeProvider ?? new FileExtensionContentTypeProvider();
contentTypeProvider.Mappings.Remove(".unityweb");
contentTypeProvider.Mappings.Add(".unityweb", "application/unityweb");
// Datafile mappings
contentTypeProvider.Mappings.Add(".data", "application/octet-stream");
contentTypeProvider.Mappings.Add(".data.gz", "application/octet-stream");
contentTypeProvider.Mappings.Add(".data.br", "application/octet-stream");
// js filemappings
contentTypeProvider.Mappings.Add(".js.gz", "application/javascript");
contentTypeProvider.Mappings.Add(".js.br", "application/javascript");
//wasm file mappings
contentTypeProvider.Mappings.Remove(".wasm");
contentTypeProvider.Mappings.Add(".wasm", "application/wasm");
contentTypeProvider.Mappings.Add(".wasm.gz", "application/wasm");
contentTypeProvider.Mappings.Add(".wasm.br", "application/wasm");
option.ContentTypeProvider = contentTypeProvider;
option.OnPrepareResponse = context =>
IHeaderDictionary headers = context.Context.Response.Headers;
string contentType = headers["Content-Type"];
if (context.File.Name.EndsWith("js.br"))
contentType = "application/javascript";
headers.Add("Content-Encoding", "br");
else if (context.File.Name.EndsWith("data.br"))
contentType = "application/octet-stream";
headers.Add("Content-Encoding", "br");
else if (context.File.Name.EndsWith("wasm.br"))
contentType = "application/wasm";
headers.Add("Content-Encoding", "br");
headers["Content-Type"] = contentType;
;
app.UseStaticFiles(option);
El Index.cshtml del juego tiene lo siguiente:
<script>
var buildUrl = "builds/@Model.Name.ToLower()";
var loaderUrl = "@ViewBag.loaderUrl";
var config =
dataUrl: "@Html.Raw(@Url.Action("GetGameDataFile", "Play", new id = @Model.Id, fileName = "data" ))",
frameworkUrl: "@ViewBag.frameworkUrl",
codeUrl: "@ViewBag.codeUrl",
memoryUrl: "@ViewBag.memoryUrl",
streamingAssetsUrl: "StreamingAssets",
companyName: "",
productName: "@Model.Name",
productVersion: "n/a"
;
// Continue standard unity template...
</script>
Intenté alojar los archivos comprimidos en AWS S3 y agregarles el tipo de contenido y las metaetiquetas de codificación de contenido y luego usar esos enlaces directamente en el archivo html y eso también funciona bien. Así que creo que a mi aplicación le falta algo, todavía soy nuevo en el desarrollo web.
0
nota: si aun no se resuelve tu pregunta por favor dejar un comentario y pronto lo podremos de nuevo , muchas gracias
sin mas,espero que te funcione