美文网首页
Unity 获取相机画面将其保存成图片

Unity 获取相机画面将其保存成图片

作者: 114105lijia | 来源:发表于2023-01-03 15:52 被阅读0次
void CameraCapture(Camera m_Camera, string filename)
    {
        int width = Screen.width;
        int height = Screen.height;

        RenderTexture rt = new RenderTexture(width, height, 16);
        m_Camera.targetTexture = rt;
        m_Camera.Render();
        RenderTexture.active = rt;

        Texture2D t = new Texture2D(width, height);
        t.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        t.Apply();

        string path = CommonTool.Path(filename);
        System.IO.File.WriteAllBytes(path, t.EncodeToJPG());
        m_Camera.targetTexture = null;
    }

public static string Path(string fileName) {

#if UNITY_EDITOR

    return Application.dataPath + "/StreamingAssets/" + fileName;

#elif UNITY_IPHONE
 
    return Application.dataPath+"/Ray/" + fileName;
 
#elif UNITY_ANDROID
 
    return "jar:file://" + Application.dataPath + "!/assets/" + fileName;

#else
    return Application.dataPath + "/StreamingAssets/" + fileName;
 
#endif

    }

相关文章

网友评论

      本文标题:Unity 获取相机画面将其保存成图片

      本文链接:https://www.haomeiwen.com/subject/extkcdtx.html