用Unity的TextAsset读取TXT文档内容,将物品信息存入字典中
2017-11-18 21:25
585 查看
用Unity的TextAsset读取TXT文档内容,将物品信息存入字典中
Text Asset 文本资源Text Assets are a format for imported text files. When you drop a text file into your Project Folder, it will be converted to a Text Asset. The supported text formats are:
文本资源是导入的文本文件的一个格式。当你拖入一个文本文件到你的项目时,他会被转换为文本资源。支持的文本格式有:
.txt
.html
.htm
.xml
.bytes
Properties 属性
Text
The full text of the asset as a single string.
物品属性分析
之后需要新建一个文本txt来存放物品的属性,这里以药品为例
TXT文档内容
每一行文本对应一种物品,用逗号分隔,每个值对应属性表里的每一列属性。
之后新建c#脚本解析文本内容
private TextAsset objectsInfoListText; // 新建TextAsset变量 private Dictionary<int, ObjectInfo> objectInfoDict = new Dictionary<int,ObjectInfo>(); void Awake() { objectsInfoListText = Resources.Load("ObjectsInfoList") as TextAsset; // 从Resouces的path中读取到文件 ReadInfo(); } void ReadInfo() { string text = objectsInfoListText.text; // 将文本内容作为字符串读取 string[] objInfoArray = text.Split('\n'); // 以\n为分割符将文本分割为一个数组 foreach (string str in objInfoArray) // 遍历每一行并将每一个药品的值放入类对象中,并存入字典 { ObjectInfo info = new ObjectInfo(); string[] propertyArray = str.Split(','); int id = int.Parse(propertyArray[0]); string name = propertyArray[1]; string icon_name = propertyArray[2]; string str_type = propertyArray[3]; ObjectType type = ObjectType.Drug; switch (str_type) { case "Equip": type = ObjectType.Equip; break; case "Mat": type = ObjectType.Mat; break; case "Drug": 91aa type = ObjectType.Drug; break; } info.id = id; info.name = name; info.icon_name = icon_name; info.type = type; if (type == ObjectType.Drug) { int hp = int.Parse(propertyArray[4]); int mp = int.Parse(propertyArray[5]); int price_sell = int.Parse(propertyArray[6]); int price_buy = int.Parse(propertyArray[7]); info.hp = hp; info.mp = mp; info.price_sell = price_sell; info.price_buy = price_buy; } // 将物品信息存储到字典中 objectInfoDict.Add(id, info); } } public enum ObjectType { Drug, Equip, Mat, }; public class ObjectInfo { public int id; public string name; public string icon_name; public ObjectType type; public int hp; public int mp; public int price_sell; public int price_buy; }
相关文章推荐
- Unity 简单读取txt文档内容的方法
- Unity读取txt文件并且用字典来使用txt里面的信息
- 编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt
- unity 读取TXT文档的内容
- Unity读取txt文件并且用字典来使用txt里面的信息
- 将本地txt文档内容导入到NSMutableDictionary(动态字典)
- Java读写文件,读取execl表格,读取指定行列单元格信息,拼接sql,写入TXT文档。
- CFileDialog 读取txt文档内容
- unity 中读取文本中的物品信息
- C++读取txt文件内容——每行按列存入数组
- TextAsset解析txt文档不识别中文问题
- C#生成PDF文档,读取TXT文件内容
- Lucene3.1 之读取txt文本文档内容
- Android读取Asset读取指定的Text文档
- java读取txt文档 将内容存放到数据库
- unity手游开发--读取Txt文本内容
- Unity 读取文件 TextAsset读取配置文件
- 读取sd卡里的txt文档信息
- 第二周第四天(解析/过滤文件/找d盘里的所有txt文件/FileInputStream/FileOutputStream/拷贝文件/readLine读取一个文本行/在一个文档里写入内容)
- Unity 简易读取配置文件内容(txt,懒人用)