How can i load FBX file in Unity runtime?

拟墨画扇 提交于 2020-06-08 03:12:08

问题


I'm in hololens project. My boss says, load the fbx file at runtime (we use unity). Our fbx file changes at runtime, so we must reload it (fbx is change from 3dMAx).

I tried the AssetBundle, but I can't create Assetbundle files without unity or unity editor mode. As far as I know, only resource (in Project tab) can insert assets to assetbundle. If I can build Assetbundle file by fbx file without unity project, I can do all.

To sum it up: I need way how to load fbx in runtime without unity resource. If I make fbx asset to assetbundle in runtime (fbx is not belong in project tab), it's ok.


edit)

Use trilib asset is the best way. i try make own FBX loader, it's too difficult and Big job. I think trilib is not perfect, but, it's best.


回答1:


You can read follow content:

is-there-an-easy-way-to-import-fbx-files-runtime:

In short: No.

Unity doesn't support importing / loading of any model format at runtime. The only exception are AssetBundles. Your options are basically:

  • Use AssetBundles

  • Find an importer for your desired format for Unity

  • Write such an importer yourself

  • Look for a feature request / write one yourself to ask UT if they add runtime model loading routines.

how-to-convert-3ds-fbx-model-into-asset-bundle-at-run-time:

Is there a tool which converts 3D models to asset bundles ? or is it possible to convert them at run time ?

You can't during the run-time because every Unity API to create Assetbundle is only available on the Editor for Editor plugins only.

I search many website for this question. Finally, I recommand that you could use this asset.It's is not free,but worth.It could save much time for you.

trilib-unity-model-loader

I hope this can help you.




回答2:


You could consider converting FBX into glTF, which Unity can load at runtime with UnityGLTF tools.

https://github.com/KhronosGroup/UnityGLTF




回答3:


Unity can not load the fbx file directly, but can load the obj file at runtime, you can convert fbx file to obj files.

  1. Convert fbx to obj files with command line script.
  2. Load obj at Unity runtime.

One way to convert fbx/dae to obj is using Blender command line, so you should installed Blender (support platform: Linux, Mac, Windows). Create this python file:

import bpy
import sys
for obj in bpy.data.objects:
    if (obj.name == "Lamp") | (obj.name == "Camera") | (obj.name == "Cube"):
        obj.select = False
    else:
        obj.select = True
argv = sys.argv
argv = argv[argv.index("--") + 1:]
inputpath = argv[0]
outputpath = argv[1]
bpy.ops.import_scene.fbx(filepath=inputpath, axis_forward='-Z', axis_up='Y', directory="", filter_glob="*.fbx", ui_tab='MAIN', use_manual_orientation=False, global_scale=1, bake_space_transform=False, use_custom_normals=True, use_image_search=True, use_alpha_decals=False, decal_offset=0, use_anim=True, anim_offset=1, use_custom_props=True, use_custom_props_enum_as_string=True, ignore_leaf_bones=False, force_connect_children=False, automatic_bone_orientation=False, primary_bone_axis='Y', secondary_bone_axis='X', use_prepost_rot=True)
bpy.ops.export_scene.obj(filepath=outputpath, check_existing=False, axis_forward='-Z', axis_up='Y', filter_glob="*.obj;*.mtl", use_selection=True, use_animation=False, use_mesh_modifiers=True, use_mesh_modifiers_render=False, use_edges=True, use_smooth_groups=False, use_smooth_groups_bitflags=False, use_normals=True, use_uvs=True, use_materials=True, use_triangles=False, use_nurbs=False, use_vertex_groups=False, use_blen_objects=True, group_by_object=False, group_by_material=False, keep_vertex_order=False, global_scale=1, path_mode='COPY')
print("finished!")

Run this file in shell:

/your blender path/blender --background --python /your python path/convert.py -- /your input path/xxx.fbx /your output path/xxx.obj

Finally, use this plugin to load obj files at run time: https://assetstore.unity.com/packages/tools/modeling/runtime-obj-importer-49547

More about blender command line to import and export files: https://blender.stackexchange.com/questions/16563/how-can-i-run-blender-from-the-command-line-to-export-and-import-models



来源:https://stackoverflow.com/questions/51701361/how-can-i-load-fbx-file-in-unity-runtime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!