第一种 PlayerPrefs
//设置要保存的信息 key只能是string形式(自定义key)需要保存的数据(value)包含三种(int,float,string)
PlayerPrefs.SetString("key", "value");
PlayerPrefs.SetInt("key", 1);
PlayerPrefs.SetFloat("key", 1.0f);
//读取保存的数据
PlayerPrefs.GetFloat("key");
PlayerPrefs.GetInt("key");
PlayerPrefs.GetString("key");
PlayerPerfs.DeleteAll();//删除所有键值
PlayerPerfs.DeleteKey("keyInt");//删除某个键值
bool exist = PlayerPerfs.HasKey("keyInt");//判断是否存在键值
第二种 Json
需要保存json文件前先要导入下面的文件放在Plugins路径下(注system.data文件要和unity版本保持一致)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
using System.Text;
using UnityEngine.AI;
using UnityEngine.UI;
public class Infos
{
public string CopyName;
public string CopyPosition;
public string CopyRotation;
}
public class Data
{
public List<Infos> _info;
public Data()
{
_info = new List<Infos>();
}
}
public class JasonData : MonoBehaviour {
private string _path;//json文件路径
private string _txtPath;//txt文件路径
void Start () {
_txtPath = Application.streamingAssetsPath + "/CopyInfo.txt";
_path = Application.streamingAssetsPath + "/CopyInfo.json";
//方法调用
ReadText();
//方法调用
ReadFromJson();
}
void ReadFromJson()
{
//读json文件
string JsonData = File.ReadAllText(_path);
List<Infos> Mesage = JsonMapper.ToObject<List<Infos>>(JsonData);
Debug.LogError(Mesage.Count);
}
void ReadText()
{
Data da = new Data();
using (StreamReader Reader = new StreamReader(_txtPath, Encoding.UTF8))
{
string temStr = string.Empty;
while (!string.IsNullOrEmpty(temStr = Reader.ReadLine()))
{
Infos inf = new Infos();
string[] Str = temStr.Split('_');
inf.CopyName = Str[0];
inf.CopyPosition = Str[1];
inf.CopyRotation = Str[2];
da._info.Add(inf);
}
}
//数据读取完毕 开始写入json 传递的List<>
string jasonData = JsonMapper.ToJson(da._info);
File.WriteAllText(_path, jasonData);
}
}
第三种 Xml
读Xml文件
static void Main(string[] args)
{
//将XML文件加载进来
XDocument document = XDocument.Load("D:\\123.xml");
//获取到XML的根元素进行操作
XElement root= document.Root;
XElement ele= root.Element("BOOK");
//获取name标签的值
XElement shuxing= ele.Element("name");
Console.WriteLine(shuxing.Value);
//获取根元素下的所有子元素
IEnumerable<XElement> enumerable = root.Elements();
foreach (XElement item in enumerable)
{
foreach (XElement item1 in item.Elements())
{
Console.WriteLine(item1.Name); //输出 name name1
}
Console.WriteLine(item.Attribute("id").Value); //输出20
}
Console.ReadKey();
}
写Xml文件
static void Main(string[] args)
{
//获取根节点对象
XDocument document = new XDocument();
XElement root = new XElement("School");
XElement book = new XElement("BOOK");
book.SetElementValue("name", "高等数学");
book.SetElementValue("name1", "大学英语");
root.Add(book);
root.Save("d:\\123.xml");
Console.ReadKey();
}
第四种 excel
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.IO;
using Excel;
using OfficeOpenXml;
public class node
{
public string name;
public int age;
public string sex;
public float yuwen,shuxue,yinyu;
}
public class lists
{
public List<node> lis;
public lists()
{
lis = new List<node>();
}
}
public class Excelstr : MonoBehaviour {
lists sa = new lists();
List<float> shu;
List<float> y;
List<float> yin;
void Start () {
exceldate();
write(sa.lis);
}
//读excel文件
public void exceldate()
{
shu = new List<float>();
y = new List<float>();
yin = new List<float>();
sa.lis = new List<node>();
string path =Application.streamingAssetsPath+"\\chengji.xlsx";
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
IExcelDataReader s = ExcelReaderFactory.CreateOpenXmlReader(file);
if (s.Read() == false)
return;
while (s.Read())
{
node no = new node();
no.name = s.GetString(0);
no.age = s.GetInt32(2);
no.sex = s.GetString(1);
no.yuwen = s.GetInt64(3);
no.shuxue = s.GetInt64(4);
no.yinyu = s.GetInt64(5);
sa.lis.Add(no);
shu.Add(no.shuxue);
y.Add(no.yuwen);
yin.Add(no.yinyu);
}
}
//写文件
public void write(List<node> no)
{
string writepath = Application.streamingAssetsPath + "\\chengji11.xlsx";
FileInfo excelinfo = new FileInfo(writepath);
if (excelinfo.Exists)
{
excelinfo.Delete();
excelinfo = new FileInfo(writepath);
}
using (ExcelPackage package = new ExcelPackage(excelinfo))
{
Debug.Log("0");
ExcelWorksheet sheet = package.Workbook.Worksheets.Add("TestInfo");
sheet.Cells[1, 1].Value = "name";
sheet.Cells[1, 2].Value = "age";
sheet.Cells[1, 3].Value = "sex";
sheet.Cells[1, 4].Value = "yuwen";
sheet.Cells[1, 5].Value = "shuxue";
sheet.Cells[1, 6].Value = "yinyu";
for (int i = 0; i < no.Count; i++)
{
Debug.Log("1");
sheet.Cells[2 + i, 1].Value = no[i].name;
sheet.Cells[2 + i, 2].Value = no[i].age;
sheet.Cells[2 + i, 3].Value = no[i].sex;
sheet.Cells[2 + i, 4].Value = no[i].yuwen;
sheet.Cells[2 + i, 5].Value = no[i].shuxue;
sheet.Cells[2 + i, 6].Value = no[i].yinyu;
}
package.Save();
}
}
}
第五种 Txt
读Txt文件
string path = @"E:\test";//文件路径
string info = string.Empty;//接收读到的信息
List<string> Allinfo = new List<string>();
//读Txt文件固定格式需要应用(using System.IO;using System.Text;)
using (StreamReader read = new StreamReader(path, Encoding.UTF8))
{
while ((info = read.ReadLine())!=null)
{
Allinfo.Add(info);
}
}
写txt文件
//写文件的固定格式
using (StreamWriter write = new StreamWriter(path, false, Encoding.UTF8))
{
string writeInfo = "需要写入的字符串,必要时用循环写入";
write.WriteLine(writeInfo);
}