Search Unity

Screenshot using dots?

Discussion in 'Entity Component System' started by Aiursrage2k, Aug 6, 2019.

  1. Aiursrage2k

    Aiursrage2k

    Joined:
    Nov 1, 2009
    Posts:
    4,835
    Is there anyway to do screenshots using dots? taking a screenshot is a slow operation imagine if this could be broken up into many core (so assuming you 8 cores its done 1/8th of the time)? Most of these operations can only be done on the mainthread

    int width = Screen.width;
    int height = Screen.height;
    Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

    // Read screen contents into the texture
    tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    tex.Apply();

    // Encode texture into PNG
    byte[] bytes = tex.EncodeToPNG();
    Destroy(tex);
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,270
    First, use async readback to prevent the main thread and GPU stall instead of tex.ReadPixels. The async readback API gives you a NativeArray which you can copy into your own NativeArray. Then you can use some 3rd-party library to encode it into a PNG using the BinaryWriter API.

    Unfortunately, that's a lot more work than EncodeToPNG(), but that's the only way to do it until Unity develops DOTS further.