标签:
1.string 格式化输出
参考 http://www.cnblogs.com/albert-struggle/archive/2012/05/22/2512744.html
2.文件读写
struct FileFormat
{
public string prefix;
public string suffix;
public int numberLength;
public int startNumber;
public int endNumber;
}
public class FileReader{
StreamReader sr;
string txtFileName = "picture list.txt";
string texturePath;
public int textureLength { get; private set; }
private List<FileFormat> _textureInfo;
// Use this for initialization
public void Start ()
{
// initial variable
_textureInfo = new List<FileFormat>();
// open exist .txt file
if (File.Exists(txtFileName) && txtFileName != null)
{
sr = new StreamReader("picture list.txt");
DecodeTxtFile();
}
}
/// <summary>
/// decode the txt file 1.前缀 2.后缀 3.内容长度 4.开始数字 5.结束数字
/// </summary>
///
private void DecodeTxtFile()
{
while (true)
{
string sLine = sr.ReadLine();
if (sLine != null)
{
// the first line is the path
if (texturePath == null)
{
texturePath = sLine;
}
else
{
char[] delimiters = { ‘ ‘ };
string[] sLineParts = sLine.Split(delimiters);
if (sLineParts.Length >= 5)
{
FileFormat ff = new FileFormat();
ff.prefix = sLineParts[0];
ff.suffix = sLineParts[1];
int.TryParse(sLineParts[2], out ff.numberLength);
int.TryParse(sLineParts[3], out ff.startNumber);
int.TryParse(sLineParts[4], out ff.endNumber);
_textureInfo.Add(ff);
}
}
}
else
break;
}
textureLength = _textureInfo.Count;
}
/// <summary>
/// get the texture length by the texture infor index
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public int GetLengthByIndex(int index)
{
if (index <= textureLength && index >= 0)
{
int lengthResult = _textureInfo[index].endNumber - _textureInfo[index].startNumber;
return lengthResult;
}
return 0;
}
/// <summary>
/// get the string name of the texture file by index and texture number
/// </summary>
/// <param name="index"></param>
/// <param name="textureNumber"></param>
/// <returns></returns>
public string GetFileNameByIndex(int index, int textureNumber, bool attachedPath = false)
{
string sResult = "";
if (textureNumber <= _textureInfo[index].endNumber - _textureInfo[index].startNumber)
{
if(!attachedPath)
sResult = _textureInfo[index].prefix + string.Format("{0:D5}", _textureInfo[index].startNumber + textureNumber) + _textureInfo[index].suffix;
else
sResult = texturePath + "\\" + _textureInfo[index].prefix + string.Format("{0:D5}", _textureInfo[index].startNumber + textureNumber) + _textureInfo[index].suffix;
return sResult;
}
return sResult;
}
}
3.读取硬盘中的image,转换为unity的texture2d
private FileInfo fileInfo;
private byte[] imageData;
/// <summary>
/// decoder the image according to the filepath
/// </summary>
/// <param name="filePath"></param>
public void Decoder(string filePath, out byte[] outImage)
{
if (filePath == null)
{
outImage = imageData;
return;
}
fileInfo = new FileInfo(filePath);
imageData = new byte[fileInfo.Length];
using (FileStream fs = fileInfo.OpenRead())
{
fs.Read(imageData, 0, imageData.Length);
outImage = imageData;
}
}
// texture to display the image
private Texture2D tex;
Decoder(fileName, out imageData);
tex.LoadImage(imageData);
标签:
原文地址:http://www.cnblogs.com/xiaoxiaoyang/p/5100628.html