Unity读取Text文本案例


//首先定义一个用来保存装备
using UnityEngine;
using System.Collections;
public enum InventoryType{
	Equip,
	Drug
}
public enum EquipType{
	Arms,
	Shose,
	Clothes,
	Jewelry
}
public class Inventory  {
	//根据文本文件里的内容创建这么一个类,来保存物品信息.
	private int _ID;//装备ID
	private string _Name;//装备名字
	private string _Icoin;//图集名字
	private InventoryType _inventoryType;//物品类型
	private EquipType _equipType;//装备类型
	private int _EquipLevel;//装备等级
	private int _Damage;//装备伤害
	private int _Hp;//装备血量
	private int _Power;//装备战斗力
	private string _Des;//装备描述

	public int ID{
		get{return _ID;}
		set{_ID=value;}
	}
	public string Name{
		get{return _Name;}
		set{_Name=value;}
	}
	public string Icoin{
		get{return _Icoin;}
		set{_Icoin=value;}
	}
	public InventoryType inventoryType{
		get{return _inventoryType;}
		set{_inventoryType=value;}
	}
	public EquipType equipType{
		get{return _equipType;}
		set{_equipType=value;}
	}
	public int EquipLevel{
		get{return _EquipLevel;}
		set{_EquipLevel=value;}
	}
	public int Damage{
		get{return _Damage;}
		set{_Damage=value;}
	}
	public int Hp{
		get{return _Hp;}
		set{_Hp=value;}
	}
	public int Power{
		get{return _Power;}
		set{_Power=value;}
	}
	public string Des{
		get{return _Des;}
		set{_Des=value;}
	}
}
用来读取Text文档
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TextParsing : MonoBehaviour {
	//字典,用来保存信息,通过物品ID来获取相应的信息
	public DictionaryinventoryDic = new Dictionary ();
	//TextAsset:当你拖入一个文本文件到你的项目时,他会被转换为文本资源。
	//支持的文本格式有:txt,html,htm,xml,bytes
	public TextAsset MyText;
	void Awake(){
		ReadTextInfo ();
		Debug.Log (inventoryDic.Count);
	}
	void ReadTextInfo(){
		//获取文本文件里的内容
		string str = MyText.ToString ();
		//将每行的数据保存到StrArray数组中
		string[] strArray = str.Split ('\n');
		//遍历strArray数组
		foreach (string itemStr in strArray) {
			Debug.Log(itemStr);
			//以 | 区分每个信息,将其保存到数组中
			string [] itemArray = itemStr.Split ('|');
			//新建一个Inventory类,存储物品信息
			Inventory inventory = new Inventory ();
			/*这里文本文件里物品信息顺序依次为(可以打开text文本看看):
			 * 装备ID,
			 * 装备名字,
			 * 对应的图集名,
			 * 物品类型,
			 * 装备类型,
			 * 装备等级,
			 * 装备加成伤害,
			 * 装备加成血量,
			 * 装备加成战斗力,
			 * 物品介绍*/
			inventory.ID = int.Parse (itemArray [0]);
			inventory.Name = itemArray [1];
			inventory.Icoin = itemArray [2];
			switch (itemArray [3]) {
			case "Equip":
				inventory.inventoryType = InventoryType.Equip;
				break;
			case "Drug":
				inventory.inventoryType = InventoryType.Drug;
				break;
			default:
				break;
			}
			if (inventory.inventoryType == InventoryType.Equip) {
				switch (itemArray [4]) {
				case "Arms":
					inventory.equipType = EquipType.Arms;
					break;
				case "Clothes":
					inventory.equipType = EquipType.Clothes;
					break;
				case"Jewelry":
					inventory.equipType = EquipType.Jewelry;
					break;
				case "Shose":
					inventory.equipType = EquipType.Shose;
					break;
				default:
					break;
				}
			}
				if (inventory.inventoryType == InventoryType.Equip) {
					inventory.EquipLevel=int.Parse(itemArray[5]);
					inventory.Damage=int.Parse(itemArray[6]);
					inventory.Hp=int.Parse(itemArray[7]);
					inventory.Power=int.Parse(itemArray[8]);
				}
				inventory.Des=itemArray[9];
            //以 键:inventory.ID  值:inventory 保存到字典中
            inventoryDic.Add(inventory.ID,inventory);
			}
		}
	}


 
  

你可能感兴趣的:(Unity)