////////////////////////////////////////////
Unityでテキストファイルを外部領域へ書き込み
////////////////////////////////////////////
最後に
外部の画像データー表示
次は外部画像ファイルを読み込む。
ReadImgFileObjという名前の
空オブジェクトを作る

Assetsにmyphotoという名前の画像ファイルを入れる

さらに
ReadImgFileScrという、C#Scriptファイルを作る
生身は以下のようにした。

---------ReadImgFileScr.cs---------
using UnityEngine;
using System.Collections;
using System.IO; //FileStream
public class ReadImgFileScr : MonoBehaviour {
private string imgFile = "myphoto.png";
void Start () {
Texture2D tex = new Texture2D(0, 0);
tex.LoadImage(LoadBin(Application.dataPath + "/" + imgFile));
gameObject.GetComponent
().material.mainTexture = tex;
}
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;
}
}
---------ReadImgFileScr.cs---------
このままだと、プレビューに表示されないので
Camera
position(0,10,0) Rotation(90,180,0) scale(1,1,1)
ReadImgFileObj
position(0,0,0) Rotation(0,0,0) scale(1,1,1)
とした。
起動してみると

- 関連記事
-
YouTubeで動画を定期的に出しています。ご視聴いただけると嬉しいです。
Reon Labo
- 2017/02/26(日) 15:23:03|
- Unity
-
| トラックバック:0
-
| コメント:0