1.计算机存储设备
CPU缓存、内存、硬盘、网盘。
硬盘又分为固态硬盘ssd和机械硬盘hdd。
2.PlayerPrefs(Unity内 置持久化存储类)
using System;
using UnityEngine;
using UnityEngine.SocialPlatforms.Impl;
public class PlayerPrefsDemo : MonoBehaviour
{
private void Start()
{
//1.Unity提供的PlayerPrefs类,本地持久化存储
//一个Key对应一个值
//文件格式.plist
//存储 Int\Float\String
//存储不能同名,即使是不同数据类型
//同名的话,后者会覆盖前者
//存储 Set ---SetInt\SetFloat\SetString
// PlayerPrefs.SetFloat("Score",100);
// Debug.Log("存储成功!");
//读取 Get ---GetInt\GetFloat\GetString
// float value = PlayerPrefs.GetFloat("Score");
// Debug.Log(value);
//尝试读取,如果没有就给一个默认值
// int value = PlayerPrefs.GetInt("Rank", 99);
// Debug.Log(value);
//判断某个Key值是否存在
// bool haskey = PlayerPrefs.HasKey("Rank");
// Debug.Log(haskey);
}
}
3.XML(适合做配置文件,不经常修改和信息传输)
语法:
<?xml version="1.0" encoding="UTF-8" ?>
<!-- 注释 -->
<!--xml一般指可扩展标记语言,优点可读性强、缺点字符含量多-->
<!--最外面的标签称之为根标签-->
<AllStudnets>
<!--标签对、键值对-->
<Student Hobby="Pingpang" Lesson="math"> <!--属性-->
<StudentName>xiaoming</StudentName>
<StudentAge>18</StudentAge>
<!--单标签【在标签没有值的时候可以使用】-->
<SrudentAdress/>
</Student>
<!--标签的名字可以重复-->
<Student>
<StudentName>xiaohong</StudentName>
<StudentAge>19</StudentAge>
<!--标签可以用英文、中文和数字,但不能用符号-->
<学生籍贯>北京</学生籍贯>
</Student>
</AllStudnets>
生成 :
using System;
using System.Xml;//关于xml操作的命名空间
using UnityEngine;
public class XMLGenarate : MonoBehaviour
{
private void Start()
{
//1.实例化xml文档对象
XmlDocument doc = new XmlDocument();
//2.创建头文本节点--------------------------头文本节点
XmlDeclaration headText = doc.CreateXmlDeclaration("1.0", "UTF-8", "");//版本号,编码,平台
//将头文本节点添加到xml文档中
doc.AppendChild(headText);
//3.创建根元素节点[GameObject]--------------根元素
XmlElement rootElement = doc.CreateElement("GameObject");
//创建属性节点
XmlAttribute layerAtt = doc.CreateAttribute("Layer");
//给属性节点赋值
//layerAtt.Value = gameObject.layer.ToString();
layerAtt.Value = LayerMask.LayerToName(gameObject.layer);
//给元素添加属性---方法1【Name】
rootElement.SetAttribute("Name", gameObject.name);
//给元素添加属性---方法2【Layer】
rootElement.SetAttributeNode(layerAtt);
//创建子节点【Transform】----------------子节点
XmlElement transformElement = doc.CreateElement("Transform");
//继续创建子节点---方法1【position】--------------子节点的子节点
XmlElement positionElement = doc.CreateElement("position");
//给元素节点设置值【文本节点】
positionElement.InnerText = transform.position.ToString();//Verctor3
//继续创建子节点【Rotation】--------------子节点的子节点
XmlElement rotationElement = doc.CreateElement("Rotation");
//给元素节点设置值【文本节点】
rotationElement.InnerText = transform.eulerAngles.ToString();//欧拉角
//继续创建子节点---方法2【LocalScale】------------子节点的子节点
XmlNode localScaleNode = doc.CreateNode(XmlNodeType.Element, "LocalSacle","");//节点类型
//给元素节点设置值【文本节点】
localScaleNode.InnerText = transform.localScale.ToString();
//添加的时候最好是从里往外添加
//4.将position和rotation节点添加到transform节点上
transformElement.AppendChild(positionElement);
transformElement.AppendChild(rotationElement);
transformElement.AppendChild(localScaleNode);
//将transform节点添加到根元素节点GameObject节点上
rootElement.AppendChild(transformElement);
//将根元素节点添加到xml文档中
doc.AppendChild(rootElement);
//5.保存文档
doc.Save(Application.dataPath + "/XMLs/GenaratedXML.xml");
}
}
解析:
using System;
using System.Xml;
using UnityEngine;
public class XMLParse : MonoBehaviour
{
private void Start()
{
//创建一个xml文档对象
XmlDocument doc = new XmlDocument();
//读取xml文档
doc.Load(Application.dataPath + "/XMLs/GenaratedXML.xml");
//解析xml中的一个节点
XmlNode posNode = doc.SelectSingleNode("GameObject/Transform/Position");
Debug.Log(posNode.InnerText);
//解析xml的多个节点
XmlNodeList list = doc.SelectNodes("//Transform");
//遍历list中的所有节点
for (int i = 0; i < list.Count; i++)
{
Debug.Log("Position:" + list[i].ChildNodes[0].InnerText);
Debug.Log("Rotation:" + list[i].ChildNodes[1].InnerText);
Debug.Log("LocalScale:" + list[i].ChildNodes[2].InnerText);
}
}
}
4.JSON
语法:
{
"student": [
{
"studentName": "xiaoming",
"studentAge": 18,
"studentScore": [80,90,100]
},
{
"studentName": "xiaohong",
"studentAge": 20,
"studentScore": {
"语文": 80,
"数学": 90,
"英语": 100
}
},
{
"studentName": "xiaogang",
"studentAge": 23
}
]
}
生成:
解析:
5.DB