当前位置:网站首页>Unityjson file creation and reading
Unityjson file creation and reading
2022-04-22 07:50:00 【Dream back to Datang knock code】
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using System;
public class LevelJosnManager : MonoBehaviour
{
#if UNITY_EDITOR
public static string filepath = Application.streamingAssetsPath+"/LevelData.json";
#else
#endif
/// <summary>
/// Store friend information
/// </summary>
public static void SaveJson()
{
// because JsonMapper.ToJson Method does not support int convert to string type , So use this line of code to put int convert to string
JsonMapper.RegisterImporter<int, string>((int value) =>
{
return value.ToString();
});
// Save the data as Json File format , Convert a data object to json Format string
string jsonData = JsonMapper.ToJson(ReadJson());
// Create file operation
FileInfo fileInfo = new FileInfo(filepath);
// establish json file
if (!fileInfo.Exists)
{
fileInfo.Create().Dispose();
fileInfo.Refresh();
}
/// Using regular expressions Garbled code json Turn into UTF-8
Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
jsonData = reg.Replace(jsonData, delegate (Match m) { return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); });
// Write content
StreamWriter write = new StreamWriter(filepath);
write.WriteLine(jsonData);
write.Dispose();
write.Close();
#if UNITY_EDITOR
// Refresh List View
AssetDatabase.Refresh();
#endif
}
/// <summary>
/// Read json file
/// </summary>
public static List<Level> ReadJson()
{
// file does not exist
if (!File.Exists(filepath))
{
if (LevelList==null)
{
LevelList = new List<Level>();
}
return LevelList;
}
// Read json file
StreamReader reader = new StreamReader(filepath);
// without json File to create a
LevelList =
JsonMapper.ToObject<List<Level>>(reader.ReadToEnd()) ?? new List<Level>();
reader.Dispose();
reader.Close();
return LevelList;
}
/// <summary>
/// Level list
/// </summary>
public static List<Level> LevelList;
}
/// <summary>
/// The data of each level
/// </summary>
public class Level
{
public List<Box> boxList;
public List<string> levelCarList;
}
public class Box
{
/// <summary>
/// square ID
/// </summary>
public string boxID;
/// <summary>
/// Grid type
/// </summary>
public string boxLevelKing;
/// <summary>
/// Grid direction
/// </summary>
public string boxLevelDirection;
}
版权声明
本文为[Dream back to Datang knock code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220621526803.html
边栏推荐
猜你喜欢
随机推荐
charles的基本使用
CameraFlyControllerEditor
Enumerations and structures in C #
js根据时间多少,计算该显示年月日
Opportunity interview questions
MySQL 中文字段排序问题(根据中文拼音排序)
unityJson文件创建和读取
unity面试题
1: Ask what is a thread pool and how to answer it best
记录JS遇到的一些坑
Rt-thread [一] 创建工程
MySQL Chinese field sorting problem (sorting according to Chinese Pinyin)
Stm32外设篇 [二] I2C
Preliminary analysis of C program and matters needing attention in writing
第六章第二节 Map和结构体
【TCP/IP 一 概述】
依赖冲突查找与解决办法(以EasyPoi为例,出现 NoSuchMethodError 或 NoClassDefFoundError )
JS wdatepicker to get the selected date
Oracle sequence usage collation
数据库高级查询(2)
![Stm32外设篇 [二] I2C](/img/98/13b749c8c1edc038b70e62389df9fe.png)


![Stm32外设篇 [四] RCC](/img/e1/e81d1e6017bebc87690227a7aa5f99.png)



![Rt-thread [二] 系统初始化流程](/img/46/6e2942e4c18c0220378050205e6528.png)

