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.

ImageTexture

繼承: Texture2D < Texture < Resource < RefCounted < Object

基於 ImageTexture2D

說明

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 = 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

false (overrides Resource)

方法

ImageTexture

create_from_image(image: Image) static

void

set_image(image: Image)

void

set_size_override(size: Vector2i)

void

update(image: Image)


方法說明

ImageTexture create_from_image(image: Image) static 🔗

建立一個新的 ImageTexture,並通過分配和設定來自 Image 的資料來初始化它。


void set_image(image: Image) 🔗

用新的 Image 替換該紋理的資料。這將為該紋理重新分配新記憶體。

如果要更新圖像,但不需要更改其參數(格式、大小),請改用 update() 以獲得更好的性能。


void set_size_override(size: Vector2i) 🔗

將紋理的大小調整為指定的尺寸。


void update(image: Image) 🔗

用新的 Image 替換該紋理的資料。

注意:該紋理必須使用 create_from_image() 建立、或首先使用 set_image() 方法初始化,然後才能更新。新的圖像大小、格式和 mipmaps 配置,應與現有紋理的圖像配置相配對。

如果需要頻繁更新紋理,請使用該方法而不是 set_image(),這比每次為一個新紋理分配額外記憶體要快。