////////////////////////////////////////////
Unityで画像ファイルの書き出し
////////////////////////////////////////////
とりあえず、
書き込み場所は
androidの場合は
path = Application.persistentDataPath + "/" + imgSaveFile;
若しくは
path = string.Format("{0}/{1}", Application.persistentDataPath , imgSaveFile);
として、保存をすればいいらしい。
現在表示されているTextureをTexture2Dとして、読み込んで
tex2 = readImgObj.GetComponent
().material.mainTexture as Texture2D;
pngのバイト情報を取得して
byte[] pngData = tex2.EncodeToPNG();
保存した。
-----------------------------------
//.WriteAllBytes方式
GameObject readImgObj = GameObject.Find("ReadImgFileObj");
tex2 = readImgObj.GetComponent().material.mainTexture as Texture2D;
path = string.Format("{0}/{1}", Application.persistentDataPath , imgSaveFile);
byte[] pngData = tex2.EncodeToPNG(); // pngのバイト情報を取得.
if( pngData != null){
File.WriteAllBytes(path, pngData);
ErrorMessage = "書き込み";
}else{
ErrorMessage = "Error";
}
-----------------------------------
また、
-----------------------------------
//.Write 方式
GameObject readImgObj = GameObject.Find("ReadImgFileObj");
Texture2D tex2 = new Texture2D(0, 0);
tex2 = readImgObj.GetComponent().material.mainTexture as Texture2D;
path = string.Format("{0}/{1}", Application.persistentDataPath , imgSaveFile);
byte[] pngData = tex2.EncodeToPNG(); // pngのバイト情報を取得.
ErrorMessage = "1";
using (FileStream BinaryFile = new FileStream(path, FileMode.Create, FileAccess.Write)) {
using (BinaryWriter Writer = new BinaryWriter(BinaryFile)) {
Writer.Write (tex2.EncodeToPNG());
}
}
-----------------------------------
でも、保存された。
- 関連記事
-
YouTubeで動画を定期的に出しています。ご視聴いただけると嬉しいです。
Reon Labo
- 2017/03/24(金) 20:14:54|
- Unity
-
| トラックバック:0
-
| コメント:0