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
Answer by FortisVenaliter · Apr 13, 2017 at 03:05 PM
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.
Answer by bkarakaya01 · Feb 12, 2019 at 12:30 PM
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
I'm not sure about this %100 but it should work. This scripts will take your obj file and import it to a file in Unity. You have to create a new file in Unity. But if this doesn't work, you should import a "model.prefab" file to Unity because "model.obj" is not a game object and sometimes when you try to import your obj file you have issues with meshes.
biz mi kurtaracaz dünyayı kanka , dinle şunu :)(https://www.youtube.com/watch?v=qHCcOuvfAa8)
Your answer
Follow this Question
Related Questions
Does the gameobject brush create tiles when painting on a 2d Tilemap? 1 Answer
How to activate a component from its reference? 2 Answers
Scene Manager and Keeping Objects when Loading/Unloading + Gameogject.SetActive not working:( 0 Answers
random an image after a event ends 0 Answers
Scene Loading from Main Menu Player Transform not moving 0 Answers