Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Using WebGL Builds in Blazor Server-Side

Discussion in 'Scripting' started by Cyber-Dog, Apr 2, 2021.

  1. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Hey,

    It took me a few hours to work this out, so I wanted to write a post here in case someone else tries to do this and faces the same Issue.

    It appears new ASP.NET Core applications, have dropped the web.config file, so the unity documentation can only get you halfway there.
    https://docs.unity3d.com/2021.1/Documentation/Manual/webgl-server-configuration-code-samples.html

    From this MSDN page, I found out how to do it on Blazor.
    https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/static-files?view=aspnetcore-5.0

    I'm not sure if the settings will be different for the client-side build of Blazor. But here is the code for Server-Side. This is for a no compression build of WebGL, the compressed build setting can be found in the first link provided on the Unity documentation page. Just Edit accordingly.

    Edit the Configure() function of the Startup.cs file like so:

    Code (CSharp):
    1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    2. {
    3.     if (env.IsDevelopment())
    4.     {
    5.         app.UseDeveloperExceptionPage();
    6.     }
    7.     else
    8.     {
    9.         app.UseExceptionHandler("/Error");
    10.         app.UseHsts();
    11.     }
    12.  
    13.     app.UseHttpsRedirection();
    14.        
    15.     // Remove This
    16.     // app.UseStaticFiles();
    17.  
    18.     // Add This
    19.     var provider = new FileExtensionContentTypeProvider();
    20.     provider.Mappings.Remove(".data");
    21.     provider.Mappings[".data"] = "application/octet-stream";
    22.     provider.Mappings.Remove(".wasm");
    23.     provider.Mappings[".wasm"] = "application/wasm";
    24.     provider.Mappings.Remove(".symbols.json");
    25.     provider.Mappings[".symbols.json"] = "application/octet-stream";
    26.     app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider });
    27.     app.UseStaticFiles();
    28.     //--------------
    29.  
    30.     app.UseRouting();
    31.  
    32.     app.UseEndpoints(endpoints =>
    33.     {
    34.         endpoints.MapBlazorHub();
    35.         endpoints.MapFallbackToPage("/_Host");
    36.     });
    37. }

    Happy Hunting ;)
     
    Last edited: Apr 2, 2021
    radiantboy and Joel-Santos like this.
  2. mcshoop

    mcshoop

    Joined:
    Mar 23, 2019
    Posts:
    1
    Bless you
     
  3. CraigWW

    CraigWW

    Joined:
    Jun 10, 2019
    Posts:
    6
    thanks, well done
     
  4. Torsilver

    Torsilver

    Joined:
    Nov 8, 2018
    Posts:
    4
    Bless you
     
  5. Joel-Santos

    Joel-Santos

    Joined:
    Feb 23, 2010
    Posts:
    121
    Saved my life.
    Just wanted to say that you can configure just one route to support this by doing this after app.UseEndpoints...

    Code (CSharp):
    1.  
    2. var provider = new FileExtensionContentTypeProvider();
    3. provider.Mappings.Remove(".data");
    4. provider.Mappings[".data"] = "application/octet-stream";
    5. provider.Mappings.Remove(".wasm");
    6. provider.Mappings[".wasm"] = "application/wasm";
    7. provider.Mappings.Remove(".symbols.json");
    8. provider.Mappings[".symbols.json"] = "application/octet-stream";
    9.  
    10. app.MapWhen(ctx => !ctx.Request.Path.StartsWithSegments("/_framework/BuildWebGL"),
    11.                 subApp => subApp.UseStaticFiles(new StaticFileOptions() { ContentTypeProvider = provider }));
    12.  
    In this case it would only do it for the folder BuildWebGL inside wwwroot
     
    radiantboy and Cyber-Dog like this.
  6. monquarter

    monquarter

    Joined:
    Jul 15, 2019
    Posts:
    2
    I am struggling with this. I am using .net 6 on a new versions of Blazor server, there isn't a Startup.cs. Presumably, these changes would go in Program.cs? When I place the BuildWebGL folder inside wwwroot I get a 404 server error.
    Code (CSharp):
    1. using Microsoft.AspNetCore.Components;
    2. using Microsoft.AspNetCore.Components.Web;
    3. using Microsoft.AspNetCore.StaticFiles;
    4. using UnityBlazor.Data;
    5.  
    6. var builder = WebApplication.CreateBuilder(args);
    7.  
    8. // Add services to the container.
    9. builder.Services.AddRazorPages();
    10. builder.Services.AddServerSideBlazor();
    11. builder.Services.AddSingleton<WeatherForecastService>();
    12.  
    13. var app = builder.Build();
    14.  
    15. // Configure the HTTP request pipeline.
    16. if (!app.Environment.IsDevelopment())
    17. {
    18.     app.UseExceptionHandler("/Error");
    19.     // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    20.     app.UseHsts();
    21. }
    22.  
    23. app.UseHttpsRedirection();
    24.  
    25. //app.UseStaticFiles();
    26.  
    27. var provider = new FileExtensionContentTypeProvider();
    28. provider.Mappings.Remove(".data");
    29. provider.Mappings[".data"] = "application/octet-stream";
    30. provider.Mappings.Remove(".wasm");
    31. provider.Mappings[".wasm"] = "application/wasm";
    32. provider.Mappings.Remove(".symbols.json");
    33. provider.Mappings[".symbols.json"] = "application/octet-stream";
    34.  
    35. app.MapWhen(ctx => !ctx.Request.Path.StartsWithSegments("/_framework/BuildWebGL"),
    36.                 subApp => subApp.UseStaticFiles(new StaticFileOptions() { ContentTypeProvider = provider }));
    37.  
    38. app.UseRouting();
    39.  
    40. app.MapBlazorHub();
    41. app.MapFallbackToPage("/_Host");
    42.  
    43. app.Run();
     
  7. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,635
    did u find a solution?