Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by SuperMasterBlasterLaser · Jun 13, 2015 at 02:54 PM · c#guitransformscale

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).

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DoTA_KAMIKADzE · Jun 13, 2015 at 03:08 PM 0
Share

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.

avatar image SuperMasterBlasterLaser · Jun 13, 2015 at 04:20 PM 0
Share

I have updated my question

avatar image Addyarb · Jun 13, 2015 at 04:48 PM 0
Share

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?

avatar image fafase · Jun 13, 2015 at 05:07 PM 0
Share

Do you really need to update the anchors? Setting them in the middle of the UI element and that would be it.

avatar image digzou · Jun 15, 2015 at 07:22 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
     }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hitmax87 · Apr 04, 2018 at 12:18 PM 0
Share

This.anchor$$anonymous$$in = Anchor$$anonymous$$in; This.anchor$$anonymous$$ax = Anchor$$anonymous$$ax;

avatar image Xtro hitmax87 · Apr 04, 2018 at 02:44 PM 0
Share

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.

avatar image Xtro · Apr 16, 2018 at 04:00 AM 0
Share

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.

avatar image quizcanners · Aug 14, 2020 at 11:27 AM 0
Share

Didn't work with Canvas -> Render $$anonymous$$ode = Screen Space - Camera. Everything flew away very far)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How To Scale GUI Based On Screen Resolution 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges