xiandie/addons/gif-importer/importer_plugin.gd

75 lines
1.5 KiB
GDScript

# Derived from https://github.com/jegor377/godot-gdgifexporter
@tool
extends EditorImportPlugin
func _can_import_threaded():
return false
func _get_priority():
return 100
func _get_import_order():
# 数字越小越提前
return ResourceImporter.IMPORT_ORDER_DEFAULT - 100
func _get_importer_name():
return "gif.animated.texture.plugin"
func _get_visible_name():
return "Sprite Frames (Thread Safe)"
func _get_recognized_extensions():
return ["gif"]
func _get_save_extension():
return "tres"
func _get_resource_type():
return "SpriteFrames"
func _get_preset_count():
return 0
func _get_preset_name(i):
return ""
func _get_import_options(_path, _i):
return []
func _get_option_visibility(path: String, option_name: StringName, options: Dictionary) -> bool:
return false
# 防止同时加载多个文件时卡死
static var MUTEX = Mutex.new()
func _import(source_file, save_path, options, platform_variants, gen_files):
if GifManager == null:
printerr("GifManager is not available!")
return FAILED
var file = FileAccess.open(source_file, FileAccess.READ)
var buffer = file.get_buffer(file.get_length())
file.close()
MUTEX.lock()
var frames = GifManager.sprite_frames_from_buffer(buffer, 0, 30)
var filename = save_path + "." + _get_save_extension()
var code = ResourceSaver.save(frames, filename)
# await get_tree().create_timer(0.1).timeout
MUTEX.unlock()
print_debug("Imported GIF frames: ", frames.get_frame_count("gif"), " from ", source_file)
return code