• 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
Question by Helical · Dec 02, 2014 at 10:16 AM · uianchor

uGUI UI, what is the best way to surround an element with anchors? how to move a UI element with anchors attached?

Both questions have to do with the anchor system in the new GUI UI in 4.6.

I find it very usefull to surround my UI element with anchors, as it means that it will keep its relevant size compared to its parent. However there is no actual shortcut for doing so and it requires me to manually place the anchors around the UI element each time, many times a day.

I find the surrounding anchors constalation the most common by far, so there should be a shortcut for it.

The second thing that I do is move UI elements around. Is there a way to move the anchors (surrounding) with them? I know if I hold shift and move an anchor it moves the element, But I could reallly use the other feature which again I can't find.

Comment

People who like this

0 Show 2
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 cougardavis · Dec 03, 2014 at 07:11 PM 0
Share

I don't have an answer for this, but I have the exact same two questions. It would make the workflow a lot easier.

avatar image Helical cougardavis · Dec 03, 2014 at 08:39 PM 0
Share

That reminds me I have found a solution

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Helical · Dec 03, 2014 at 08:40 PM

Apparently there is a place where all the cool kids go to. There is a script there called uGUITools written by senshi. Follow the instructions.

http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/

This is his code with variables renamed for clarity.

         RectTransform myTransform = transform as RectTransform;
         RectTransform parentTransform = transform.parent as RectTransform;
         
         Vector2 newAnchorsMin = new Vector2(myTransform.anchorMin.x + myTransform.offsetMin.x / parentTransform.rect.width,
                                             myTransform.anchorMin.y + myTransform.offsetMin.y / parentTransform.rect.height);
         Vector2 newAnchorsMax = new Vector2(myTransform.anchorMax.x + myTransform.offsetMax.x / parentTransform.rect.width,
                                             myTransform.anchorMax.y + myTransform.offsetMax.y / parentTransform.rect.height);

         myTransform.anchorMin = newAnchorsMin;
         myTransform.anchorMax = newAnchorsMax;
         myTransform.offsetMin = myTransform.offsetMax = new Vector2(0, 0);

What this does is basically repositions the anchors acording to the current anchors position and the offsets. and then sets the offsets to "Vector2(0, 0)" so the element would fit exactly where the anchors are.

I use this code as an extension to the editor workflow. But I believe it could also work dinamically, not tested though.

Comment

People who like this

0 Show 1 · 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 cougardavis · Dec 04, 2014 at 01:03 AM 0
Share

This is useful. Thanks!

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

26 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

Related Questions

Prevent Unity Web Player from seizing the focus. 1 Answer

Instantiating into UI objects 1 Answer

[SOLUTION] set UI RectTransform Anchor Presets from code c# 1 Answer

How to change azure spatial anchor objects? 1 Answer

How to go around creating inventory with new GUI (4.6 Beta)? 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