链接:
Unreal Python API.
思路
1.直接在API搜索自己想干嘛,然后查看合适的方法  2.点进看中的方法,描述合适,然后开始研究怎么用 
这个方法需要的参数,是Array(AssetImportTask)类型的,继续点进去看看这个类型是怎么来的。
AssetImportTask里面直接一堆参数,Base是unreal.object——unreal._object,那就直接进到unreal._object  unreal._object中有个set_editor_property,所以我们可以直接用set_editor_property来设置AssetImportTask里面的参数,再返回给我们搜到的方法:import_asset_tasks(import_tasks)就可以了。 
写代码
刚开始写这种脚本,从下往上写是不错的方法。 按照我们思路来,先写最直接的方法,然后填空就可以了
import unreal
import_tasks = []
unreal.AssetTools.import_asset_tasks(import_tasks)
然后我们需要一个类型是AssetImportTask的对象,放到列表里面
file = unreal.AssetImportTask()
file.set_editor_property("automated",True)
file.set_editor_property("destination_name","ABC")
file.set_editor_property("destination_path","/Game/texture/")
file.set_editor_property("filename",asset_path)
file.set_editor_property("replace_existing",True)
file.set_editor_property("replace_existing_settings",True)
file.set_editor_property("save",True)
import_tasks.append(file)
设置属性的时候我们发现了,有个属性竟然需要我们的文件路径(废话),那么赶紧把文件路径给它吧!
asset_path = "C:\Users\dzj\Desktop\Game\asadafh.TGA"
最后我们把代码返回来,编译一下:
LogPython: Error: TypeError: descriptor 'import_asset_tasks' requires a 'AssetTools' object but received a 'list'
得到一个错误,‘import_asset_tasks’需要的是’AssetTools’ object
但我们的AssetTool不是一个object,所以我们直接搜索AssetTool,看谁能返回一个Base是Object的AssetTool给我们。  此时就只有这个helpers能进我们眼了,进去看看,确实符合要求  所以我们要把最后一句代码改一改
unreal.AssetToolsHelpers.get_asset_tools().import_asset_tasks(import_tasks)
再执行,成功导入。 
完整代码
import unreal
import_tasks = []
asset_path = "C:\\Users\\dzj\\Desktop\\Game\\asadafh.TGA"
file = unreal.AssetImportTask()
file.set_editor_property("automated",True)
file.set_editor_property("destination_name","ABC")
file.set_editor_property("destination_path","/Game/texture/")
file.set_editor_property("filename",asset_path)
file.set_editor_property("replace_existing",True)
file.set_editor_property("replace_existing_settings",True)
file.set_editor_property("save",True)
import_tasks.append(file)
unreal.AssetToolsHelpers.get_asset_tools().import_asset_tasks(import_tasks)
需要批量的话,只要在asset_path上整个for循环就可以了
|