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



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

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

综上所述:我需要一种方法,在没有统一资源的情况下,如何在运行时加载fbx。如果我在运行时让fbx资产到资产捆绑包(fbx不属于项目选项卡(,那没关系。


编辑(

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

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

https://github.com/KhronosGroup/UnityGLTF

您可以阅读以下内容:

有没有一种简单的方法在运行时导入fbx文件:

简而言之:没有

Unity不支持在运行时。唯一的例外是AssetBundles。您的选择是基本上:

  • 使用资产捆绑

  • 为Unity 找到您想要的格式的进口商

  • 自己写这样一个进口商

  • 寻找一个特性请求/自己写一个,询问UT是否添加了运行时模型加载例程。

如何转换-3ds-fbx-model-in-to-asset-bundle运行时:

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

运行时不能因为用于创建资产绑定的每个Unity API仅在编辑器仅用于编辑器插件。

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

trilib统一模型装载机

我希望这能帮助你。

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

您可以尝试使用Trilib。Unity资产商店中有一个插件,我个人在一个项目中使用过它,我非常喜欢它。

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

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

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

最新更新