- Home /
uGUI keep position and size of GUI elements when anchors change
Hello.
In my script I have changed uGUI elements anchors by assigning new Vector2 for anchorMin and anchorMax to its RectTransform.
When change occurs, the position and size of GUI element changes.
How to make GUI keep its old position and size even if anchors changed?
In this code I'm changing anchor values:
RectTransform rect = (RectTransform) gameobject.transform;
rect.anchorMin = new Vector2(0f, rect.anchorMin.y);
rect.anchorMax = new Vector2(0f, rect.anchorMax.y);
After that, my UI elements changes its position and its width(It bacames zero).
Could you update your question with description of which behavior of UI element you're trying to achieve? Because that's anchor's sole purpose to resize and reposition UI element based on how you configured it. You could resize you're UI element to "keep visual size" the same, but I doubt that you would need that in first place.
I don't have a good answer for this, but I will say that changing anchors on-the-fly isn't really an elegant way to do things, and that there is a better way to achieve what you're trying to do. Perhaps you could post a screenshot, or describe more in-depth the effect you're trying to achieve?
Do you really need to update the anchors? Setting them in the middle of the UI element and that would be it.
After updating your anchors, you should also set your Rectransforms' 'Offset$$anonymous$$ax' and 'Offset$$anonymous$$in' to vector2.zero. This way your elements will always stretch correctly corresponding to your anchors.
Answer by DoTA_KAMIKADzE · Jun 13, 2015 at 05:47 PM
Ok my guess is that you have initially all 4 anchors spread somewhere, then for no apparent reason you want to move them to the left side (the reason for which I asked and you haven't answered). As people in comments below your question wrote - that's a strange thing to want, but anyway I'll try to answer how you can retain the width.
Example #1: Keep anchors respective to each other and move them left:
rect.anchorMax = new Vector2(rect.anchorMax.x - rect.anchorMin.x, rect.anchorMax.y);
rect.anchorMin = new Vector2(0f, rect.anchorMin.y);
Example #2: "Squash" the anchors to the left:
float rrw = rect.rect.width;
rect.anchorMin = new Vector2(0f, rect.anchorMin.y);
rect.anchorMax = new Vector2(0f, rect.anchorMax.y);
rect.sizeDelta = new Vector2(rrw, rect.sizeDelta.y);
If this is still not the thing that you want (for unexplained reason) then try to properly explain (quoting myself...):
which behavior of UI element you're trying to achieve?
Answer by IgorAherne · Jun 14, 2018 at 06:19 PM
Also, this function might be handy: SetSizeWithCurrentAnchors()
https://docs.unity3d.com/ScriptReference/RectTransform.SetSizeWithCurrentAnchors.html
Answer by Xtro · Oct 13, 2017 at 04:56 PM
My solution:
public static void SetAnchors(this RectTransform This, Vector2 AnchorMin, Vector2 AnchorMax)
{
var Parent = This.parent;
if (Parent) This.SetParent(null);
This.anchorMin = AnchorMin;
This.anchorMax = AnchorMax;
if (Parent) This.SetParent(Parent);
}
This.anchor$$anonymous$$in = Anchor$$anonymous$$in; This.anchor$$anonymous$$ax = Anchor$$anonymous$$ax;
Unfortunately, it's not that simple :( When you do this what you suggested, the size/position of the UI object changes dramatically.
$$anonymous$$y implementation sets the anchors while keeping the rect in same position and size.
I think I see a bug in my code above. I'll test it and update my suggestion if it's wrong.
Here I updated my solution. Basically, if the RectTransform doesn't have a parent, the anchors aren't in effect so we can modify them as we like. If it has a parent, we need to save and restore the parent info before and after modifying the anchor values.
Didn't work with Canvas -> Render $$anonymous$$ode = Screen Space - Camera. Everything flew away very far)
Your answer
Follow this Question
Related Questions
Animate the scaling of a gameObject 4 Answers
Saving default transform of an object c# 1 Answer
Scale UI element On One Side 0 Answers
Distribute terrain in zones 3 Answers