This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+2 votes

I'm making a game which is simillar to the Rush Hour puzzle game, I want to split a texture into some parts, and apply it to different textures.
My texture is on a 1:N ratio and I want to split it on N sprites.
for example, I have the car image below:
full car

As it is a 1:2 ratio, I want to apply it on 2 different sprites, as represented here:
enter image description here

I would like to do it all through the code as I want the user to be able to add their own car images, I've heard about atlas but I don't understand how can I use it only through code.

Godot version 3.2.1
in Engine by (29 points)
edited by

2 Answers

+1 vote
Best answer

Yes, you can by using TextureAstlas
Tutorial: https://www.youtube.com/watch?v=oEBUXW674dc

by (160 points)
selected by

I'll add this video to my godot watchlist. Thank you for sharing!

+2 votes

You can use the same strategy as originally posted in this QA post.

I was working on something similar, and ended up with the following:

func your_function_here():
    var texture := _tile_map.tile_set.tile_get_texture(id)
    var region := Rect2(0, 0, 16, 16)
    texture_button.texture_normal = get_cropped_texture(texture, region)

func get_cropped_texture(texture : Texture, region : Rect2) -> AtlasTexture:
    var atlas_texture := AtlasTexture.new()
    atlas_texture.set_atlas(texture)
    atlas_texture.set_region(region)
    return atlas_texture
by (24 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.