Skip to content

Instantly share code, notes, and snippets.

@xwipeoutx
Last active October 1, 2024 04:57
Show Gist options
  • Save xwipeoutx/4221404088a74b9975a35040819c5861 to your computer and use it in GitHub Desktop.
Save xwipeoutx/4221404088a74b9975a35040819c5861 to your computer and use it in GitHub Desktop.
Find the rectangle a rect transform will use on a SceneView. Good for labels and stuff.
var sceneView = SceneView.currentDrawingSceneView;
var canvas = rectTransform.GetComponentInParent<Canvas>();
var zoomFactor = sceneView.position.height / (sceneView.camera.orthographicSize * 2);
var pivotPosition = HandleUtility.WorldToGUIPoint(rectTransform.position);
var sizeOnCanvas = RectTransformUtility.PixelAdjustRect(rectTransform, canvas).size;
var size = sizeOnCanvas * zoomFactor + buffer * Vector2.one;
var pos = pivotPosition - size * new Vector2(rectTransform.pivot.x, 1 - rectTransform.pivot.y);
var sceneViewRect = new Rect(pos, size);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment