Problem with instantiating images of different sizes

Hello people!! Well I’m going straight to the point, I have a project 90% of the way walking, but I had a problem when instantiating the images, I have a single prefab and gameObject that instantiates the dragable images on the screen, but they are coming out distorted, because the images have different sizes, I was wondering if there is a possibility that these images are set to their original size?

I will leave a picture illustrating my problem :slight_smile:

Well, basically, the images are starting from a single base ~
It would be very interesting to discover a component or some logic that would help me to program this. If necessary I can attach the codes that are making this happen.
Everything is working as expected, except this part, I do not want to change the size of the images because I intend to make a trash can with collider, it would be a very large collision area. So… I wanted to know if there is finally the possibility of instantiating these images in your original size from this premise (see image).

In your image inspector, see that SET NATIVE SIZE button?

That button is obviously only for editor time but behind every editor button is code.

Best of all, the code for Unity UI is available.

You can get the UI source off github but the function you want is right here:

        public override void SetNativeSize()
        {
            if (activeSprite != null)
            {
                float w = activeSprite.rect.width / pixelsPerUnit;
                float h = activeSprite.rect.height / pixelsPerUnit;
                rectTransform.anchorMax = rectTransform.anchorMin;
                rectTransform.sizeDelta = new Vector2(w, h);
                SetAllDirty();
            }
        }

Highly recommend digging through the UnityEditor.UI namespace sourcecode. Lotsa neat insight in there whenever I’m having issues with making UI work.

Yes, thank you very much! after some research i managed to find ^^

2 Likes