PS:本系列笔记将会记录我此次在北京学习Unity开发的总体过程,方便后期写总结,笔记为日更。
笔记内容均为 自己理解,不保证每个都对
Part 1 文件读写:
数据存储:
根据游戏类型划分:单机游戏,弱联网游戏,强联网游戏
单机游戏:不需要联网,数据存储在本地内存中
弱联网游戏:关键操作需要联网,数据本地和服务器都有存储
强联网游戏:时刻必须联网,数据存储在服务器中
文件读写 以 TXT 文档为例:
三种主要路径:
1、Application.dataPath:项目的工程路径
2、Application.streamingAssetsPath:只读路径,随项目发布到软件当中
3、Application.persistentDataPath:可读可写路径,持久性存储数据
Debug.Log("dataPath == " + Application.dataPath);
Debug.Log("streamingAssetsPath == " + Application.streamingAssetsPath);
Debug.Log("persistentDataPath == " + Application.persistentDataPath);
样例输出:
利用streamingAssetsPath来读写文件:
将要读文件 放在 StreamingAssets中
通过 StreamReader进行读, StreamWriter进行写(StreamWriter写时 为全部重写)
例如:
string path = Application.streamingAssetsPath + "/UserInfo.txt";
StreamReader tmpReader = new StreamReader(path);
//读的时候用
tmpReader.ReadLine(); //返回类型为 string
string path = Application.streamingAssetsPath + "/UserInfo.txt";
StreamWriter tmpWriter = new StreamWriter(path);
//读的时候用
tmpWriter.Write(); //没有换行
tmpWriter.WriteLine(); //自带换行
Demo:
简易实现 类似 QQ登录,可以选择账号自动输入密码,或 手动输入账号密码,并记住账号的功能
基本类:
public class UserItem
{
public string userName; //用户名
public string passWord; //密码
public UserItem(string username, string password)
{
this.userName = username;
this.passWord = password;
}
}
M层:
要做到的功能有(判断是否存在,读数据,写数据,)
public class LoginM
{
public List<UserItem> allUser;
public LoginM()
{
allUser = new List<UserItem>();
LoadUserInfo();
}
public bool isContain(string UserName, string PassWord) //判断用户名是否已在表中
{
bool flag = true;
for (int i = 0; i < allUser.Count; i++)
{
if(UserName == allUser[i].userName)
{
flag = false;
break;