如何在 Unity 运行时加载 FBX 文件?

How can I load FBX file in Unity runtime?

我正在进行全息镜头项目。我的老板说,在运行时加载 fbx 文件(我们使用 unity)。我们的 fbx 文件在运行时发生变化,所以我们必须重新加载它(fbx 是从 3dMAx 更改而来的)。

我尝试了 AssetBundle,但我无法在没有 unity 或 unity 编辑器模式的情况下创建 Assetbundle 文件。据我所知,只有资源(在项目选项卡中)可以将资产插入资产包。如果我能在没有unity项目的情况下通过fbx文件构建Assetbundle文件,我就可以做到。

总结一下:我需要如何在没有统一资源的情况下在运行时加载 fbx。如果我在运行时将 fbx 资产制作为 assetbundle(fbx 不属于项目选项卡),没关系。


编辑)

使用 trilib 资产是最好的方法。我尝试制作自己的 FBX 加载器,这太难了,而且工作量很大。我认为trilib并不完美,但它是最好的。

您可以阅读以下内容:

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.

:

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.

这个问题我搜索了很多网站。最后,我建议你可以使用这个 asset.It 不是免费的,但是 worth.It 可以为你节省很多时间。

trilib-unity-model-loader

希望对您有所帮助。

您可以考虑将 FBX 转换为 glTF,Unity 可以在运行时使用 UnityGLTF 工具加载它。

https://github.com/KhronosGroup/UnityGLTF

Unity不能直接加载fbx文件,但可以在运行时加载obj文件,可以将fbx文件转为obj文件。

  1. 使用命令行脚本将 fbx 转换为 obj 文件。
  2. 在 Unity 运行 时间加载对象。

将 fbx/dae 转换为 obj 的一种方法是使用 Blender 命令行,因此您应该安装 Blender(支持平台:Linux、Mac、Windows)。 创建此 python 文件:

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!")

运行 这个文件在 shell:

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

最后,使用这个插件在运行时间加载obj文件: https://assetstore.unity.com/packages/tools/modeling/runtime-obj-importer-49547

有关导入和导出文件的 blender 命令行的更多信息:https://blender.stackexchange.com/questions/16563/how-can-i-run-blender-from-the-command-line-to-export-and-import-models

您可以尝试使用 Trilib。 Unity Asset store 中的一个插件,我个人在一个项目中使用过它并且非常喜欢它。

它支持各种文件格式(OBJ、FBX、PLY、GLTF),还可以导入几何、材质、纹理、动画甚至混合变形等所有内容。

与 Android、iOS、WebGL 和桌面平台兼容。

这里是 link 以使其更容易:- https://assetstore.unity.com/packages/tools/modeling/trilib-2-model-loading-package-157548