- Home /
[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:
When stretched it looks like this
And when correctly set and the corners the stretching is correct because it also move the button up as stretch it like this:
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)
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?
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.
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);
}
}
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.
The script works great! But its December now and this essential behavior is still not implemented in the rect?
Answer by Maccyfin · Nov 22, 2016 at 09:05 AM
@rakkarage Awesome. great script. thanks for sharing. Saved me a lot of time :)
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
Answer by RX187 · Apr 11, 2018 at 12:36 PM
The post may be old but this just helped a lot, thank you @rakkarage !
Your answer
Follow this Question
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