RectTransform position relative to Its Parent scale

I have a UI Element that can be Zoom In & Out (This feature works by modifying Its sizeDelta) and You can create Pins but when you Zoom In/Out the Pins you’ve created keep their positions so they not longer represent what you original intended (They are already a child of this UI Element, hence the wording of the question but RectTransform don’t work like regular Transforms, please don’t answer by saying “Make it a child”)

I’m having a hard time solving this as It easy as It may seem. Any help it will be greatly appreciated. I need to do this via code.

Also the Canvas is set to Screen Space - Overlay

Attach this script to your pins (assuming they’re children of your UI element):

public class Pin : MonoBehaviour
{
    RectTransform parentRect = null;

    float posPercentX = 0.0f;
    float posPercentY = 0.0f;

    void Start()
    {
        parentRect = transform.parent.GetComponent<RectTransform>();
        Vector2 halfRectSize = parentRect.sizeDelta * 0.5f; 

        posPercentX = Mathf.InverseLerp(-halfRectSize.x, halfRectSize.x, transform.localPosition.x);
        posPercentY = Mathf.InverseLerp(-halfRectSize.y, halfRectSize.y, transform.localPosition.y);
    }

    void Update()
    {
        Vector2 halfRectSize = parentRect.sizeDelta * 0.5f;

        float posX = Mathf.Lerp(-halfRectSize.x, halfRectSize.x, posPercentX);
        float posY = Mathf.Lerp(-halfRectSize.y, halfRectSize.y, posPercentY); 

        transform.localPosition = new Vector2(posX, posY);
    }
}

When a pin is first placed (in this case I’ve put this in the Start method), the code determines where the pin is positioned as a percentage relative to the bounds of the parent rect at its current size. This results in an X and Y coordinate that sits between 0 and 1 (similar to what UV coordinates are for a texture map basically).

In Update, these percentage values are then used to get back the correct X and Y position of the pin by lerping them against the new size of the parent rect.