How to load a GameObject from Byte[] in the scene on runtime?

Hi all,

I apologize for being a novice and for asking an obvious question.

I am downloading a 3D object from URL. I am using UnityWebRequest to do so. The thing is the downloaded object is in the form of System.Byte but I want it to be GameObject and I cannot do that on the fly, can’t even cast it.

This object is an .OBJ file of 22MB.

Basically I need a a piece of code that loads an object at runtime.

Thank you,
Arjun

Well, a .OBJ file is not a gameobject, so I’m not sure how the code should be expected to cast it as such.

.OBJ is a model file. That can only be imported in the editor… If you want to import it into a scene, you’ll need to write (or find online and import) a script to read the format and import it into a mesh object. From there, you can apply the mesh to a MeshFilter with a MeshRenderer and have it appear in your scene.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class ObjDownloader : MonoBehaviour
{

    private string modelName = "NAME"
    public Button download;
    public ObjImporter objImporter;
    public GameObject emptyPrefabWithMeshRenderer;

    // Use this for initialization
    void Start()
    {
        Debug.Log("Started");
        download.onClick.AddListener(() => StartCoroutine(ImportObject()));
    }

    // Update is called once per frame
    IEnumerator ImportObject()
    {

        WWW www = new WWW("http://Link" + modelName);
        yield return www;

        if (string.IsNullOrEmpty(www.error))
        {
            Debug.Log("Download Error");
        }
        else
        {
            string write_path = Application.dataPath + "/Objects/" + modelName;

            System.IO.File.WriteAllBytes(write_path, www.bytes);

            Debug.Log("Wrote to path");
        }

        GameObject spawnedPrefab;

        Mesh importedMesh = objImporter.ImportFile(Application.dataPath + "/Objects/" + modelName);

        spawnedPrefab = Instantiate(emptyPrefabWithMeshRenderer);
        spawnedPrefab.transform.position = new Vector3(0, 0, 0);
        spawnedPrefab.GetComponent<MeshFilter>().mesh = importedMesh;
    }
}

And also take this script : http://wiki.unity3d.com/index.php/ObjImporter