Search Unity

Load .obj or .fbx file into gameobject from remote server

Discussion in 'Editor & General Support' started by condoace, Sep 29, 2015.

  1. condoace

    condoace

    Joined:
    Sep 29, 2015
    Posts:
    2
    I am trying to download a obj file or fbx file from a remote server into my game scene dynamically. I want to replace the empty game object with the fbx or obj model. For example I have a fbx file of a 3D house, I want to load this dynamically when a user collides with a specific gameobject... any code ideas?

    I am currently familiar with the code below for downloading images and converting to textures but how would I go about downloading a 3D obj or fbx file and displaying that inside my gameobject

    // Continuously get the latest webcam shot from outside "Friday's" in Times Square
    // and DXT compress them at runtime
    var url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg";

    function Start () {
    // Create a texture in DXT1 format
    renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);
    while(true) {
    // Start a download of the given URL
    var www = new WWW(url);

    // wait until the download is done
    yield www;

    // assign the downloaded image to the main texture of the object
    www.LoadImageIntoTexture(renderer.material.mainTexture);
    }
    }
     
    Last edited: Sep 29, 2015
    eilatc likes this.
  2. Kyle_WG

    Kyle_WG

    Joined:
    Feb 12, 2013
    Posts:
    8
    You'll need to manually create a mesh from the bytes that have been downloaded as that object won't be passed through Unity's AssetImporter,
    Say if you downloaded that file in .obj format you'll need to convert those bytes to the runtime type that is known.
    Take a look at this obj importer and maybe amend it to run during play?
    http://wiki.unity3d.com/index.php?title=ObjImporter.
    With this way you'll have to convert those bytes to a string format first which could be a bit hard on the processing, especially if it's a large .obj file.

    Another way to do it is create an intermediate file format that you can serialize and deserialize easily. (Maybe using a binaryFormatter [although different platforms have some limitations on this]).
    The class will hold the object data (vert / tris etc.) which you can directly serialize to a file. Then when you download it you deserialize those bytes directly back to this object.
    Then you can use this data to set the data on Unity's mesh.
     
    fffMalzbier likes this.