Unity不能直接加载fbx文件,但是可以在运行时加载obj文件,可以将fbx文件转成obj文件。
- 使用命令行脚本将 fbx 转换为 obj 文件。
- 在 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