Exporting a gameobject FROM unity webgl

Hi, I want to create an downloadable obj or fbx file through my unity webgl game. Is it possible? if possible, how do I go about doing it?

Well, this depends on many different things. First of all in order to create a “download” for the enduser you can either use DataURIs or you need a server which supports some kind of scripting language like PHP.

DataURIs are a neat way to directly pass data from javascript that runs inside the browser to the browser for further processing. However as you can read on the SO question i’ve linked, browser support varies and the data size is quite limited. So usually you would first “send” your data to your server and then start a download and retrieve it from the server. This need to be dynamic hence you need server-side scripting support for that.

Next problem is FBX is a proprietary file format which is quite complex and doesn’t have proper documentation. You would have to use the SDK that Autodesk provides and run some sort of converter / builder plugin on your server to generate such a file.

OBJ on the other hand is a relative simple format. It can be generated without much hassle directly inside the browser. Though as mentioned above the main issue is to initiate a download. The only reliable way is to go over your server.

edit

I quickly created an example project and tried some approaches. I only have FireFox as browser, so i can’t test other browsers but in FF it works as it should:

public class WebDownloadHelper
{
    // Source: http://stackoverflow.com/a/27284736/1607924
    static string scriptTemplate = @"
            var link = document.createElement(""a"");
            link.download = '{0}';
            link.href = 'data:application/octet-stream;charset=utf-8;base64,{1}';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            delete link;
        ";

    public static void InitiateDownload(string aName, byte[] aData)
    {
        string base64 = System.Convert.ToBase64String(aData);
        string script = string.Format(scriptTemplate, aName, base64);
        Application.ExternalEval(script);
    }
    public static void InitiateDownload(string aName, string aData)
    {
        byte[] data = System.Text.Encoding.UTF8.GetBytes(aData);
        InitiateDownload(aName, data);
    }
}

This of course only works in a WebGL or Webplayer(now deprecated) build when executed inside a browser. Inside the editor it doesn’t work.

All you have to do is calling WebDownloadHelper.InitiateDownload with a default file name which will be used in the download dialog and the actual data as byte array. I also made an overload that takes a string which will be UTF8 encoded. So it’s easier to create a download from a string.

Here’s an example WebGL build. There are two buttons. One will simply download “random generated byte data”, the other will download the text that is entered in the textfield below.

So to everyone: feel free to test if the download works for you and leave a comment. If you do, don’t forget to mention the browser you’re using.

edit
I just had some spare time so i implemented an image download into my WebGL Mandelbrot renderer ^^. So now we have the first real usage which works great. Now you can render the images inside your browser and export the final image as JPG or PNG. The actual export is just a single line of code:

WebDownloadHelper.InitiateDownload("Image.png", tex.EncodeToPNG());

I also added URL fragment parameters which allows you to send a link of a specific point / zoom level to someone else so he can generate the same image. The URL is automatically updated when you change the view. The controls were initially designed for an Android build (hence the size of the buttons).

Here are some nice points: Example1 Example2 Example3