- Home /
[4.6] How do I make a UI object the same size as another UI object?
In my project I'm making a pause screen with the new UI system. I have an icon that will move around the screen, but I don't know how to get my icon to size correctly.
As shown in this image: http://imgur.com/2Z7r8Eb I need rectangle A to take on the dimensions of rectangle B so that it will completely overlap it.
How would I do this?
Answer by Mmmpies · Feb 04, 2015 at 09:45 PM
Hmmm, getting late (yet again) where I am but I think I can answer even if sleepy!
Set the anchors to the center of the parent for both objects, then drag the panels onto the public slots of this script:
using UnityEngine;
using System.Collections;
public class MatchSize : MonoBehaviour {
public RectTransform RTB; // the one you want to resize
public RectTransform RTA; // the one you want to copy the size of
// Use this for initialization
void Start () {
RTB.sizeDelta = new Vector2(RTA.rect.width, RTA.rect.height);
RTB.localPosition = RTA.localPosition;
}
}
Sorry if rubbish, it's late!
I seem to be having some problems with it. It doesn't scale properly still. This is what is happening:http://imgur.com/YJ2214r
I have my moving UI set to it's own variable and I have a variable set up for my objects. I have the anchor set to the center of the object and the borders around it as shown in this image: http://imgur.com/y$$anonymous$$RTaz3
Any idea why it isn't properly overlaying?
Sorry night time fell. In that image you got the anchors set to the corners of the object. $$anonymous$$aybe we need another approach if you absolutely need it that way.
In my test both objects had the same parent object and the anchors in the center of the parent panel. That way it picks up the right settings.
I'll see if I can another way of doing it.
EDIT
In fact only the panel that moves needs to have it's anchors set to the center of the parent panel for both objects, is that workable for you?
I moved the anchor points on my moving UI to the center of the object and that got it to work. Thank you very much for your help!
The only issue now is it stretches on the sides: http://imgur.com/v8$$anonymous$$2hNV
Any suggestions for fixing that?
First, you can post images directly on here and it's pretty fast. See the button to the right of the paperclip. Just saves time jumping backwards and forwards between web pages.
As for the other issue that Vector2 is totally O$$anonymous$$ for manipulation. It's just 2 floats so just try (RTA.rect.width - 1f, RTA.rect.height) but test it at different resolution and aspect ratios (if res/aspect changes are likely for your platform).
Your answer