Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How can i add a rect bottom offset

Discussion in 'Scripting' started by melonhead, Jan 30, 2020.

  1. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    630
    i have the following code from my screenshot script

    Code (CSharp):
    1. RenderTexture renderTexture = myCamera.targetTexture;
    2.    
    3.                 Texture2D renderResult = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
    4.  
    5.  
    6.                 renderResult.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    7.                 renderResult.Apply();
    8.                 byte[] byteArray = renderResult.EncodeToPNG();
    I need to remove a couple of pixel rows from the bottom of the picture before it is saved as they always come out just black on the bottom of the saved picture, if i add a value to the 0 in the read pixels line it chops off the top but cannot work out how to remove the bottom of the picture so how do i add an offset to the bottom of the rect

    any help very useful

    thank you
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Try doing something like:
    Code (CSharp):
    1. renderTexture.height - *your_offset*
    for the RenderTexture2D constructor and ReadPixels.

    It should reduce area read / written from the bottom in theory
     
  3. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    You can also use percentages of the screen without having to resort to fixed variables (which cause trouble with difference resoltuons/ window size). ;)
     
  4. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    630
    i have already tried renderTexture.height - *your_offset* but it still removes from the top down not the bottom upwards,
     
    Last edited: Jan 30, 2020
  5. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    630
    just tested a build and the 2 bottom dark rows are not there when screenshot is taken so only happens in editor play mode for some reason, anyone know why this is or is it for some reason capturing the 2 top rows of the console that is directly under the game view window