配置文件
配置文件
1.TXT
示例
using System.IO;
using UnityEngine;
public class Juse : MonoBehaviour
{
public string configPath;
public string[] configContent;
private void Start()
{
configPath = Path.Combine(Application.streamingAssetsPath, "config.txt");
if (File.Exists(configPath))
{
configContent = File.ReadAllLines(configPath);
}
foreach(string content in configContent)
{
Debug.Log(content);
}
}
}
将Txt文件放入工程的StreamingAssets文件夹中,即可读取。
2.CSV
示例
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class Juse : MonoBehaviour
{
public List<Student> students = new List<Student>();
void Start()
{
//文件位置
string filePath = Application.streamingAssetsPath + "/csvtest.csv";
//判断目录是否存在
if (!Directory.Exists(Application.streamingAssetsPath))
{
//创建目录
Directory.CreateDirectory(Application.streamingAssetsPath);
}
StreamWriter sw = new StreamWriter(filePath);
//向列表中写入数据
Student student1 = new Student(2, "xiaohua");
Student student2 = new Student(3, "xiaohong");
students.Add(student1);
students.Add(student2);
//向文件中写入数据
for (int i = 0; i < students.Count; i++)
{
sw.WriteLine(students[i].id.ToString(), students[i].name);
}
//推送到流文件
sw.Flush();
//关闭文件
sw.Close();
StreamReader sr = new StreamReader(filePath);
//读取文件数据
string str;
while ((str = sr.ReadLine()) != null)
{
Debug.Log(str);
}
//关闭文件
sr.Close();
}
}
[System.Serializable]
public class Student
{
public int id;
public string name;
public Student(int id, string name)
{
this.id = id;
this.name = name;
}
}
3.Xml
示例
using System;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;
public class Juse : MonoBehaviour
{
private List<Skill> skillList = new List<Skill>();
private void Start()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Application.streamingAssetsPath+"/Skill.xml");
XmlNode root =xmlDoc.ChildNodes[1];
foreach (XmlNode nodes in root)
{
Skill skill = new Skill();
foreach(XmlNode node in nodes)
{
if(node.Name == "id")
{
skill.id = Int32.Parse(node.InnerText);
}
if (node.Name == "name")
{
skill.name = node.InnerText;
skill.lang = node.Attributes[0].Value;
}
if(node.Name == "damage")
{
skill.damage = Int32.Parse(node.InnerText);
}
}
skillList.Add(skill);
}
foreach(Skill skill in skillList)
{
Debug.Log(skill.id+skill.name+skill.lang+skill.damage);
}
}
}
class Skill
{
public int id;
public string name;
public string lang;
public int damage;
}
将xml文件放入工程的StreamingAssets文件夹中,用类来存,并读取。
4.Json
4.1用JsonUtility方法
新建两个Button来分别控制json的生成和读取。
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
//要存储为json的对象
[Serializable]
public class DayRangeMessage
{
public string date_time;//存储当天的日期
public List<int> rangelist; //一天之内每次运行程序生成的随机数列表
}
public class JsonTest : MonoBehaviour
{
string JsonPath; //json文件的路径
DayRangeMessage dayrangeMessage;//要存起来的对象
DayRangeMessage dayrangeMessagetemp;//要读取出来的对象
void Start()
{
JsonPath = Application.streamingAssetsPath + "/JsonTest.json";
InitJsonData();
}
//json 数据初始化
void InitJsonData()
{
//生成一个DayRangeMessage对象
dayrangeMessage = new DayRangeMessage();
//给DayRangeMessage对象的第一个属性date_time赋值
dayrangeMessage.date_time = "2019年8月14号";
//给DayRangeMessage对象的第二个属性rangelist赋值
//第二个个属性里添加有三个子元素
dayrangeMessage.rangelist = new List<int>();
dayrangeMessage.rangelist.Add(10);
dayrangeMessage.rangelist.Add(22);
dayrangeMessage.rangelist.Add(33);
}
//把上面初始化的数据进行保存
public void SaveJson()
{
//如果本地没有对应的json 文件,重新创建
if (!File.Exists(JsonPath))
{
File.Create(JsonPath).Dispose();//不加.Dispose第二次运行会报错
}
string json = JsonUtility.ToJson(dayrangeMessage, true);
File.WriteAllText(JsonPath, json);
Debug.Log("保存成功");
}
//从本地读取json数据
public void ReadJson()
{
if (!File.Exists(JsonPath))
{
Debug.LogError("读取的文件不存在!");
return;
}
string json = File.ReadAllText(JsonPath);
dayrangeMessagetemp = JsonUtility.FromJson<DayRangeMessage>(json);
//读取第一个属性:日期
string date = dayrangeMessagetemp.date_time;
Debug.Log(date);
//获取第二个属性:
for (int j = 0; j < dayrangeMessagetemp.rangelist.Count; j++)
{
Debug.Log(dayrangeMessagetemp.rangelist[j]);
}
}
}
4.2用LitJson方法
LitJson方法需要在Plugins插件中放入LitJson.dll文件,若无法引入,命名空间需要在VS中添加引用。
新建两个Button来分别控制json的生成和读取。
using LitJson;
using System.IO;
using UnityEngine;
public class Hero
{
public string name;
public int level;
public int attack;
public Hero() { }// 这个要有,不然JsonMapper.ToObject<T>(jsonStr) 会报错
public Hero(string name, int level, int attack)
{
this.name = name;
this.level = level;
this.attack = attack;
}
}
public class JsonTest : MonoBehaviour
{
Hero one;
string jsonPath;
void Start()
{
jsonPath = Application.streamingAssetsPath + "/JsonTest.json";
one = new Hero("SuperMan", 20, 345);
}
//把上面初始化的数据进行保存
public void SaveJson()
{
//如果本地没有对应的json 文件,重新创建
if (!File.Exists(jsonPath))
{
File.Create(jsonPath).Dispose();//不加.Dispose第二次运行会报错
}
string jsonStr = JsonMapper.ToJson(one);
File.WriteAllText(jsonPath, jsonStr);
Debug.Log("保存成功");
}
//从本地读取json数据
public void ReadJson()
{
if (!File.Exists(jsonPath))
{
Debug.LogError("读取的文件不存在!");
return;
}
string json = File.ReadAllText(jsonPath);
Hero jsonToObject = JsonMapper.ToObject<Hero>(json);
Debug.Log(jsonToObject.name);
}
}
5.Excel
方法一:EPPlus.dll
需要在plugins中导入EPPlus.dll文件。
using System.IO;
using UnityEngine;
using OfficeOpenXml;
public class Juse : MonoBehaviour
{
string ExcelPath;
void Start()
{
//string connectionStr="Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + jsonPath + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
//OleDbConnection oleConnection = new OleDbConnection(connectionStr);
ExcelPath = Application.streamingAssetsPath + "/ExcelRead.xlsx";
FileInfo fileInfo = new FileInfo(ExcelPath);
using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
{
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[1];//读取Excel中的第一张表格
string s = worksheet.Cells[3, 3].Value.ToString();//读取第一行、第三列的数据
Debug.Log(s);
worksheet.Cells[5, 3].Value = "des";//在第五行第三列写入数据
excelPackage.Save();//存储数据
}
}
}
方法二:SimpleExcel
1.在VS中导入Panda.SimpleExcel包
2.从Unity项目的package文件中将dll导入plugins文件夹,用哪个版本的可以问Chatgpt.


浙公网安备 33010602011771号