如何解决如何在游戏中导入图像文件并将其设置为纹理
基本上我想在游戏运行时导入一个图像文件并将其设置为对象的精灵/纹理。
我刚刚开始编码,所以我的代码一团糟,但我会向你展示我的想法。
我使用文件对话框来选择我想要的文件:
var selected = null
func _process(_delta):
if visible:
selected = get_current_path()
然后我尝试导入我使用加载功能选择的文件并将其设置为纹理:
var tex = ImageTexture.new()
var img = Image
onready var imag = get_node("Sprite")
if Input.is_action_just_pressed("ui_home"):
img = load($FileDialog.selected)
tex.create_from_image(img)
imag.set_texture(tex)
但问题是这只适用于 res:// 文件夹中的文件。我无法从其他目录中导入文件,该文件尚未在 godot 编辑器中作为纹理导入。帮助表示赞赏。谢谢!
解决方法
使用 Image.load(path)
从任意路径加载图像。请务必检查返回的错误代码。
var image_path = "..."
var image = Image.new()
var error = image.load(image_path)
if error != OK:
# handle the error
var texture = ImageTexture.new()
texture.create_from_image(image)
sprite.texture = texture
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。