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

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);
	}
}

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

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

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