问题描述:
使用HTTPRequest下载一个URL指向的文件(png图片)并不复杂,但当我想在request_completed信号的回调函数中立即使用下载的内容时,却遇到了困难,之前有问题的代码如下:
extends Node2Dfunc _ready():$HTTPRequest.download_file = "DLC/baidu.png"#指定保存的文件名,同时可以指定保存的目录$HTTPRequest.request('https://www.baidu.com/img/bd_logo1.png')func _on_HTTPRequest_request_completed(result, response_code, headers, body):$Sprite.texture = load("res://DLC/baidu.png") #这里会报错,无法读取该资源感谢godotQQ群:691534145的各位大佬提供的帮助,以下代码QQ群友MowⅡ网友提供的,再次感谢。
extends Node2Dfunc _ready():if ResourceLoader.exists('res://Development/DLC/baidu.png'):$Sprite.texture = ResourceLoader.load('res://Development/DLC/baidu.png')else:$HTTPRequest.request('https://www.baidu.com/img/bd_logo1.png')func _on_HTTPRequest_request_completed(result, response_code, headers, body):if response_code == 200:var img = Image.new()if img.load_png_from_buffer(body) == 0:img.save_png('res://Development/DLC/baidu.png')var texture = ImageTexture.new()texture.create_from_image(img)$Sprite.texture = texture不过我总感觉HTTPRequest类提供的download_file这个接口应该有对于这个场景更好的解决办法。改天继续寻找答案