You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Godot引擎中访问游戏.exe同目录下的文件?

在Godot中加载与exe同目录的外部资源

核心实现逻辑

通过Godot的OS.get_executable_path()获取游戏exe的路径,再拼接外部资源的相对路径,就能直接读取与exe同目录下的文件,无需将资源嵌入引擎包。

具体代码示例

加载图片(Texture2D)

# 获取exe所在目录
var exe_dir = OS.get_executable_path().get_base_dir()
# 拼接同目录下的图片路径(示例图片名为test.png)
var image_path = exe_dir + "/test.png"

# 加载并赋值到Sprite2D节点
var texture = load(image_path)
if texture:
    $Sprite2D.texture = texture
else:
    print("图片加载失败,请检查文件是否存在或路径是否正确")

加载音频(AudioStream)

var exe_dir = OS.get_executable_path().get_base_dir()
var audio_path = exe_dir + "/bgm.mp3"

# 加载并播放音频
var audio_stream = load(image_path)
if audio_stream:
    $AudioStreamPlayer.stream = audio_stream
    $AudioStreamPlayer.play()
else:
    print("音频加载失败,请检查文件是否存在或路径是否正确")

关键注意事项

  • 发布游戏时,务必将外部资源与exe放在同一目录,路径区分大小写(Linux/macOS平台需特别注意)。
  • 若需要实时生效修改后的资源,可使用FileWatcher节点定时监听文件变化,触发重新加载逻辑。
  • 全程使用动态路径拼接,避免硬编码绝对路径,保证跨平台兼容性。

内容的提问来源于stack exchange,提问作者Boas World Draw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 22:01:00