- 工程目录,注意素材命名(人物名/动作_行x列.png)
- 素材预览
3. 递归指定目录扫码png文件工具类 file_helper.gd
class_name file_helper
static func scan(path : String) -> Array:
var file_name := ""
var files := []
var dir := DirAccess.open(path)
if dir:
dir.list_dir_begin()
file_name = dir.get_next()
while file_name != "":
if dir.current_is_dir():
var sub_path = path + "/" + file_name
files += scan(sub_path)
else:
if file_name.get_extension() == "png":
var name := path + "/" + file_name
files.push_back(name)
file_name = dir.get_next()
dir.list_dir_end()
else:
print("Failed to open:" + path)
return files
- 递归指定目录下的所有PNG图片批量生成各个角色多个动作动画文件并保存
@tool
extends EditorScript
func _run():
var texture_base_path = "res://素材//人物//"
var save_base_path = "res://素材//动画//人物//"
var save_file_format = "tres"
batch_save_tres(texture_base_path, save_base_path, save_file_format)
# 遍历路径下的全部PNG图片批量生成动画文件并保存
# texture_base_path 纹理图片遍历基础路径
# save_base_path 动画文件保存基础路径
# save_file_format 动画保存文件格式
func batch_save_tres(texture_base_path, save_base_path, save_file_format):
var files = file_helper.scan(texture_base_path)
for file in range(files.size()):
var texture_path = files[file]
var texture_name_rows_and_columns = texture_path.get_file().get_slice(".", 0).get_slice("_", 1)
var rows_number = texture_name_rows_and_columns.get_slice("x", 0)
var columns_number = texture_name_rows_and_columns.get_slice("x", 1)
save_one_tres(texture_path, save_base_path, save_file_format, int(rows_number), int(columns_number))
# 保存单个动画文件
# texture_path 纹理集全路径
# save_base_path 保存基本路径
# save_file_format 动画保存文件格式
# sprite_rows_number 纹理行数,每行一个动画
# sprite_columns_number 纹理列数,每列是一个动画中的一帧
func save_one_tres(texture_path, save_base_path, save_file_format, sprite_rows_number, sprite_columns_number):
# 处理纹理名称
var texture_name = texture_path.get_file().get_slice(".", 0)
# 处理保存路径
var save_path = save_base_path + texture_name + "." + save_file_format
# 分割出动画名称
var animation_library_name = texture_name
if texture_name.find("_") != -1:
animation_library_name = texture_name.get_slice("_", 0)
# 设置纹理
var texture = ResourceLoader.load(texture_path)
var base_frame = AtlasTexture.new()
base_frame.atlas = texture
# 帧宽和高
var sprite_width = texture.get_width() / sprite_columns_number
var sprite_height = texture.get_height() / sprite_rows_number
# 批量添加动画集
var sprite_frames = get_animations(base_frame, animation_library_name, sprite_width, sprite_height, sprite_rows_number, sprite_columns_number)
# 保存动画集文件
ResourceSaver.save(sprite_frames, save_path, ResourceSaver.FLAG_REPLACE_SUBRESOURCE_PATHS)
# 批量添加动画集
func get_animations(base_frame, animation_library_name, sprite_width, sprite_height, sprite_rows_number, sprite_columns_number):
var sprite_frames = SpriteFrames.new()
sprite_frames.remove_animation("default")
for x in range(sprite_rows_number):
var animation_name = animation_library_name + "_" + String.num(x)
sprite_frames.add_animation(animation_name)
# 设置动画帧率
sprite_frames.set_animation_speed(animation_name, sprite_columns_number)
for y in range(sprite_columns_number):
var frame = base_frame.duplicate()
frame.region = Rect2(y * sprite_width, x * sprite_height, sprite_width, sprite_height)
sprite_frames.add_frame(animation_name, frame)
return sprite_frames
- 运行结果,多个动作多个方向批量生成完成: