Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
ImageTexture
继承: Texture2D < Texture < Resource < RefCounted < Object
描述
基于 Image 的 Texture2D。对于图像的显示,必须使用 create_from_image() 方法从中创建一个 ImageTexture:
var image = Image.load_from_file("res://icon.svg")
var texture = ImageTexture.create_from_image(image)
$Sprite2D.texture = texture
这样,可以在运行时通过从编辑器内部和外部加载图像来创建纹理。
警告:最好使用 @GDScript.load() 加载导入的纹理,而不是使用 Image.load() 从文件系统中动态加载它们,因为后者可能不适用于导出的项目:
var texture = load("res://icon.svg")
$Sprite2D.texture = texture
这是因为图像必须首先作为 CompressedTexture2D 导入,然后才能使用 @GDScript.load() 加载。如果仍想像加载任何其他 Resource 一样加载图像文件,请将其导入为 Image 资源,然后使用 @GDScript.load() 方法正常加载它。
注意:可以使用 Texture2D.get_image() 方法从导入的纹理中检索该图像,该方法返回该图像的副本:
var texture = load("res://icon.svg")
var image = texture.get_image()
ImageTexture 并不意味着直接在编辑器界面中进行操作,主要用于通过代码在屏幕上动态渲染图像。如果需要从编辑器中按程序生成图像,请考虑实现一个新的 EditorImportPlugin,将图像保存和导入为自定义纹理资源。
注意:由于图形硬件限制,最大纹理大小为 16384×16384 像素。
教程
属性
resource_local_to_scene |
|
方法
create_from_image(image: Image) static |
|
get_format() const |
|
void |
|
void |
set_size_override(size: Vector2i) |
void |
方法说明
ImageTexture create_from_image(image: Image) static 🔗
创建一个新的 ImageTexture,并通过分配和设置来自 Image 的数据来初始化它。
返回纹理的格式,Format 之一。
void set_image(image: Image) 🔗
用新的 Image 替换该纹理的数据。这将为该纹理重新分配新内存。
如果要更新图像,但不需要更改其参数(格式、大小),请改用 update() 以获得更好的性能。
void set_size_override(size: Vector2i) 🔗
将纹理的大小调整为指定的尺寸。
用新的 Image 替换该纹理的数据。
注意:该纹理必须使用 create_from_image() 创建或首先使用 set_image() 方法初始化,然后才能更新。新的图像大小、格式和 mipmap 配置,应与现有纹理的图像配置相匹配。
如果需要频繁更新纹理,请使用该方法而不是 set_image(),这比每次为一个新纹理分配额外内存要快。