+1 vote

How set texture size on sprite node, after dynamic load? After texture load size node is resize to texture size.

var image = ("res://assets/images.png")
get_node("sprite_node").set_texture(image)
in Engine by (207 points)

2 Answers

+3 votes
Best answer

The easiest way to do this is to use sprite scale like so:

var image = ('res://assets/images.png')
var scale = Vector2((percent of width), (percent of height))
var this_sprite = get_node("sprite_node")
this_sprite.set_texture(image)
this_sprite.set_scale(scale)

by (223 points)
selected by

If anyone wanted to set size in pixels, then there is how you do it:

var is = get_node("sprite").get_texture().get_size() #image size
var th = 50 #target height
var tw = 100 #target width
var scale = Vector2((is.x/(is.x/tw))/50, (is.y/(is.y/th))/50)

Somehow while those /50, should be logically /100, they shouldn't. For me, it made the sprites twice as small, so when setting /50 it was perfect. Might be different for you.

Just in case someone else is also looking for it in 3.x, its changed to:

var s = Vector2()

s.x = x_scale
s.y = y_scale
$sprite.set_texture(your_texture)
$sprite.scale = s
+4 votes

var sizeto=Vector2(720,1080)
var size=texture.get
size()
var scalevactor=sizeto/size
scale=scale_vactor

by (37 points)

You are right, I like it most, but you have some syntax error (at least in Godot 3.4).
Here is the correction

    var sizeto=Vector2(80,80)
    var size=texture.get_size()
    var scale_factor=sizeto/size
    scale=scale_factor
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.