private void CapturePhoto() { //圖片存儲的位置 string rootPath = @"d:\photo-whzq\"; if (!Directory.Exists(rootPath)) { Directory.CreateDirectory(rootPath); } string filename = System.DateTime.Now.ToString("yyyyMMddHHmmssms") + ".png"; capturePicPng(rootPath + filename); string filefullname = rootPath + filename; ; PlayerPrefs.SetString(IMAGEFILE, filefullname); } public void capturePicPng(string _filePath) { FacePhoto.GetComponent<FaceDetection>().capturePicPng(_filePath); } //保存圖片 private IEnumerator getTexture(string _filePath, byte _type) { yield return new WaitForEndOfFrame(); //至關於一個畫布 Texture2D t = new Texture2D(1920, 1080, TextureFormat.RGBA32, false); //至關於在畫布上畫畫 //讀取屏幕的局部像素信息 t.ReadPixels(new UnityEngine.Rect(0, 0, Screen.width, Screen.height), 0, 0, true); t.Apply(); if (0 == _type) { byte[] byt = t.EncodeToJPG(); //將byte[]所有寫入到文件 File.WriteAllBytes(_filePath, byt); } if (1 == _type) { byte[] byt = t.EncodeToPNG(); File.WriteAllBytes(_filePath, byt); } cameraTexture.Play(); } /// 拍照PNG /// /// public void capturePicPng(string _filePath) { ///< 正在錄屏,直接返回 if (/*bIsSeriousCapture ||*/ null == cameraTexture) { return; } if (null != cameraTexture && !cameraTexture.isPlaying) { return; } cameraTexture.Pause();//中止攝像頭 StartCoroutine(getTexture(_filePath, 1)); }