Unity 截圖功能(安卓、IOS)
阿新 • • 發佈:2019-01-04
我們在一些專案中可能需要截圖儲存功能(特別是AR的一些專案),將截下來的圖儲存的相簿中,從而實現分享功能。下面就Android和IOS說一下他們是如何將圖片儲存到本地圖冊的。
關於安卓端,儲存到相簿方法很簡單,就是也路徑的問題,具體方法如下:
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();
//獲取系統時間
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);
//記錄每一個截圖名字
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);
}
}
其中主要的就是安卓相簿的一個路徑問題,我是在相簿路徑下,新建了一個名叫ARphoto相簿,所有儲存下來的圖片都儲存在這個相簿中,可以再相簿應用中檢視到這個相簿資料夾。
在IOS端儲存相簿是無法單獨在Unity中完成的。需要呼叫Xcode中的方法,關於Unity與Xcode之間的互動有什麼疑問的可以檢視我之前寫的 博文http://blog.csdn.net/hasion/article/details/43668229
其具體實現方法如下:
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.cs
if (!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
大體執行流程就是這樣的,有什麼錯誤或者建議的地方,歡迎大家指正,謝謝!!!