////////////////////////////////////////////
Unityで画像ファイルの書き出し
////////////////////////////////////////////
保存されたのを確認するためには
androidの場合は
/data/data/<アプリのID>/files/
なので、見つけてもいいけど
画面操作で、呼び出せるようにしてみた
ReadImgFile2Obj という名前の planeオブジェクトを作り
画面に並ぶように配置する。
--------- ReadImgFileScr.cs ----------------------
using UnityEngine;
using System.Collections;
using System.IO; //FileStream
#if UNITY_EDITOR
using UnityEditor;
#endif
public class ReadImgFileScr : MonoBehaviour
{
private string imgFile = "myphoto.png";
private string imgSaveFile = "mysave2img.png";
public Texture2D tex;
public Texture2D tex2;
private string path = "";
private string ErrorMessage = "";
void Start()
{
StartCoroutine(LoadImg());
}
byte[] LoadBin(string path)
{
FileStream fs = new FileStream(path, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] buf = br.ReadBytes((int)br.BaseStream.Length);
br.Close();
return buf;
}
IEnumerator LoadImg()
{
//string path = "";
GameObject readImgObj = GameObject.Find("ReadImgFileObj");
#if UNITY_EDITOR
path = Application.streamingAssetsPath + "\\" + imgFile;
Texture2D tex = new Texture2D(0, 0);
tex.LoadImage(LoadBin(path));
readImgObj.GetComponent
().material.mainTexture = tex;
yield return new WaitForSeconds(0f);
#elif UNITY_ANDROID
path = "jar:file://" + Application.dataPath + "!/assets" + "/" + imgFile;
WWW www = new WWW(path);
yield return www;
Texture2D tex = new Texture2D(0, 0);
readImgObj.GetComponent().material.mainTexture = www.texture;
#endif
}
IEnumerator LoadSaveImg()
{
//string path = "";
GameObject writeImgObj = GameObject.Find("ReadImgFile2Obj");
#if UNITY_EDITOR
path = Application.streamingAssetsPath + "\\" + imgSaveFile;
Texture2D tex = new Texture2D(0, 0);
tex.LoadImage(LoadBin(path));
writeImgObj.GetComponent().material.mainTexture = tex;
yield return new WaitForSeconds(0f);
#elif UNITY_ANDROID
//path = "jar:file://" + Application.dataPath + "!/assets" + "/" + imgSaveFile;
//path = Application.persistentDataPath + "/" + imgSaveFile;
//読み込む場所が違うので、読み込み方式が異なる
path = string.Format("{0}/{1}", Application.persistentDataPath , imgSaveFile);
byte[] imageBytes = File.ReadAllBytes(path);
Texture2D tex = new Texture2D(0, 0);
bool isloadbmpSuccess = tex.LoadImage(imageBytes);
writeImgObj.GetComponent().material.mainTexture = tex;
yield return new WaitForSeconds(0f);
#endif
}
IEnumerator SaveImg()
{
GameObject readImgObj = GameObject.Find("ReadImgFileObj");
#if UNITY_EDITOR
//画像の保存
Texture2D tex2 = new Texture2D(0, 0);
tex2 = readImgObj.GetComponent().material.mainTexture as Texture2D;
// 使っているテクスチャをTexture2Dとして使う場合は as Texture2D を使う
byte[] pngData = tex2.EncodeToPNG(); // pngのバイト情報を取得.
string filePath = EditorUtility.SaveFilePanel("Save Texture", "", imgSaveFile, "png");
if (filePath.Length > 0)
{
// pngファイル保存.
File.WriteAllBytes(filePath, pngData);
}
//画像の保存
yield return new WaitForSeconds(0f);
#elif UNITY_ANDROID
tex2 = readImgObj.GetComponent().material.mainTexture as Texture2D;
//path = "jar:file://" + Application.dataPath + "!/assets" + "/" + imgSaveFile;
path = Application.persistentDataPath + "/" + imgSaveFile;
//path = string.Format("{0}/{1}", Application.persistentDataPath , imgSaveFile);
byte[] pngData = tex2.EncodeToPNG(); // pngのバイト情報を取得.
ErrorMessage = "FindFile";
ErrorMessage = "FindFile2";
if( pngData != null)
{
ErrorMessage = "FindFile3";
File.WriteAllBytes(path, pngData);
ErrorMessage = "書き込み";
}else{
ErrorMessage = "Error";
}
yield return new WaitForSeconds(0f);
#endif
}
void OnGUI()
{
if (GUI.Button(new Rect(300, 200, 100, 40), "画像保存"))
{
//StartCoroutine("LoadText2");
//StartCoroutine(SaveImg());//OK
//StartCoroutine(SaveImgNew());//ok
StartCoroutine(SaveImgOld());
}
if (GUI.Button(new Rect(300, 250, 100, 40), "画像確認"))
{
//StartCoroutine("LoadText2");
StartCoroutine(LoadSaveImg());
}
GUI.TextArea(new Rect(300, 300, 100, 40), ErrorMessage);
}
IEnumerator SaveImgOld()
{
GameObject readImgObj = GameObject.Find("ReadImgFileObj");
#if UNITY_EDITOR
//画像の保存
Texture2D tex2 = new Texture2D(0, 0);
tex2 = readImgObj.GetComponent().material.mainTexture as Texture2D;
// 使っているテクスチャをTexture2Dとして使う場合は as Texture2D を使う
byte[] pngData = tex2.EncodeToPNG(); // pngのバイト情報を取得.
string filePath = EditorUtility.SaveFilePanel("Save Texture", "", imgSaveFile, "png");
if (filePath.Length > 0)
{
// pngファイル保存.
File.WriteAllBytes(filePath, pngData);
}
//画像の保存
yield return new WaitForSeconds(0f);
#elif UNITY_ANDROID
//.WriteAllBytes方式
tex2 = readImgObj.GetComponent().material.mainTexture as Texture2D;
//path = "jar:file://" + Application.dataPath + "!/assets" + "/" + imgSaveFile;
//path = Application.persistentDataPath + "/" + imgSaveFile;
path = string.Format("{0}/{1}", Application.persistentDataPath , imgSaveFile);
byte[] pngData = tex2.EncodeToPNG(); // pngのバイト情報を取得.
ErrorMessage = "FindFile2";
if( pngData != null)
{
ErrorMessage = "FindFile3";
File.WriteAllBytes(path, pngData);
ErrorMessage = "書き込み";
}else{
ErrorMessage = "Error";
}
yield return new WaitForSeconds(0f);
#endif
}
IEnumerator SaveImgNew()
{
GameObject readImgObj = GameObject.Find("ReadImgFileObj");
#if UNITY_EDITOR
//画像の保存
Texture2D tex2 = new Texture2D(0, 0);
tex2 = readImgObj.GetComponent().material.mainTexture as Texture2D;
// 使っているテクスチャをTexture2Dとして使う場合は as Texture2D を使う
byte[] pngData = tex2.EncodeToPNG(); // pngのバイト情報を取得.
string filePath = EditorUtility.SaveFilePanel("Save Texture", "", imgSaveFile, "png");
if (filePath.Length > 0)
{
// pngファイル保存.
File.WriteAllBytes(filePath, pngData);
}
//画像の保存
yield return new WaitForSeconds(0f);
#elif UNITY_ANDROID
//.Write 方式
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";
//tex2.ReadPixels (new Rect(0, 0, Screen.width, Screen.height), 0, 0);
using (FileStream BinaryFile = new FileStream(path, FileMode.Create, FileAccess.Write)) {
ErrorMessage = "2";
using (BinaryWriter Writer = new BinaryWriter(BinaryFile)) {
ErrorMessage = "3";
Writer.Write (tex2.EncodeToPNG());
ErrorMessage = "ED";
}
}
yield return new WaitForSeconds(0f);
#endif
}
}
--------- ReadImgFileScr.cs ----------------------
表示させる項目が増えてきたので
すべての void OnGUI(){ の下に
以下の部分を追記する
//解像度に依存せず
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(Screen.width / 800f, Screen.height / 600f, 1f));
- 関連記事
-
YouTubeで動画を定期的に出しています。ご視聴いただけると嬉しいです。
Reon Labo
- 2017/03/29(水) 15:46:48|
- Unity
-
| トラックバック:0
-
| コメント:0