unity学习中经常要碰到的几种数据结构
常碰到的几种数据结构:1.Array,2.ArrayList,3.List<T>,4.LinkedList<T>,5.Queue<T>,6.Stack<T>,7.Dictionary<K,T>
在我的DEMO中运用到了List<T>,ArrayList和Dictionary<K,T>,其中展示下我运用字典来存储一个游戏中技能和物品信息的过程
以存放物品信息举例
1.在Assest件下创建一个TXT格式的记事本,存储物品信息,如下图
1 1001,红药,HealthPotion,Drug,50,0,50 2 1002,蓝药,ManaPotion,Drug,0,50,50 3 1003,红蓝恢复,CrystallineFlask,Drug,25,25,35 4 2001,蛋帽,0,Equip,10,10,0,Head,100 5 2002,沃格勒特的巫师帽,Wooglet's Witchcap,Equip,30,30,0,Head,300 6 2003,灭世者之帽,Rabadon's Deathcap,Equip,60,60,0,Head,600 7 2004,长剑,Long Sword,Equip,10,0,0,RightHand,100 8 2005,暴风大剑,B.F.Sword,Equip,30,0,0,RightHand,300 9 2006,魔宗利刃,Manamune,Equip,60,0,0,RightHand,600 10 2007,无尽之刃,Infinity Edge,Equip,200,0,0,RightHand,4000 11 2008,多兰盾,Doran's Shield,Equip,0,10,0,LeftHand,100 12 2009,冰霜之心,Glacial Shroud,Equip,0,30,0,LeftHand,300 13 2010,兰兆之盾,Randuin's Omen,Equip,0,60,0,LeftHand,800 14 2011,布甲,Cloth Armor,Equip,0,10,0,Armor,100 15 2012,守望者铠甲,Warden's Mail,Equip,0,30,0,Armor,300 16 2013,钢铁烈焰之匣,Locket of the Iron Solari,Equip,0,60,0,Armor,600 17 2014,贤者之石,Philosopher's Stone,Equip,5,5,5,Common,150 18 2015,水银饰带,Quicksilver Sash,Equip,10,10,10,Common,400 19 2016,女妖面纱,Banshee's Veil,Equip,15,15,15,Common,600 20 2017,草鞋,Boots of Speed,Equip,0,0,1,Shoe,100 21 2018,布甲鞋,Ninja Tabi,Equip,0,10,2,Shoe,300 22 2019,家园卫士,Ninja Homeguard,Equip,0,15,3,Shoe,600
2.以上文本对应属性如下信息
3.具体存储信息脚本
1 using UnityEngine; 2 using System.Collections; 3 using System.Collections.Generic;//新增Collections.Generic这个命名空间 4 5 public class ObjectsInfo : MonoBehaviour { 6 public TextAsset objectInfoText;//把TXT文本放进来 7 public Dictionary<int,ObjectInfo> ObjectInfoDic = new Dictionary<int,ObjectInfo > (); 8 public static ObjectsInfo _instance;//做成单例模式,方便其他脚本的引用 9 void Awake () { 10 _instance = this; 11 readInfo ();//初始化读取内部数据信息 12 } 13 void readInfo(){ 14 string text = objectInfoText.text; 15 //用string语法中的.Split方法来分本记事本的字符串 16 string[] StrArray=text.Split('\n'); 17 foreach (string array in StrArray) { 18 string[] arr=array.Split(','); 19 ObjectInfo info = new ObjectInfo (); 20 info.id=int.Parse(arr[0]); 21 info.name=arr[1]; 22 info.icon_name=arr[2]; 23 ObjectType type=ObjectType.Drug; 24 string kind=arr[3];
//用switch语法来创建类型 25 switch(kind){ 26 case "Drug": 27 type=ObjectType.Drug; 28 break; 29 case "Equip": 30 type=ObjectType.Equip; 31 break; 32 }
//根据类型在字典中存储相关数据 33 info.type=type; 34 if(type==ObjectType.Drug){ 35 info.hp=int.Parse( arr[4]); 36 info.mp=int.Parse(arr[5]); 37 info.price=int.Parse(arr[6]); 38 } 39 else if(type==ObjectType.Equip){ 40 info.attack=int.Parse(arr[4]); 41 info.defence=int.Parse(arr[5]); 42 info.speed=int.Parse(arr[6]); 43 string str=arr[7]; 44 switch(str){ 45 case "Head": 46 info.dressType=DressType.Head; 47 break; 48 case "RightHand": 49 info.dressType=DressType.RightHand; 50 break; 51 case "LeftHand": 52 info.dressType=DressType.LeftHand; 53 break; 54 case "Armor": 55 info.dressType=DressType.Armor; 56 break; 57 case "Common": 58 info.dressType=DressType.Common; 59 break; 60 case "Shoe": 61 info.dressType=DressType.Shoe; 62 break; 63 } 64 info.price=int.Parse(arr[8]); 65 } 66 67 ObjectInfoDic.Add(info.id,info);//切记最后添加这个语句存入数据 68 69 } 70 } 71 //根据ID查找到对应的相关信息 72 public ObjectInfo GetInfoByID(int id){ 73 ObjectInfo info = null; 74 ObjectInfoDic.TryGetValue (id, out info); 75 return info; 76 } 77 78 } 79 //两种物品类型,一种药品,一种装备 80 public enum ObjectType{ 81 Drug, 82 Equip 83 } 84 //装备的穿戴类型有五种 85 public enum DressType{ 86 Head, 87 RightHand, 88 LeftHand, 89 Armor, 90 Common, 91 Shoe 92 } 93 //新建一个类,用来存放物品信息,包括编号,物品名字,物品图标名字,物品类型(药品,装备,加血,加蓝,购买价钱 94 public class ObjectInfo{ 95 public int id; 96 public string name; 97 public string icon_name; 98 public ObjectType type; 99 public int hp; 100 public int mp; 101 public int price; 102 103 //装备类型,新增一个穿戴部位类型,攻击力,防御力和速度 104 public DressType dressType; 105 public int attack; 106 public int defence; 107 public int speed; 108 }