ImageTexture
继承: Texture2D
< Texture
< Resource
< RefCounted
< Object
A Texture2D
based on an Image
.
描述
A Texture2D
based on an Image
. For an image to be displayed, an ImageTexture has to be created from it using the create_from_image
method:
var image = Image.load_from_file("res://icon.svg")
var texture = ImageTexture.create_from_image(image)
$Sprite2D.texture = texture
This way, textures can be created at run-time by loading images both from within the editor and externally.
Warning: Prefer to load imported textures with @GDScript.load
over loading them from within the filesystem dynamically with Image.load
, as it may not work in exported projects:
var texture = load("res://icon.svg")
$Sprite2D.texture = texture
This is because images have to be imported as a CompressedTexture2D
first to be loaded with @GDScript.load
. If you'd still like to load an image file just like any other Resource
, import it as an Image
resource instead, and then load it normally using the @GDScript.load
method.
Note: The image can be retrieved from an imported texture using the Texture2D.get_image
method, which returns a copy of the image:
var texture = load("res://icon.svg")
var image: Image = texture.get_image()
An ImageTexture is not meant to be operated from within the editor interface directly, and is mostly useful for rendering images on screen dynamically via code. If you need to generate images procedurally from within the editor, consider saving and importing images as custom texture resources implementing a new EditorImportPlugin
.
Note: The maximum texture size is 16384×16384 pixels due to graphics hardware limitations.
属性
bool | resource_local_to_scene |
方法
ImageTexture | create_from_image ( image: Image ) static1 |
Format | get_format ( ) const2 |
void | set_image ( image: Image ) |
void | set_size_override ( size: Vector2i ) |
void | update ( image: Image ) |
方法说明
ImageTexture
create_from_image ( image: Image
) static1
Creates a new ImageTexture and initializes it by allocating and setting the data from an Image
.
Returns the format of the texture, one of Format.
void
set_image ( image: Image
)
Replaces the texture's data with a new Image
. This will re-allocate new memory for the texture.
If you want to update the image, but don't need to change its parameters (format, size), use update
instead for better performance.
void
set_size_override ( size: Vector2i
)
Resizes the texture to the specified dimensions.
void
update ( image: Image
)
Replaces the texture's data with a new Image
.
Note: The texture has to be created using create_from_image
or initialized first with the set_image
method before it can be updated. The new image dimensions, format, and mipmaps configuration should match the existing texture's image configuration.
Use this method over set_image
if you need to update the texture frequently, which is faster than allocating additional memory for a new texture each time.
本方法通常需要用户覆盖才能生效。
本方法无副作用,不会修改该实例的任何成员变量。
本方法除了能接受在此处描述的参数外,还能够继续接受任意数量的参数。
本方法用于构造某个类型。
调用本方法无需实例,可直接使用类名进行调用。
本方法描述的是使用本类型作为左操作数的有效运算符。
这个值是由下列位标志构成位掩码的整数。
无返回值。