Skip to content

Instantly share code, notes, and snippets.

@chengkehan
Created December 10, 2022 05:06
Show Gist options
  • Save chengkehan/9325affe6c2a80c3c5ad3324e303c428 to your computer and use it in GitHub Desktop.
Save chengkehan/9325affe6c2a80c3c5ad3324e303c428 to your computer and use it in GitHub Desktop.
Convert ugui anchored position to world position without RectTransform
private Vector3 AnchoredPositionToWorldPosition(Vector2 anchoredPosition)
{
Vector2 canvasSize = canvasRectTransform.sizeDelta;
Vector2 viewportPosition = new Vector2(
(anchoredPosition.x + (canvasSize.x * 0.5f)) / canvasSize.x,
(anchoredPosition.y + (canvasSize.y * 0.5f)) / canvasSize.y
);
Vector3 worldPosition = CameraManager.GetInstance().GetUICamera().ViewportToWorldPoint(viewportPosition);
worldPosition.z = canvasRectTransform.position.z;
return worldPosition;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment