godot - 如何在游戏中导入图像文件并将其设置为纹理
问题描述
基本上我想在游戏运行时导入一个图像文件并将其设置为对象的精灵/纹理。
我刚开始编码,所以我的代码是一团糟,但我会告诉你我想出了什么。
我使用文件对话框来选择我想要的文件:
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
推荐阅读
- uno-platform - Uno 计算器不使用键盘上的 Enter 键(Ubuntu Snap)
- typescript - Typescript 中处理错误的最佳实践
- python - 我怎样才能打印你好和所有女性成员
- python - 经过训练的 E2E 语音识别模型甚至无法正确识别训练数据
- r - IPTW 后的 Kaplan Meier 曲线
- python - 使 seaborn 刻面的宽度与沿 x 轴的数据范围成比例
- html - Bootstrap 3 - 如何仅在一侧拥有容器全宽
- spring-boot - spring-boot saml:配置服务提供者 entityId
- javascript - 无法在主机文件中映射到 IP 的域上设置 document.cookie
- c# - 我可以按(LINQ)分组并添加一个字段并连接另一个吗?