转化Json格式文件 读取数据
// Json书写格式
{
"goodsList":[
{
"EquipID": 11,
"EquipName": "Benz1",
"EquipModePath": "Mode/goods",
"EquipPicturePath": "EquipPicturePath1",
"EquipLength": 8,
"EquipWide": 3,
"EquipTall": 1.2,
"EquipWeight": 36.4
},
{
"EquipID": 2,
"EquipName": "Benz2",
"EquipModePath": "Mode/goods",
"EquipPicturePath": "EquipPicturePath2",
"EquipLength": 9,
"EquipWide": 4,
"EquipTall": 1.4,
"EquipWeight": 37
}
]
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using LitJson;
[System.Serializable]
public class GoodsRoot
{
public List<EquipInfo> goodsList;
}
public class ConfigManager
{
#region Instance
private static ConfigManager sInstance;
public static ConfigManager Instance
{
get
{
if (sInstance == null)
{
sInstance = new ConfigManager();
}
return sInstance;
}
}
#endregion
private List<EquipInfo> EquipInfos = new List<EquipInfo>();
public List<EquipInfo> GetEquipInfos
{
get
{
return EquipInfos;
}
}
ConfigManager()
{
Getgoodsinfo();
}
void Getgoodsinfo()
{
var streamreader = Resources.Load<TextAsset>("Config/goods").text;
//JsonReader js = new JsonReader(streamreader);//再转换成json数据
GoodsRoot r = JsonUtility.FromJson<GoodsRoot>(streamreader);//读取
EquipInfos = r.goodsList;
}
//void Getcarbodyinfo()
//{
// var streamreader = Resources.Load<TextAsset>("Config/Carbody").text;
// JsonReader js = new JsonReader(streamreader);//再转换成json数据
// GoodsRoot r = JsonMapper.ToObject<GoodsRoot >(js);//读取
// EquipInfos = r.carList;
//}
}
//读取配置文件,进行语言切换配置表书写 文字打印赋值
//文件书写格式
//str001 好好学习 Study hard
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class I18n
{
static I18n sInstance;
public static I18n Instance
{
get
{
if (sInstance == null)
{
sInstance = new I18n();
}
return sInstance;
}
}
Dictionary<string, string[]> mKeyValues = new Dictionary<string, string[]>();
I18n()
{
Init();
}
void Init()
{
mKeyValues = new Dictionary<string, string[]>();
var fileStr = Resources.Load<TextAsset>("Configs/i18n").text;
string[] lines = fileStr.Split('\n');
foreach (var line in lines)
{
string[] fields = line.Split('\t');
if (fields.Length != 3)
{
continue;
}
var key = fields[0];
#if VERSION_EN
var value = fields[2];
#else
var value = fields[1];
var value1 = fields[2];
string[] valus = new string[2] { value, value1 };
#endif
mKeyValues.Add(key, valus);
}
//}
}
public string GetValue(string key, Language language)
{
switch (language)
{
case Language.Chinese:
return mKeyValues[key][0];
case Language.English:
return mKeyValues[key][1];
}
//if (mKeyValues.ContainsKey(key))
//{
// //return mKeyValues[key];
//}
Debug.LogError("不存在 " + key);
return "";
}
}
创建TXT文件存储需要数据
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Experimental.PlayerLoop;
public class TxtLoad : MonoBehaviour
{
public Transform tra;
// Use this for initialization
void Start()
{
for (int i = 0; i < tra.childCount-1; i++)
{
string info = tra.GetChild(i).name + " " + tra.GetChild(i).GetComponent<GraspItem>().Id;
print(info);
CreateFile("E:/MyTxt.txt", info);
}
//CreateFile("E:/MyTxt.txt", "这是你写的信息");
//CreateFile("E:/MyTxt.txt", "Hello!I am Jtro!");
}//End of Start
void Update()
{
if (Input.GetKey(KeyCode.A))
{
readtxt();
}
}
/// <summary>
/// Creates the file.创建文本的方法
/// </summary>
/// <param name="_filePath">File path. 文件的路径</param>
/// <param name="_data">Data. 文件的内容</param>
public void CreateFile(string _filePath, string _data)
{
StreamWriter sw;
FileInfo fi = new FileInfo(_filePath);
//如果文件不存在
if (!fi.Exists)
{
//创建文件
sw = fi.CreateText();
}
else
{
//打开文件
sw = fi.AppendText();
}
sw.WriteLine(_data);//以行的形式写入
//sw.Write(_data);//首位衔接的方式写入
sw.Close();//关闭流
sw.Dispose();//销毁流
}
/// <summary>
/// Destroies the file.销毁创建的文本
/// </summary>
/// <param name="_filePath">File path. 文本的路径</param>
public void DestroyFile(string _filePath)
{
if (File.Exists(_filePath))
{
File.Delete(_filePath);
Debug.Log("删除文件: " + _filePath);
}
}
/// <summary>
/// 读取文件
/// </summary>
public void readtxt()
{
string[] strs = File.ReadAllLines("E:/MyTxt.txt");
print(strs[0]);
}
}