unity3D项目中很多需要配置文件,对于一些简单的配置,我们可以用text文件来保存我们的配置数据。下面是我的解析代码:
using System;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
public class LoadText
{
public static string textPath = Application.streamingAssetsPath + "/Parama.txt";
public static string[] LoadText1()
{
byte[] dataBytes=new byte[100];
string[] dataList=new string[19];
try
{
FileStream file = new FileStream(textPath, FileMode.Open);
file.Seek(0, SeekOrigin.Begin);
file.Read(dataBytes, 0, 100);
string s = Encoding.Default.GetString(dataBytes);
dataList=s.Split(',');
file.Close();
return dataList;
}
catch (Exception)
{
throw;
}
}
}
c#的流形式用完一定要关闭。否则会报错。
还有一个做测试用的,我们把输出信息保存到文本里可以看到程序运行情况。上代码:
public void Save(string information)
{
Debug.Log("Save");
string path = Application.streamingAssetsPath + @"/TestText.txt";
FileStream aFile;
if (!File.Exists(path))
{
aFile = new FileStream(path, FileMode.OpenOrCreate);
}
else
{
aFile = new FileStream(path, FileMode.Append);
}
StreamWriter sw = new StreamWriter(aFile);
sw.Write(information);
sw.Close();
sw.Dispose();
}