I need anchor a ui element to a gameobject like bellow. There was a feature like this on NGUI.
I made a workaround here is the code.
public static Vector3 GetScreenPosition(Transform transform,Canvas canvas,Camera cam)
{
Vector3 pos;
float width = canvas.GetComponent<RectTransform>().sizeDelta.x;
float height = canvas.GetComponent<RectTransform>().sizeDelta.y;
float x = Camera.main.WorldToScreenPoint(transform.position).x / Screen.width;
float y = Camera.main.WorldToScreenPoint(transform.position).y / Screen.height;
pos = new Vector3(width * x - width / 2, y * height - height / 2);
return pos;
}
Use a world space canvas that is a child of the GameObject.