var texture = ImageTexture.new() var image = Image.new() image.load("res://icon.png") texture.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.
var texture = load("res://icon.png") $Sprite2D.texture = texture
This is because images have to be imported as a StreamTexture2D 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.png") var image : Image = texture.get_image()
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.
get_format ( ) const
void create_from_image ( Image image )
Initializes the texture by allocating and setting the data from an Image.
Format get_format ( ) const
Returns the format of the texture, one of Format.
void set_size_override ( Vector2 size )
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 initialized first with the create_from_image method before it can be updated. The new image dimensions, format, and mipmaps configuration should match the existing texture's image configuration, otherwise it has to be re-created with the create_from_image method.
Use this method over create_from_image if you need to update the texture frequently, which is faster than allocating additional memory for a new texture each time.