• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

学习记录unity的json存储

武飞扬头像
石阿包
帮助10

1效果展示

学新通

 2代码展示(备注很详细)

  1.  
    using System.Collections;
  2.  
    using System.Collections.Generic;
  3.  
    using UnityEngine;
  4.  
    using UnityEngine.UI;
  5.  
    using System.IO;
  6.  
     
  7.  
    //将类序列化到文件中,同时序列化到编辑器中
  8.  
    [System.Serializable]
  9.  
    public class PlayerCoin{
  10.  
    public int count;
  11.  
    public string name;
  12.  
    public bool isWinner;
  13.  
    }
  14.  
     
  15.  
    //存储一系列类数据,必须创建一个链表,链表必须存在在类中,才能被序列化
  16.  
    public class PlayerCoinList
  17.  
    {
  18.  
    public List<PlayerCoin> playerCoinList = new List<PlayerCoin>();
  19.  
    }
  20.  
     
  21.  
    public class DataSaveManager : MonoBehaviour
  22.  
    {
  23.  
    public PlayerCoinList list = new PlayerCoinList();
  24.  
    PlayerCoin knight; //保存骑士
  25.  
    PlayerCoin wizzard; //保存巫师
  26.  
     
  27.  
    public Text knightCoin; //骑士的金币数量的显示文字
  28.  
    public Text wizzardCoin; //巫师的金币数量的显示文字
  29.  
     
  30.  
     
  31.  
    // Start is called before the first frame update
  32.  
    void Start()
  33.  
    {
  34.  
    //创建json数据,只需要创建一次就行
  35.  
    // GenerateData();
  36.  
    //数据的读取
  37.  
    LoadDate();
  38.  
    }
  39.  
     
  40.  
    // Update is called once per frame
  41.  
    void Update()
  42.  
    {
  43.  
     
  44.  
    }
  45.  
     
  46.  
    #region 生成实例并赋初值
  47.  
    void GenerateData()
  48.  
    {
  49.  
    knight = new PlayerCoin();
  50.  
    knight.count = 0;
  51.  
    knight.name = "Knight";
  52.  
    knight.isWinner = false;
  53.  
     
  54.  
    wizzard = new PlayerCoin();
  55.  
    wizzard.count = 0;
  56.  
    wizzard.name = "Wizzard";
  57.  
    wizzard.isWinner = false;
  58.  
     
  59.  
    list.playerCoinList.Add(knight);
  60.  
    list.playerCoinList.Add(wizzard);
  61.  
    }
  62.  
    #endregion
  63.  
     
  64.  
    #region 金币增加按钮事件
  65.  
    //按钮事件:点击按钮时金币数量的增加
  66.  
    public void OnClickCoin(string _player)
  67.  
    {
  68.  
    if (_player.Equals("Knight"))
  69.  
    {
  70.  
    knight.count = 1;
  71.  
    knightCoin.text = knight.count.ToString();
  72.  
    }else if (_player.Equals("Wizzard"))
  73.  
    {
  74.  
    wizzard.count = 1;
  75.  
    wizzardCoin.text = wizzard.count.ToString();
  76.  
    }
  77.  
    }
  78.  
    #endregion
  79.  
     
  80.  
    #region 数据存储
  81.  
    void SaveData()
  82.  
    {
  83.  
    //将list转化成一个json文件
  84.  
    string json = JsonUtility.ToJson(list,true);
  85.  
    //设置保存的路径
  86.  
    string filePath = Application.streamingAssetsPath "/playCoinList.json";
  87.  
    //文件的写入
  88.  
    using(StreamWriter sw = new StreamWriter(filePath))
  89.  
    {
  90.  
    sw.WriteLine(json);
  91.  
    sw.Close();
  92.  
    sw.Dispose();
  93.  
    }
  94.  
    }
  95.  
     
  96.  
    #endregion
  97.  
     
  98.  
    #region 保存按钮事件
  99.  
     
  100.  
    public void OnClickQuit()
  101.  
    {
  102.  
    //数据的存储
  103.  
    SaveData();
  104.  
    //退出游戏
  105.  
    UnityEditor.EditorApplication.isPlaying = false; //编辑器下退出游戏
  106.  
    Application.Quit();
  107.  
    }
  108.  
    #endregion
  109.  
     
  110.  
    #region 数据的读取
  111.  
     
  112.  
    void LoadDate()
  113.  
    {
  114.  
    string json;
  115.  
    string filePath = Application.streamingAssetsPath "/playCoinList.json";
  116.  
     
  117.  
    //检测json文件是否存在
  118.  
    if (File.Exists(filePath))
  119.  
    {
  120.  
    //json文件存在,直接进行数据读取
  121.  
    using (StreamReader sr = new StreamReader(filePath))
  122.  
    {
  123.  
    json = sr.ReadToEnd();
  124.  
    sr.Close();
  125.  
    }
  126.  
    //将json文件转化为内存中的一个变量
  127.  
    list = JsonUtility.FromJson<PlayerCoinList>(json);
  128.  
     
  129.  
    //将读取的数据赋值
  130.  
    knight = list.playerCoinList[0];
  131.  
    wizzard = list.playerCoinList[1];
  132.  
     
  133.  
    //更新文本显示
  134.  
    knightCoin.text = knight.count.ToString();
  135.  
    wizzardCoin.text = wizzard.count.ToString();
  136.  
    }
  137.  
    else
  138.  
    {
  139.  
    //json文件不存在,先创建,再进行读取
  140.  
    GenerateData();
  141.  
    }
  142.  
     
  143.  
     
  144.  
    }
  145.  
    #endregion
  146.  
     
  147.  
    }
学新通

3Unity界面

学新通

 脚本挂载在manager这个空物体上,对应的按钮添加对应的事件(注意:在金币增加按钮事件的添加时:输入的string的字符串必须与代码中一致)

4程序包链接

链接:https://pan.百度.com/s/1a9x6Jmbh8o9L1Q54tqED6Q 
提取码:jl98

(ps:在哔哩哔哩找了一个视频学习的,视频链接也放这了:【UNITY教程】第1集 使用JSON存储数据_哔哩哔哩_bilibili

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfgkcii
系列文章
更多 icon
同类精品
更多 icon
继续加载