42 lines
1.7 KiB
GDScript3
42 lines
1.7 KiB
GDScript3
|
extends Control
|
|||
|
|
|||
|
var gif_path = "res://asset/art/gif/"
|
|||
|
var sprite_frames = preload("res://config/animation/entity_sprite_frames.tres")
|
|||
|
|
|||
|
|
|||
|
func _ready():
|
|||
|
var gif_files = []
|
|||
|
for file in DirAccess.open(gif_path).get_files():
|
|||
|
if file.get_extension() == "gif":
|
|||
|
gif_files.append(file)
|
|||
|
for gif_file in gif_files:
|
|||
|
var path = gif_path + gif_file
|
|||
|
var frames = load(path) as SpriteFrames
|
|||
|
if frames and frames.has_animation("gif"):
|
|||
|
var animation_name = gif_file.get_basename()
|
|||
|
var dir_name = gif_path + animation_name + "/"
|
|||
|
if DirAccess.dir_exists_absolute(dir_name):
|
|||
|
# 如果已经存在则跳过,避免重复导入
|
|||
|
# 重新导入时需要删除文件夹
|
|||
|
continue
|
|||
|
DirAccess.make_dir_absolute(dir_name)
|
|||
|
if not sprite_frames.has_animation(animation_name):
|
|||
|
sprite_frames.add_animation(animation_name)
|
|||
|
else:
|
|||
|
sprite_frames.clear(animation_name)
|
|||
|
sprite_frames.set_animation_loop(animation_name, frames.get_animation_loop("gif"))
|
|||
|
sprite_frames.set_animation_speed(animation_name, frames.get_animation_speed("gif"))
|
|||
|
for i in range(frames.get_frame_count("gif")):
|
|||
|
var texture = frames.get_frame_texture("gif", i) as Texture2D
|
|||
|
var image = texture.get_image()
|
|||
|
# save image to dir
|
|||
|
var image_path = dir_name + str(i) + ".png"
|
|||
|
image.save_png(image_path)
|
|||
|
var new_texture := ImageTexture.create_from_image(image)
|
|||
|
# 设置资源路径,sprite_frames 中便会保存引用路径而不是二进制数据
|
|||
|
new_texture.resource_path = image_path
|
|||
|
var duration = frames.get_frame_duration("gif", i)
|
|||
|
sprite_frames.add_frame(animation_name, new_texture, duration)
|
|||
|
print("Imported: ", animation_name, " frames: ", frames.get_frame_count("gif"))
|
|||
|
ResourceSaver.save(sprite_frames)
|