我們在一些項目中可能需要截圖保存功能(特別是AR的一些項目),將截下來(lái)的圖保存的相冊中,從而實(shí)現分享功能。下面就Android和IOS說(shuō)一下他們是如何將圖片保存到本地圖冊的。
關(guān)于安卓端,保存到相冊方法很簡(jiǎn)單,就是也路徑的問(wèn)題,具體方法如下:
public class takephoto : MonoBehaviour{ private int i = 0; //UI public GameObject[] btn; //存儲路徑 private string Path_save; //讀取路徑 private string Path_read; private string filepath; private string destination; void Start() { filepath = Application.persistentDataPath + "/test.txt"; } void OnClick() { StartCoroutine(getTexture2d()); } IEnumerator getTexture2d() { //隱藏UI for (int j = 0; j < btn.Length; j++) { btn[j].GetComponentInChildren<UISprite>().enabled = false; } //截圖操作 yield return new WaitForEndOfFrame(); Texture2D t = new Texture2D(Screen.width, Screen.height,TextureFormat.RGB24,false); //顯示UI for (int j = 0; j < btn.Length; j++) { btn[j].GetComponentInChildren<UISprite>().enabled = true; } t.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true); byte[] bytes = t.EncodeToPNG(); t.Compress(true); t.Apply(); //獲取系統時(shí)間 System.DateTime now = new System.DateTime(); now = System.DateTime.Now; string filename = string.Format("image{0}{1}{2}{3}.png", now.Day, now.Hour, now.Minute, now.Second); //記錄每一個(gè)截圖名字 StreamWriter sw; FileInfo ft = new FileInfo(filepath); if (!ft.Exists) { sw = ft.CreateText(); } else { sw = ft.AppendText(); } sw.WriteLine(filename); sw.Close(); sw.Dispose(); //應用平臺判斷,路徑選擇 if (Application.platform == RuntimePlatform.Android) { string origin = Path_save; destination = "/mnt/sdcard/DCIM/ARphoto"; if (!Directory.Exists(destination)) { Directory.CreateDirectory(destination); } destination = destination + "/" + filename; Path_save = destination; } //保存文件 File.WriteAllBytes(Path_save, bytes); }}其中主要的就是安卓相冊的一個(gè)路徑問(wèn)題,我是在相冊路徑下,新建了一個(gè)名叫ARphoto相冊,所有保存下來(lái)的圖片都保存在這個(gè)相冊中,可以再圖庫應用中查看到這個(gè)相冊文件夾。
在IOS端保存相冊是無(wú)法單獨在Unity中完成的。需要調用Xcode中的方法,關(guān)于Unity與Xcode之間的交互有什么疑問(wèn)的可以查看我之前寫(xiě)的 博文http://blog.csdn.net/hasion/article/details/43668229
其具體實(shí)現方法如下:
public class TakePhoto : MonoBehaviour{[DllImport("__Internal")]private static extern void _SavePhoto (string readaddr);private string path_save;private string path_read;public GameObject _demoBtns;void OnClick (){this.GetComponentInChildren<UITexture> ().enabled = false;_demoBtns.SetActive (false);System.DateTime now = System.DateTime.Now;string filename = string.Format ("image{0}{1}{2}{3}{4}{5}.png", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);path_save = filename;path_read = Application.persistentDataPath + "/" + path_save;//set photomanager.csif (!string.IsNullOrEmpty (path_save)) {PhotosManager.GetInstance ().PhotoPath = path_read;}StartCoroutine (SavePhoto ());}IEnumerator SavePhoto (){yield return new WaitForSeconds (0.2f);Application.CaptureScreenshot (path_save);yield return new WaitForSeconds (0.5f);this.GetComponentInChildren<UITexture> ().enabled = true;_demoBtns.SetActive (true);yield return new WaitForSeconds (1.2f);_SavePhoto (path_read);}}Xcode中執行文件如下:
#import "PhotoManager.h"@implementation PhotoManager- ( void ) imageSaved: ( UIImage *) image didFinishSavingWithError:( NSError *)error contextInfo: ( void *) contextInfo{ if (error != nil) { NSLog(@"有錯誤"); } else { NSLog(@"保存結束"); }}void _SavePhoto(char *readAddr){ NSString *strReadAddr = [NSString stringWithUTF8String:readAddr]; UIImage *img = [UIImage imageWithContentsOfFile:strReadAddr]; NSLog(@"%@",[NSString stringWithFormat:@"w:%f, h:%f", img.size.width, img.size.height]); NSLog(@"%@",[NSString stringWithFormat:@"%s",readAddr ]); PhotoManager *instance = [PhotoManager alloc]; UIImageWriteToSavedPhotosAlbum(img, instance, @selector(imageSaved:didFinishSavingWithError:contextInfo:), nil);}@end大體執行流程就是這樣的,有什么錯誤或者建議的地方,歡迎大家指正,謝謝?。?!
關(guān)于Unity中的Update、Lateupdate和FixedUpdate。
MonoBehaviour.Update更新當MonoBehaviour啟用時(shí),其Update在每一幀被調用。MonoBehaviour.FixedUpdate固定更新當MonoBehaviour啟用時(shí),其FixedUpdate在每一幀被調用。處理Rigi
Unity3D時(shí)間順序與功能
Unity3D中所有控制腳本的基類(lèi)MonoBehaviour有一些虛函數用于繪制中事件的回調,也可以直接理解為事件函數,例如大家都很清楚的Start,Update等函數,以下做
Unity+Mono斷點(diǎn)調試步驟
項目制作過(guò)程中,我們可能要用到斷點(diǎn)調試來(lái)檢測程序運行過(guò)程中的實(shí)時(shí)數據,從而檢測出程序出錯的位置。具體步驟如下:1、Mono必須是Unity安裝時(shí)自
聯(lián)系客服