kyuskoj 发表于 2023-1-3 19:17

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

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    }
页: [1]
查看完整版本: Unity 获取相机画面将其保存成图片