学习记录unity的json存储
1效果展示
2代码展示(备注很详细)
-
using System.Collections;
-
using System.Collections.Generic;
-
using UnityEngine;
-
using UnityEngine.UI;
-
using System.IO;
-
-
//将类序列化到文件中,同时序列化到编辑器中
-
[]
-
public class PlayerCoin{
-
public int count;
-
public string name;
-
public bool isWinner;
-
}
-
-
//存储一系列类数据,必须创建一个链表,链表必须存在在类中,才能被序列化
-
public class PlayerCoinList
-
{
-
public List<PlayerCoin> playerCoinList = new List<PlayerCoin>();
-
}
-
-
public class DataSaveManager : MonoBehaviour
-
{
-
public PlayerCoinList list = new PlayerCoinList();
-
PlayerCoin knight; //保存骑士
-
PlayerCoin wizzard; //保存巫师
-
-
public Text knightCoin; //骑士的金币数量的显示文字
-
public Text wizzardCoin; //巫师的金币数量的显示文字
-
-
-
// Start is called before the first frame update
-
void Start()
-
{
-
//创建json数据,只需要创建一次就行
-
// GenerateData();
-
//数据的读取
-
LoadDate();
-
}
-
-
// Update is called once per frame
-
void Update()
-
{
-
-
}
-
-
-
void GenerateData()
-
{
-
knight = new PlayerCoin();
-
knight.count = 0;
-
knight.name = "Knight";
-
knight.isWinner = false;
-
-
wizzard = new PlayerCoin();
-
wizzard.count = 0;
-
wizzard.name = "Wizzard";
-
wizzard.isWinner = false;
-
-
list.playerCoinList.Add(knight);
-
list.playerCoinList.Add(wizzard);
-
}
-
-
-
-
//按钮事件:点击按钮时金币数量的增加
-
public void OnClickCoin(string _player)
-
{
-
if (_player.Equals("Knight"))
-
{
-
knight.count = 1;
-
knightCoin.text = knight.count.ToString();
-
}else if (_player.Equals("Wizzard"))
-
{
-
wizzard.count = 1;
-
wizzardCoin.text = wizzard.count.ToString();
-
}
-
}
-
-
-
-
void SaveData()
-
{
-
//将list转化成一个json文件
-
string json = JsonUtility.ToJson(list,true);
-
//设置保存的路径
-
string filePath = Application.streamingAssetsPath "/playCoinList.json";
-
//文件的写入
-
using(StreamWriter sw = new StreamWriter(filePath))
-
{
-
sw.WriteLine(json);
-
sw.Close();
-
sw.Dispose();
-
}
-
}
-
-
-
-
-
-
public void OnClickQuit()
-
{
-
//数据的存储
-
SaveData();
-
//退出游戏
-
UnityEditor.EditorApplication.isPlaying = false; //编辑器下退出游戏
-
Application.Quit();
-
}
-
-
-
-
-
void LoadDate()
-
{
-
string json;
-
string filePath = Application.streamingAssetsPath "/playCoinList.json";
-
-
//检测json文件是否存在
-
if (File.Exists(filePath))
-
{
-
//json文件存在,直接进行数据读取
-
using (StreamReader sr = new StreamReader(filePath))
-
{
-
json = sr.ReadToEnd();
-
sr.Close();
-
}
-
//将json文件转化为内存中的一个变量
-
list = JsonUtility.FromJson<PlayerCoinList>(json);
-
-
//将读取的数据赋值
-
knight = list.playerCoinList[0];
-
wizzard = list.playerCoinList[1];
-
-
//更新文本显示
-
knightCoin.text = knight.count.ToString();
-
wizzardCoin.text = wizzard.count.ToString();
-
}
-
else
-
{
-
//json文件不存在,先创建,再进行读取
-
GenerateData();
-
}
-
-
-
}
-
-
-
}
3Unity界面
脚本挂载在manager这个空物体上,对应的按钮添加对应的事件(注意:在金币增加按钮事件的添加时:输入的string的字符串必须与代码中一致)
4程序包链接
链接:https://pan.百度.com/s/1a9x6Jmbh8o9L1Q54tqED6Q
提取码:jl98
(ps:在哔哩哔哩找了一个视频学习的,视频链接也放这了:【UNITY教程】第1集 使用JSON存储数据_哔哩哔哩_bilibili)
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfgkcii
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01