unity 保存圖片,保存截圖。

直接上代碼:

                System.DateTime now = System.DateTime.Now;
                string times = now.ToString();
                times = times.Trim();
                times = times.Replace("/", "-");
                times = times.Replace(":", "-");
                string filename = "Screenshot" + times + ".png";

                //截取屏幕  
                Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
                texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
                texture.Apply();
                //轉爲字節數組  
                byte[] bytes = texture.EncodeToPNG();

                //判斷是否爲Android平臺  
                if (Application.platform == RuntimePlatform.Android)
                {
                    string destination = "/sdcard/DCIM/Screenshots";
                    //判斷目錄是否存在,不存在則會創建目錄  
                    if (!Directory.Exists(destination))
                    {
                        Directory.CreateDirectory(destination);
                    }
                    string Path_save = destination + "/" + filename;
                    //存圖片  
                    File.WriteAllBytes(Path_save, bytes);
                }
                else
                {
                    string Path_save = Application.persistentDataPath + "/" + filename;
                    Debug.Log(Path_save);
                    File.WriteAllBytes(Path_save, bytes);

                    if (Application.platform == RuntimePlatform.IPhonePlayer)
                    {
                        Framework.iOSPhotoManager.PhotoManager.SaveImageToAlbum(Path_save);
                    }
                }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章