Search code examples
c#asp.netasp.net-mvcunity-game-engineunity-webgl

how to use unity3d webgl content in a asp.net mvc view?


I have a unity webgl project and an asp.net mvc project.

I need to show the webgl content in a view.

The first thing that came to my mind was to simply copy the content of the index.html that unity gave me and paste it in a .cshtml file and change the addresses. But when I do this the incorrect header error pops up.

Am I doing some thing wrong or everything wrong. should I change my method entirely?

I also added these file extensions to the web.config

<staticContent>
  <!-- Unity 5.x -->
  <remove fileExtension=".mem" />
  <mimeMap fileExtension=".mem" mimeType="application/octet-stream" />
  <remove fileExtension=".data" />
  <mimeMap fileExtension=".data" mimeType="application/octet-stream" />
  <remove fileExtension=".memgz" />
  <mimeMap fileExtension=".memgz" mimeType="application/octet-stream" />
  <remove fileExtension=".datagz" />
  <mimeMap fileExtension=".datagz" mimeType="application/octet-stream" />
  <remove fileExtension=".unity3dgz" />
  <mimeMap fileExtension=".unity3dgz" mimeType="application/octet-stream" />
  <remove fileExtension=".jsgz" />
  <mimeMap fileExtension=".jsgz" mimeType="application/x-javascript; charset=UTF-8" />
</staticContent>

Solution

  • Basically, you copy all the file references to your view and of course your canvas container with the code that initializes it.

    Add everything that is a css style file with

    @Styles.Render("~/webgl/TemplateData/style.css")
    

    And everything that is a js file with

    @Scripts.Render("~/webgl/TemplateData/UnityProgress.js");
    

    So you`ll have something similar to this

    @{
        ViewBag.Title = "Stage";
        @Styles.Render("~/webgl/TemplateData/style.css")
        @Scripts.Render("~/webgl/TemplateData/UnityProgress.js");
        @Scripts.Render("~/webgl/Build/UnityLoader.js");
    }
    
    <h2>Stage</h2>
    
    <script>
        var gameInstance = UnityLoader.instantiate("gameContainer", "../webgl/Build/Builds.json", {onProgress: UnityProgress});
    </script>
    
    <div class="webgl-content">
        <div id="gameContainer" style="width: 960px; height: 600px"></div>
        <div class="footer">
            <div class="webgl-logo"></div>
            <div class="fullscreen" onclick="gameInstance.SetFullscreen(1)"></div>
            <div class="title">Figurines</div>
        </div>
    </div>
    

    Don`t forget to add unityweb mime type to web.config

    <system.webServer>
        <staticContent>
          <remove fileExtension=".unityweb" />
          <mimeMap fileExtension=".unityweb" mimeType="application/octet-stream" />
        </staticContent>
    </system.webServer>