【问题标题】:How can I load FBX file in Unity runtime?如何在 Unity 运行时加载 FBX 文件?
【发布时间】:2019-01-13 01:02:29
【问题描述】:

我正在进行一个全息透镜项目。我的老板说,在运行时加载 fbx 文件(我们使用统一)。我们的 fbx 文件在运行时更改,因此我们必须重新加载它(fbx 是从 3dMAx 更改的)。

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

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


编辑)

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

【问题讨论】:

标签: c# unity3d fbx


【解决方案1】:

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

https://github.com/KhronosGroup/UnityGLTF

【讨论】:

  • 这是有史以来最好的解决方案,您只需在服务器端使用 node.js C++ 扩展将 fbx 转换为 gltf github.com/cyrillef/FBX-glTF
【解决方案2】:

您可以阅读以下内容:

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

简而言之:没有。

Unity 不支持在以下位置导入/加载任何模型格式 运行。唯一的例外是 AssetBundle。你的选择是 基本上:

  • 使用资源包

  • 为你想要的 Unity 格式找到一个导入器

  • 自己写一个这样的导入器

  • 寻找一个功能请求/自己写一个来询问 UT 是否添加运行时模型加载例程。

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

是否有将 3D 模型转换为资产包的工具?或者是 可以在运行时转换它们吗?

你不能在运行时 因为每个用于创建 Assetbundle 的 Unity API 仅在 仅限编辑器插件的编辑器。

我在很多网站上搜索这个问题。最后,我建议您可以使用此资产。它不是免费的,但值得。它可以为您节省很多时间。

trilib-unity-model-loader

希望对你有帮助。

【讨论】:

    【解决方案3】:

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

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

    将 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

    【讨论】:

      【解决方案4】:

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

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

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

      这里的链接使它更容易:- https://assetstore.unity.com/packages/tools/modeling/trilib-2-model-loading-package-157548

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
      猜你喜欢
      • 1970-01-01
      • 2014-05-04
      • 2013-11-23
      • 2013-11-11
      • 1970-01-01
      • 2015-10-13
      • 2013-07-19
      • 2019-10-26
      • 2013-01-25
      相关资源
      最近更新 更多