【问题标题】:Rect relative to Render Texture相对于渲染纹理的矩形
【发布时间】:2017-05-22 18:49:17
【问题描述】:

我很难弄清楚如何找到代表 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) 时,我看到整个 RenderTexture 的定义尺寸为 1920x1080

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

【问题讨论】:

    标签: unity3d unity5


    【解决方案1】:

    我也遇到过这个问题。这是我解决它的方法。 首先我们需要 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);
    

    注意:宽高应该是整数。

    编辑:当我们使用屏幕覆盖并将分辨率从标准更改为任何其他时,上述解决方案不起作用。即使我使用有损尺度,我也无法获得一致的行为。 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);
    

    【讨论】:

      猜你喜欢
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-11
      • 2013-01-31
      • 2013-01-25
      • 1970-01-01
      相关资源
      最近更新 更多