In Unity, how can I take pictures with AR Camera and save images? (android)

Viewed 30

I am developing an AR Camera app (android) using Unity AR Foundation, how can I take pictures with AR Camera and save images?

If you click the button with the code below, I even took a screenshot of the screen, but the "Path" setting doesn't work well. I'd like to have the screenshot PNG file saved in the folder within the Unity project.

public Camera camera;   

private int resWidth;
private int resHeight;
string path;

void Start()
{
    resWidth = Screen.width;
    resHeight = Screen.height;
    path = "../ScreenShot/";
    Debug.Log(path);
}

public void ClickScreenShot()
{
    DirectoryInfo dir = new DirectoryInfo(path);
    if (!dir.Exists)
    {
        Directory.CreateDirectory(path);
    }
    string name;
    name = System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png";
    RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
    camera.targetTexture = rt;
    Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
    Rect rec = new Rect(0, 0, screenShot.width, screenShot.height);
    camera.Render();
    RenderTexture.active = rt;
    screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
    screenShot.Apply();

    byte[] bytes = screenShot.EncodeToPNG();
    File.WriteAllBytes(name, bytes);
}
0 Answers
Related