找回密码
 立即注册
查看: 237|回复: 0

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

[复制链接]
发表于 2023-1-3 19:17 | 显示全部楼层 |阅读模式
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开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-16 02:44 , Processed in 0.117394 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表