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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
5
Question by PedroGabriel00 · Sep 01, 2014 at 06:30 AM · ui4.6rectanchor

[Unity 4.6 Beta] Anchor snap to button (New UI System)

Hello, I was trying the new UI at Android but found the best way to make it stretch correctly was setting the anchors exactly at the same size of the button.

Found out it was too hard to exactly place the anchor at the same size (position) of the button corners because it does not snap to corners\edges and if it is placed a little bit wrong the sizes gonna be different from other elements making it unaligned.

Using the Rect transform anchor presets the anchors always goes to Button parent size (a panel) which is not correctly set when stretching

Like this following image:

alt text

When stretched it looks like this

alt text

And when correctly set and the corners the stretching is correct because it also move the button up as stretch it like this:

alt text

There are any way to make the anchor snap to the button or any other element (it's own) because it is really hard to exactly match it... or something automatically? (maybe I'm doing it all wrong?)

PS: While making this question I noticed I could make the anchor as the button and then snap the button to the anchor because this method it snap correctly and then I could hold shift while move the anchor to resize the button with it (but using values would not work)

Comment
Add comment · 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 Cranke · Sep 03, 2014 at 01:10 AM 0
Share

This is a great question. I am having the same issue.

Using GUI scripting from the past, I was able to utilize GUI.matrix to scale the UI for different resolutions and aspect ratios. The new anchor system is obviously meant to do this too, but I can't find a way to do it nicely in a very similar manner.

$$anonymous$$uch like original poster, I want to create a whole bunch of buttons of a certain size (say 40 pixels high) and have them scale with the screen characteristics. The best way to achieve this seems to be to put the anchors on the four corners of the button, but it is very hard to place the anchors on the corners using the mouse (every button ends up slightly off and ends up looking a little different). And, if you use the anchors first to create a bix into which you stretch button, you can't setup the button to be exactly 40 pixels high.

Is there a way around this?

Or am I missing something?

avatar image Gizmoi · Sep 03, 2014 at 01:14 PM 0
Share

I am having the same problem. In the video tutorial, the anchors do snap to the edges of the current object. I am assu$$anonymous$$g it's a bug with the beta that it doesn't currently.

4 Replies

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

Answer by rakkarage · Sep 03, 2014 at 01:09 PM

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

Senshi made a menu for this

 using UnityEditor;
 using UnityEngine;
 
 public class uGUITools : MonoBehaviour {
     [MenuItem("uGUI/Anchors to Corners %[")]
     static void AnchorsToCorners(){
         RectTransform t = Selection.activeTransform as RectTransform;
         RectTransform pt = Selection.activeTransform.parent as RectTransform;
 
         if(t == null || pt == null) return;
         
         Vector2 newAnchorsMin = new Vector2(t.anchorMin.x + t.offsetMin.x / pt.rect.width,
                                             t.anchorMin.y + t.offsetMin.y / pt.rect.height);
         Vector2 newAnchorsMax = new Vector2(t.anchorMax.x + t.offsetMax.x / pt.rect.width,
                                             t.anchorMax.y + t.offsetMax.y / pt.rect.height);
 
         t.anchorMin = newAnchorsMin;
         t.anchorMax = newAnchorsMax;
         t.offsetMin = t.offsetMax = new Vector2(0, 0);
     }
 
     [MenuItem("uGUI/Corners to Anchors %]")]
     static void CornersToAnchors(){
         RectTransform t = Selection.activeTransform as RectTransform;
 
         if(t == null) return;
 
         t.offsetMin = t.offsetMax = new Vector2(0, 0);
     }
 }
Comment
Add comment · Show 12 · 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 PedroGabriel00 · Sep 03, 2014 at 01:20 PM 1
Share

Thanks for sharing because I really was in need of this! Would like to see something similar in next Unity release...

For people wondering how to use this: $$anonymous$$ake a folder called "Editor" inside your project "assets" folder and place the .cs file inside.

avatar image TheDelhiDuck · Sep 17, 2014 at 09:17 PM 0
Share

Exactly what I was looking for too, thanks.

avatar image fingerbib · Oct 18, 2014 at 11:49 AM 0
Share

thanks, i could not figure out how to add the script.

avatar image doppelmonster · Dec 03, 2014 at 09:46 AM 0
Share

The script works great! But its December now and this essential behavior is still not implemented in the rect?

avatar image Ness · Dec 23, 2014 at 11:02 PM 0
Share

Thanks!!!!

Show more comments
avatar image
0

Answer by Maccyfin · Nov 22, 2016 at 09:05 AM

@rakkarage Awesome. great script. thanks for sharing. Saved me a lot of time :)

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 d3thdrug · May 19, 2017 at 01:54 PM

I was also trying to anchor buttons to the screen, I didn't need to use this script. I just created an empty image and then parent my buttons under the image and used the anchor of the image. Worked fine

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 RX187 · Apr 11, 2018 at 12:36 PM

The post may be old but this just helped a lot, thank you @rakkarage !

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

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

12 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

Related Questions

[Unity 4.6 Beta] Rect Transform Position (New UI System) 7 Answers

Questions about UGUI Rect Transform Anchors. How to make UI objects scale, correctly, with screen? 1 Answer

[4.6 UI] how to move a button *AND* its anchors ? 3 Answers

4.6 UI Text rect does not expand automatically 2 Answers

UI How to configure Left And Right. 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