补充文件夹相关
Editor
打包是不会将Editor文件打包出去
Resources
-
-
Resources资源在App中,是存储在resources.assets、resources.assets.resS、sharedassets0.assets、sharedassets0.assets.resS文件中
-
有.assets和.assets.resS两个后缀文件。同名文件的.assets存储的是序列化文件,类似于Prefab,存储着资源的所有序列化信息,以及对二进制数据源的引用。而.assets.resS里面存储的是二进制数据源,如图片、音效等,是.assets里面所引用的实际的图片、音效等数据。
-
SteammingAsset
只能有一个且位于Assets下,一般放置二进制配置文件,打包时不会被加密压缩,一般用于存储二进制文件
序列化和反序列化
序列化类对象添加特性:[System.Serializable],字段中的类也需要添加特性
内存流对象:
类对象:MemoryStream
命名空间:System.IO
2进制格式化对象:
类对象:BinaryFormatter
命名空间:System.Runtime.Serialization.Formatters.Binary
序列化
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, animal);
byte[] aniBytes = ms.GetBuffer();
File.WriteAllBytes(path, aniBytes);
ms.Close();
}
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, animal);
fs.Flush();
fs.Close();
}
反序列化
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read))
{
BinaryFormatter bf = new BinaryFormatter();
Animal an = bf.Deserialize(fs) as Animal;
fs.Close();
}
using (MemoryStream ms = new MemoryStream(bytes))
{
BinaryFormatter bf = new BinaryFormatter();
Animal an = bf.Deserialize(ms) as Animal;
ms.Close();
}
使用Excel表
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read))
{
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(fs);
DataSet result = excelReader.AsDataSet();
for (int i = 0; i < result.Tables.Count; i++)
{
string tableName = result.Tables[i].TableName;
int rowCount = result.Tables[i].Rows.Count;
int colCount = result.Tables[i].Columns.Count;
DataTable dataTable = result.Tables[i];
DataRow dr = dataTable.Rows[0];
object obj1 = dr[0];
object obj2 = dataTable.Rows[0][1];
}
fs.Close();
}
二进制数据持久化
转换为字节数据
int age = 10;
byte[] ageByte = BitConverter.GetBytes(age);
age = BitConverter.ToInt32(ageByte, 0);
byte[] strByte = Encoding.UTF8.GetBytes("111111");
string str = Encoding.UTF8.GetString(strByte);
文件操作
类名:File 命名空间:System.IO
if (File.Exists(Application.dataPath + "/DataPersistence/Teach1.cs"))
{
Debug.Log("存在");
}
FileStream gfs = File.Create(Application.dataPath + "/DataPersistence/Teach2.txt");
File.WriteAllBytes(Application.dataPath + "/DataPersistence/Teach2.txt", BitConverter.GetBytes(1024));
string[] strArray = new string[] { "一", "二", "三" };
File.WriteAllLines(Application.dataPath + "/DataPersistence/Teach2.txt", strArray);
File.WriteAllText(Application.dataPath + "/DataPersistence/Teach2.txt", "面包");
Byte[] bytes = File.ReadAllBytes(Application.dataPath + "/DataPersistence/Teach2.txt");
strArray = File.ReadAllLines(Application.dataPath + "/DataPersistence/Teach2.txt");
string tStr = File.ReadAllText(Application.dataPath + "/DataPersistence/Teach2.txt");
File.Delete(Application.dataPath + "/DataPersistence/Teach2.txt");
File.Open( Application.dataPath + "/DataPersistence/Teach2.txt", FileMode.OpenOrCreate, FileAccess.Write);
类名:FileStream 命名空间:System.IO
基本操作:
FileStream fs = new FileStream("teach1.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
FileStream gfs = File.Create(Application.dataPath + "/DataPersistence/Teach2.txt");
File.Open( Application.dataPath + "/DataPersistence/Teach2.txt", FileMode.OpenOrCreate,
FileAccess.Write);
long fsLength = fs.Length;
bool canRead = fs.CanRead;
bool canWrite = fs.CanWrite;
fs.Flush();
fs.Close();
fs.Dispose();
写入字节:
FileStream fs1 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
Byte[] data1 = BitConverter.GetBytes(100);
Byte[] data2 = BitConverter.GetBytes(true);
fs1.Write(data1, 0, 4);
fs1.Write(data2, 0, data2.Length);
Byte[] data3 = Encoding.UTF8.GetBytes("Hello World");
int strLength = data3.Length;
fs1.Write(BitConverter.GetBytes(strLength), 0, sizeof(int));
fs1.Write(data3, 0, data3.Length);
fs1.Flush();
fs1.Dispose();
读取字节
FileStream fs2 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
Byte[] byte1 = new byte[4];
int streamIndex = fs2.Read(byte1, 0, 4);
int data1 = BitConverter.ToInt32(byte1);
Byte[] byte2 = new byte[4];
fs2.Read(byte2, 0, 4);
int strLength = BitConverter.ToInt32(byte2);
Byte[] strBytes = new Byte[strLength];
fs2.Read(strBytes, 0, strLength);
string str = Encoding.UTF8.GetString(strBytes);
byte[] data = new byte[fs2.Length];
fs2.Read(data, 0, data.Length);
fs2.Dispose();
安全关闭
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
}
文件夹操作
操作文件夹
if (Directory.Exists(path))
{
Debug.Log("存在文件夹");
}
DirectoryInfo df = Directory.CreateDirectory(path);
Directory.Delete(path, true);
string[] allDirec = Directory.GetDirectories(path);
string[] allFiles = Directory.GetFiles(path);
Directory.Move(Application.dataPath + "/TestFile", path);
目录信息和文件信息
类名:DirectoryInfo
DirectoryInfo df = Directory.CreateDirectory(path);
string direcPath = df.FullName;
string direcName = df.Name;
DirectoryInfo dfParent = Directory.GetParent(path);
DirectoryInfo[] sonDf = df.GetDirectories();
类名:FileInfo
DirectoryInfo df = Directory.CreateDirectory(path);
string direcPath = df.FullName;
string direcName = df.Name;
DirectoryInfo dfParent = Directory.GetParent(path);
DirectoryInfo[] sonDf = df.GetDirectories();
FileInfo[] fi = df.GetFiles();
foreach (FileInfo fileInfo in fi)
{
string fileName = fileInfo.Name;
string filePath = fileInfo.FullName;
long fileLength = fileInfo.Length;
string fileExtension = fileInfo.Extension;
}