【Unity 31】 Unity中的文件读写, json,Get和Post,聚合数据接口的使用

这篇博客详细介绍了Unity中的文件读写,包括不同路径的应用,如Application.dataPath、Application.streamingAssetsPath和Application.persistentDataPath。接着讲解了Json的键值对语法和在Unity中使用Json的方法,以及如何进行字符串与类之间的转换。此外,文章还探讨了Get和Post两种HTTP请求的特点,并举例说明了聚合数据接口的使用,如历史上的今天和QQ号码吉凶测试的API调用。
摘要由CSDN通过智能技术生成

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;
         
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值