I made ScreenShot function in my app, and actually it works. But in this function, if the screen resolution is large, this inserts a little delay (within one second). So that I tried to use System.Threading.Thread() and call GenerateScreenShot() in thread. But faced below error related to Texture2D.
So are there other solution in unity? My app uses USB camera, and delay isn’t allowed. Please let me know…
void GenerateScreenShot(string filename_) {
int sw = Screen.width;
int sh = Screen.height;
Texture2D tex = new Texture2D(sw, sh, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0.0f, 0.0f, sw, sh), 0, 0);
tex.Apply();
byte[] bytes = tex.EncodeToPNG();
Destroy(tex);
Debug.Log(Application.dataPath);
string url = Application.dataPath.ToString() + filename_;
File.WriteAllBytes(url, bytes);
}
error in thread
UnityEngine.Texture2D:Internal_Create(Texture2D, Int32, Int32, TextureFormat, Boolean)
UnityEngine.Texture2D:.ctor(Int32, Int32, TextureFormat, Boolean) (at C:\BuildAgent\work\842f9557127e852\Runtime\ExportGenerated\Editor\Graphics.cs:506)
MyScript:GenerateScreenShot(String) (at Assets\Scripts\MyScript.cs:102)
MyScript:start() (at Assets\Scripts\MyScript.cs:156)