40 lines
No EOL
2.3 KiB
Markdown
40 lines
No EOL
2.3 KiB
Markdown
To use a tile from an atlas for a `TextureRect` in a `ScrollContainer` in Godot 4.3, follow these steps:
|
|
|
|
1. **Create or Load a TileSet Atlas**:
|
|
- Import your sprite sheet into Godot and create a `TileSet` resource. Define the tile regions in the `TileSet` editor[3].
|
|
|
|
2. **Set Up the AtlasTexture**:
|
|
- For each tile you want to use, create an `AtlasTexture` resource. Set its `atlas` property to your sprite sheet and define the `region` to crop the specific tile[4].
|
|
|
|
3. **Configure the TextureRect**:
|
|
```gdscript
|
|
var atlas_texture = AtlasTexture.new()
|
|
atlas_texture.atlas = preload("res://path_to_your_spritesheet.png")
|
|
atlas_texture.region = Rect2(x, y, width, height) # Coordinates of the tile
|
|
$TextureRect.texture = atlas_texture
|
|
```
|
|
Replace `x`, `y`, `width`, and `height` with the tile's position and dimensions[4].
|
|
|
|
4. **Add to ScrollContainer**:
|
|
- Place the `TextureRect` inside a container (e.g., `VBoxContainer`) that is a child of the `ScrollContainer`. Ensure the container's size exceeds the `ScrollContainer`'s viewport to enable scrolling[2][5].
|
|
|
|
**Troubleshooting**:
|
|
- If textures don't update after modifying the atlas, avoid using "Make Unique" on `AtlasTexture` instances, as this breaks the link to the original atlas[1].
|
|
- For crashes when adding many `TextureRect` nodes, ensure the `ScrollContainer`'s children are properly nested and avoid excessive updates during runtime[5].
|
|
|
|
This approach leverages Godot's `AtlasTexture` to efficiently reuse tiles from a sprite sheet while maintaining scrollable UI elements.
|
|
|
|
Citations:
|
|
[1] https://www.reddit.com/r/godot/comments/12cttew/godot_4_textureatlas_questions/
|
|
[2] https://docs.godotengine.org/en/stable/classes/class_scrollcontainer.html
|
|
[3] https://docs.godotengine.org/en/stable/tutorials/2d/using_tilesets.html
|
|
[4] https://docs.godotengine.org/de/stable/classes/class_atlastexture.html
|
|
[5] https://github.com/godotengine/godot/issues/75713
|
|
[6] https://www.youtube.com/watch?v=w3jla1fTZuQ
|
|
[7] https://docs.godotengine.org/en/4.3/classes/class_atlastexture.html
|
|
[8] https://docs.godotengine.org/en/4.3/classes/class_scrollcontainer.html
|
|
[9] https://docs.godotengine.org/en/stable/classes/class_texturerect.html
|
|
[10] https://www.youtube.com/watch?v=2o_IiotMnls
|
|
|
|
---
|
|
Antwort von Perplexity: https://www.perplexity.ai/search/godot-4-3-use-tile-from-atlas-HHNbSG1FRniUTuYX96GNzA?utm_source=copy_output |