Created
December 10, 2022 05:06
-
-
Save chengkehan/9325affe6c2a80c3c5ad3324e303c428 to your computer and use it in GitHub Desktop.
Convert ugui anchored position to world position without RectTransform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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