@tool class_name Sfx extends AudioStreamPlayer # @export var volume_db := 0.0 @export_enum("child", "game/八音盒", "game/旋转锁", "ghost", "lvping", "ui", "c01", "c02") var dir := "ui": set(value): dir = value _update_files() #@export var file: String = "": #set(val): #if val and current_files.has(val): #file = val var sfx: AudioStream var file: String var sfx_root_path = "res://asset/audio/sfx/" var current_files := PackedStringArray() func _ready() -> void: if Engine.is_editor_hint(): _update_files() _reload_sfx() bus = &"game_sfx" func _reload_sfx(): if file and dir: # print("reload sfx", sfx_root_path + dir + "/" + file) sfx = load(sfx_root_path + dir + "/" + file) as AudioStream else: sfx = null stream = sfx func _update_files(): current_files.clear() if not dir: return var dir_access := DirAccess.open(sfx_root_path + dir) as DirAccess for f in dir_access.get_files(): if f.ends_with(".wav") or f.ends_with(".ogg") or f.ends_with(".mp3"): current_files.push_back(f) #for d in dir_access.get_directories(): #var sub_dir_access := DirAccess.open(sfx_root_path + dir + "/" + d) as DirAccess #for f in sub_dir_access.get_files(): #if f.ends_with(".wav") or f.ends_with(".ogg") or f.ends_with(".mp3"): #current_files.push_back(d + "/" + f) if current_files.is_empty(): file = "" elif not file or not current_files.has(file): file = current_files[0] _reload_sfx() notify_property_list_changed() #func _property_can_revert(property: StringName) -> bool: #if property == &"file": #return true #return false # #func _property_get_revert(property: StringName) -> Variant: #if property == &"file": #return "" #return null func _get_property_list(): return [ { "name": &"file", "type": TYPE_STRING, "hint": PROPERTY_HINT_ENUM_SUGGESTION, "hint_string": ",".join(current_files), } ] func _get(property: StringName) -> Variant: if property == &"file": return file return null func _set(property: StringName, value: Variant) -> bool: if property == &"file": file = value _reload_sfx() # notify_property_list_changed() return true return false # func play() -> void: # if sfx: # AudioManager.play_sfx(sfx, volume_db) # queue free 导致 sfx 无法播放,使用全局声源 func global_play() -> void: if sfx: AudioManager.play_sfx(sfx)