Child's anchoredPosition to be set based on Canvas (Vector2)

Scenario

I have a position Vector2 targetPosition based on the root Canvas of UnityEngine.UI

Now I want to move a UI Element RectTransform ShowButton within that Canvas to that targetPosition. [RectTransform](https://docs.unity3d.com/ScriptReference/RectTransform.html).anchoredPosition

Issue

This ShowButton is a child of a RectTransform aligned at center & bottom. Hence the vector2 value of ShowButton.anchoredPosition is relative to that.

Request

Now if I move my anchoredPosition of ShowButton, it moves relatively.

  • How can I convert targetPosition to a relative position of ShowButton (or)
  • How can I move ShowButton on a global scale (Vector2)?

Thank you,
Karsnen

Canvas Render mode is Unity - Scripting API: RenderMode.ScreenSpaceOverlay

SOLUTION
SPOILER: a sort of a hack!

Steps

  • Create an empty gameobject (say “locationCalculator”) with RectTransform as it’s component
  • Have that gameobject rest under the root canvas
  • Once you have position (Vector2) relative to the root canvas, set the parent locationCalculator to the RectTransform to which you want to calculate. Also use ```
    Transform.SetParent (Transform, bool)
- Capture the anchoredPosition of locationCalculator. This becomes your target position.
- Reset locationCalculator position & parent to normal values (check code)

**Creation of the Gameobject** 

```csharp
   [SerializeField]
        internal RectTransform MyCanvasTransform;
        private RectTransform locationCalculator;

        private void Awake()
        {
            if(locationCalculator == null)
            {
                GameObject _go = new GameObject("locationCalculator");

                locationCalculator = _go.AddComponent<RectTransform>();
                locationCalculator.SetParent(MyCanvasTransform);
                _go.transform.localScale = Vector3.one;
                _go.transform.rotation = Quaternion.identity;
                _go.transform.position = Vector3.zero;
                _go.transform.SetAsFirstSibling();
            }
        }

Getting the target positional values

        internal Vector2 GetLocal(Vector2 _globalPosition, RectTransform _referenceTransform)
        {
            Vector2 _positionBuffer = _globalPosition;
            locationCalculator.anchoredPosition = _positionBuffer;
            locationCalculator.SetParent(_referenceTransform, true);

            _positionBuffer = locationCalculator.anchoredPosition;
            locationCalculator.SetParent(MyCanvasTransform);
            locationCalculator.anchoredPosition = Vector2.zero;
            locationCalculator.SetAsFirstSibling();
            _referenceTransform = null;

            return _positionBuffer;
        }

NOTE
PLEASE note the above solution does not work when & if the RectTransform for which you are figuring position has a size delta of Vector2.zero. It would happen if you stretch to fit.

Thank you & would like to also any other possible direct solutions.