相对于渲染纹理的矩形

Rect relative to Render Texture

我很难弄清楚如何找到代表 ui 元素区域的正确矩形。我得到 Ui 元素 RectTransform。我试图直接使用它,使用一个函数和 RectTransformUtility,但似乎没有任何效果。 这是代码。

    RenderTexture.active = scr;

    //adjust rect position
    //Rect rect = RectUtils.RectTransformToScreenSpace (rectTransform);
    int width = System.Convert.ToInt32 (rect.width);
    int height = System.Convert.ToInt32 (rect.height);

    // Create a texture the size of the screen, RGB24 format
    Texture2D tex = new Texture2D(width, height, TextureFormat.RGBA32, false);

    RenderTexture.active = scr;
    // Read screen contents into the texture
    tex.ReadPixels(rect, 0, 0);

    tex.Apply();

    RenderTexture.active = null;

    // Encode texture into PNG
    byte[] bytes = tex.EncodeToPNG();
    //todo destroy the texture
    Object.Destroy (tex);
    // For testing purposes, also write to a file in the project folder
    File.WriteAllBytes(path, bytes);

我尝试以不同的方式创建矩形,例如:

方法一:

    Vector2 size = Vector2.Scale(transform.rect.size, transform.lossyScale);
    Rect rect = new Rect(transform.position.x, Screen.height - transform.position.y, size.x, size.y);
    rect.x -= (transform.pivot.x * size.x);
    rect.y -= ((1.0f - transform.pivot.y) * size.y);
    return rect;

方法二:

    Vector2 temp = rectT.transform.position;
    var startX = temp.x - width/2;
    var startY = temp.y - height/2;

    var tex = new Texture2D (width, height, TextureFormat.RGB24, false);
    tex.ReadPixels (new Rect(startX, startY, width, height), 0, 0);

我使用的是 Unity 5.5 Mac 版本。

当我传递给 ReadPixels new Rect (0, 0, Screen.width, Screen.height) 时,我在其定义的尺寸 1920x1080

中看到了整个 RenderTexture

我想将它用作 UI 元素的面包师,因为我遇到了性能问题,并且肯定没有其他解决方案来实现 required 行为。

我也被这个问题困扰过。这就是我解决它的方法。 首先,我们需要 RectTransform 在屏幕中的位置,为此我使用了 RectTransformUtility。然后因为rect x,y是左下角的起点(注:在GUI中x,y是左上角)我们分别减去宽度和高度的一半。

        Vector2 rectPos = RectTransformUtility.WorldToScreenPoint (Camera.main, rectTransform.position);

        float startX = rectPos.x - width / 2;
        float startY = rectPos.y - height / 2;
        Rect screenRect = new Rect (startX, startY, width, height);

注意:width height 必须是整数。

编辑:当我们使用屏幕覆盖并将分辨率从标准更改为任何其他分辨率时,上述解决方案不起作用。即使我使用了 lossyscale,我也无法获得一致的行为。 RecTransformUtility 的一种方法对这种情况很有帮助 (WorldToScreenPoint)。

    Vector3[] bounds = new Vector3[4];

    rectTransform.GetWorldCorners (bounds);

    Vector2 rectPos = RectTransformUtility.WorldToScreenPoint (camera, rectTransform.position);

    Vector2 minPosition = RectTransformUtility.WorldToScreenPoint (camera, bounds[0]);
    Vector2 maxPosition = RectTransformUtility.WorldToScreenPoint (camera, bounds[2]);

    Vector2 size = maxPosition - minPosition;

    float startX = rectPos.x - size.x / 2;
    float startY = rectPos.y - size.y / 2;

    return new Rect (startX, startY,  size.x, size.y);