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)?
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();
}
}
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.