Convert RectTransform.rect to Rect world
I want to turn a RectTransform.rect into a world space Rect object. RectTransform doesn't appear to have a method to convert the rectangle object to world coordinates. But I want something like the following.
myRectTransform.ConvertToWorldRect(); 
Answer by Ash-Blue · Nov 19, 2015 at 05:29 AM
Looks like you can chain together a few Unity API methods for some clever conversion. Example below includes support for modifying the Rect's size based upon the current Canvas scale.
 using UnityEngine;
 using System.Collections;
 
 static public class RectTransformExt {
     /// <summary>
     /// Converts RectTransform.rect's local coordinates to world space
     /// Usage example RectTransformExt.GetWorldRect(myRect, Vector2.one);
     /// </summary>
     /// <returns>The world rect.</returns>
     /// <param name="rt">RectangleTransform we want to convert to world coordinates.</param>
     /// <param name="scale">Optional scale pulled from the CanvasScaler. Default to using Vector2.one.</param>
     static public Rect GetWorldRect (RectTransform rt, Vector2 scale) {
         // Convert the rectangle to world corners and grab the top left
         Vector3[] corners = new Vector3[4];
         rt.GetWorldCorners(corners);
         Vector3 topLeft = corners[0];
 
         // Rescale the size appropriately based on the current Canvas scale
         Vector2 scaledSize = new Vector2(scale.x * rt.rect.size.x, scale.y * rt.rect.size.y);
 
         return new Rect(topLeft, scaledSize);
     }
 }
 
corners[0] is bottom Left corner, not top left. link text
Answer by Bodix_ · Mar 22, 2021 at 11:50 AM
More convenient solution:
 public static class RectTransformExtensions
 {
     public static Rect GetWorldRect(this RectTransform rectTransform)
     {
         Vector3[] corners = new Vector3[4];
         rectTransform.GetWorldCorners(corners);
         // Get the bottom left corner.
         Vector3 position = corners[0];
         
         Vector2 size = new Vector2(
             rectTransform.lossyScale.x * rectTransform.rect.size.x,
             rectTransform.lossyScale.y * rectTransform.rect.size.y);
 
         return new Rect(position, size);
     }
 }
Answer by andrew_pearce · Feb 23 at 07:08 AM
Thanks for the tip! Why we cannot use top right and bottom left corner to calculate the rect size?
public static class RectTransformExtensions {
     public static Rect GetWorldRect(this RectTransform rectTransform) {
        Vector3[] corners = new Vector3[4];
        rectTransform.GetWorldCorners(corners);
        return new Rect(corners[0], corners[2] - corners[0]);
     }
 }Seems to be working fine.
Your answer
 
 
             Follow this Question
Related Questions
Problem with using Rect.set(), it does nothing. 2 Answers
Place GameObject's rect 0 Answers
RectTransform Left Right Bottom Top 0 Answers
Trouble getting created sprite to set. 0 Answers
UI 5.2.1p4 rect empty 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                