76 lines
1.6 KiB
GDScript
76 lines
1.6 KiB
GDScript
@tool
|
|
class_name Sfx extends AudioStreamPlayer
|
|
|
|
@export_enum("child", "game/八音盒", "game/旋转锁", "ghost", "lvping", "ui", "c01", "c02") var dir := "ui":
|
|
set(value):
|
|
dir = value
|
|
_update_files()
|
|
|
|
var sfx: AudioStream
|
|
|
|
var file: String
|
|
var sfx_root_path = "res://asset/audio/sfx/"
|
|
var current_files := PackedStringArray()
|
|
|
|
|
|
func _ready() -> void:
|
|
bus = &"game_sfx"
|
|
_update_files()
|
|
_reload_sfx()
|
|
|
|
|
|
func _reload_sfx():
|
|
# 仅在编辑器模式下加载音频 stream
|
|
if not Engine.is_editor_hint():
|
|
return
|
|
var path = sfx_root_path + dir + "/" + file
|
|
if file and dir and FileAccess.file_exists(path):
|
|
sfx = load(sfx_root_path + dir + "/" + file) as AudioStream
|
|
else:
|
|
sfx = null
|
|
set("stream", sfx)
|
|
if stream:
|
|
print("sfx [", name, "] stream=", stream.resource_path)
|
|
|
|
|
|
func _update_files():
|
|
current_files.clear()
|
|
if not dir or not Engine.is_editor_hint():
|
|
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)
|
|
notify_property_list_changed()
|
|
|
|
|
|
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()
|
|
return true
|
|
return false
|
|
|
|
|
|
# queue free 导致 sfx 无法播放,使用全局声源
|
|
func global_play() -> void:
|
|
if stream:
|
|
AudioManager.play_sfx(stream)
|