UnityEngine.RectTransform.GetWorldCorners C# (CSharp) Method

GetWorldCorners() public method

Get the corners of the calculated rectangle in world space.

public GetWorldCorners ( Vector3 fourCornersArray ) : void
fourCornersArray Vector3 Array that corners should be filled into.
return void
        public void GetWorldCorners(Vector3[] fourCornersArray)
        {
            if ((fourCornersArray == null) || (fourCornersArray.Length < 4))
            {
                Debug.LogError("Calling GetWorldCorners with an array that is null or has less than 4 elements.");
            }
            else
            {
                this.GetLocalCorners(fourCornersArray);
                Transform transform = base.transform;
                for (int i = 0; i < 4; i++)
                {
                    fourCornersArray[i] = transform.TransformPoint(fourCornersArray[i]);
                }
            }
        }

Usage Example

コード例 #1
0
    public static Rect GetScreenRectFromRectTransform(RectTransform rectTransform)
    {
        Vector3[] corners = new Vector3[4];

        rectTransform.GetWorldCorners(corners);

        float xMin = float.PositiveInfinity;
        float xMax = float.NegativeInfinity;
        float yMin = float.PositiveInfinity;
        float yMax = float.NegativeInfinity;

        for (int i = 0; i < 4; i++)
        {
            // For Canvas mode Screen Space - Overlay there is no Camera; best solution I've found
            // is to use RectTransformUtility.WorldToScreenPoint) with a null camera.
            Vector3 screenCoord = RectTransformUtility.WorldToScreenPoint(null, corners[i]);

            if (screenCoord.x < xMin)
                xMin = screenCoord.x;
            if (screenCoord.x > xMax)
                xMax = screenCoord.x;
            if (screenCoord.y < yMin)
                yMin = screenCoord.y;
            if (screenCoord.y > yMax)
                yMax = screenCoord.y;
        }
        Rect result = new Rect(xMin, Screen.height - yMax, xMax - xMin, yMax - yMin);
        return result;
    }
All Usage Examples Of UnityEngine.RectTransform::GetWorldCorners