- Home /
How to convert byte[] to GameObject?
I am working on an app that will allow a user to download a FBX model and load it into the app on the iPad. I have successfully written the FBX file to the Application.persistentDataPath on the iPad, and I'm trying to load this new model into the scene.
I am able to read the file as a byte[] and am looking for the best way to Instantiate those bytes into the scene.
If anyone knows how I can to this it would be very helpful.
Thanks in advance!
Answer by Bunny83 · Feb 17, 2015 at 02:44 AM
That's not possible since the Unity engine can't read FBX files at runtime. Reading FBX files is an edit-time-feature of the Editor. If you want to load a model at runtime you need to parse the data yourself. FBX is a very complex format and actually needs the SDK from Autodesk.
It's usually easier to use a more simple format like OBJ. There are several OBJ loaders out there.
Thanks for the advice.
As I'm a bit new to Unity I apologize if the answer to my next question is an obvious one.
I'm going to have a login dialog that, upon successful connection, gives a user a list of models to download and import. When you say 'load a model at runtime' I need to be able to load these models as part of some event trigger, rather than on startup of the app.
Would using OBJ be the best format to do this?
As @fafase says, convert the FBX files into GameObjects, and write these GameObjects into AssetBundles. Then put these AssetBundles onto your server.
Thanks for all the advice everyone!
I have successfully loaded the FBX model an an AssetBundle. Creating the AssetBundle required the Unity Editor. Is there any way to create an AssetBundle on the fly?
Ideally I would like my web service to be able to take a FBX, generate the assetbundle, then i can download the assetbunble to my device.
What is the end game for displaying the fbx on the device? I ask because to me, it seems like the best way to accomplish what you want to do is to get the FBX SD$$anonymous$$, use it to read the fbx file, then create a new game object with a new blank mesh (and meshfilter), then edit the underlying mesh data using the data from the fbx.
you'll want to populate the following:
mesh.vertices mesh.uv mesh.uv1 mesh.uv2 mesh.normal mesh.tangent mesh.triangles
The fbx will have all of this data (except possibly for uv1 and uv2, which are optional). you just need to create your mesh from scratch using the data, pump it into a gameobject, and display it.