+1 vote

Suppose I have a ColorRect inside a Control.
I set the ColorRect's anchors to
left 0.5, right 0.5, top 0, bottom 0

I adjust it in the editor view so the ColorRect is nicely centered in the Control.

Then suppose at run-time I want to resize the ColorRect's width to be wider. How can I easily do this while keeping the ColorRect centered?

What I tried was to change the rect_size.x in code, but this just resulted in the ColorRect getting longer to the right but not centered.

I thought having anchors would mean resizing it would keep it centered?

in Engine by (259 points)

1 Answer

+2 votes

I found a way. After changing the rect_size.x I need to call

set_anchors_and_margins_preset(Control.PRESET_CENTER, Control.PRESET_MODE_KEEP_SIZE)

On the ColorRect to 're-center' it. I still wish there was another way as I think simple resizing should respect the anchors.

by (259 points)

Thanks for your answer. Did you find a way of doing it without writing code? for example, if I want to grow a panel with animation..

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.