////////////////////////////////////////////
Unityで外部画像ファイルの読み出し
////////////////////////////////////////////
画像の表示は、
ReadImgFileObjという名前の Planeオブジェクトを作り
-----------------------------------
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;
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()
{
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
}
void OnGUI()
{
GUI.TextArea(new Rect(300, 300, 100, 40), ErrorMessage);
}
}
-----------------------------------
な感じでできたのですが
書き込みが・・・・
結構難したかった。
- 関連記事
-
YouTubeで動画を定期的に出しています。ご視聴いただけると嬉しいです。
Reon Labo
- 2017/03/22(水) 22:47:49|
- Unity
-
| トラックバック:0
-
| コメント:0