unity学习中经常要碰到的几种数据结构

时间:2023-03-09 08:22:13
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格式的记事本,存储物品信息,如下图

 ,红药,HealthPotion,Drug,,,
,蓝药,ManaPotion,Drug,,,
,红蓝恢复,CrystallineFlask,Drug,,,
,蛋帽,,Equip,,,,Head,
,沃格勒特的巫师帽,Wooglet's Witchcap,Equip,30,30,0,Head,300
,灭世者之帽,Rabadon's Deathcap,Equip,60,60,0,Head,600
,长剑,Long Sword,Equip,,,,RightHand,
,暴风大剑,B.F.Sword,Equip,,,,RightHand,
,魔宗利刃,Manamune,Equip,,,,RightHand,
,无尽之刃,Infinity Edge,Equip,,,,RightHand,
,多兰盾,Doran's Shield,Equip,0,10,0,LeftHand,100
,冰霜之心,Glacial Shroud,Equip,,,,LeftHand,
,兰兆之盾,Randuin's Omen,Equip,0,60,0,LeftHand,800
,布甲,Cloth Armor,Equip,,,,Armor,
,守望者铠甲,Warden's Mail,Equip,0,30,0,Armor,300
,钢铁烈焰之匣,Locket of the Iron Solari,Equip,,,,Armor,
,贤者之石,Philosopher's Stone,Equip,5,5,5,Common,150
,水银饰带,Quicksilver Sash,Equip,,,,Common,
,女妖面纱,Banshee's Veil,Equip,15,15,15,Common,600
,草鞋,Boots of Speed,Equip,,,,Shoe,
,布甲鞋,Ninja Tabi,Equip,,,,Shoe,
,家园卫士,Ninja Homeguard,Equip,,,,Shoe,

2.以上文本对应属性如下信息

unity学习中经常要碰到的几种数据结构

3.具体存储信息脚本

 using UnityEngine;
using System.Collections;
using System.Collections.Generic;//新增Collections.Generic这个命名空间 public class ObjectsInfo : MonoBehaviour {
public TextAsset objectInfoText;//把TXT文本放进来
public Dictionary<int,ObjectInfo> ObjectInfoDic = new Dictionary<int,ObjectInfo > ();
public static ObjectsInfo _instance;//做成单例模式,方便其他脚本的引用
void Awake () {
_instance = this;
readInfo ();//初始化读取内部数据信息
}
void readInfo(){
string text = objectInfoText.text;
//用string语法中的.Split方法来分本记事本的字符串
string[] StrArray=text.Split('\n');
foreach (string array in StrArray) {
string[] arr=array.Split(',');
ObjectInfo info = new ObjectInfo ();
info.id=int.Parse(arr[]);
info.name=arr[];
info.icon_name=arr[];
ObjectType type=ObjectType.Drug;
string kind=arr[];
//用switch语法来创建类型
switch(kind){
case "Drug":
type=ObjectType.Drug;
break;
case "Equip":
type=ObjectType.Equip;
break;
}
//根据类型在字典中存储相关数据
info.type=type;
if(type==ObjectType.Drug){
info.hp=int.Parse( arr[]);
info.mp=int.Parse(arr[]);
info.price=int.Parse(arr[]);
}
else if(type==ObjectType.Equip){
info.attack=int.Parse(arr[]);
info.defence=int.Parse(arr[]);
info.speed=int.Parse(arr[]);
string str=arr[];
switch(str){
case "Head":
info.dressType=DressType.Head;
break;
case "RightHand":
info.dressType=DressType.RightHand;
break;
case "LeftHand":
info.dressType=DressType.LeftHand;
break;
case "Armor":
info.dressType=DressType.Armor;
break;
case "Common":
info.dressType=DressType.Common;
break;
case "Shoe":
info.dressType=DressType.Shoe;
break;
}
info.price=int.Parse(arr[]);
} ObjectInfoDic.Add(info.id,info);//切记最后添加这个语句存入数据 }
}
//根据ID查找到对应的相关信息
public ObjectInfo GetInfoByID(int id){
ObjectInfo info = null;
ObjectInfoDic.TryGetValue (id, out info);
return info;
} }
//两种物品类型,一种药品,一种装备
public enum ObjectType{
Drug,
Equip
}
//装备的穿戴类型有五种
public enum DressType{
Head,
RightHand,
LeftHand,
Armor,
Common,
Shoe
}
93 //新建一个类,用来存放物品信息,包括编号,物品名字,物品图标名字,物品类型(药品,装备,加血,加蓝,购买价钱
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; 103 //装备类型,新增一个穿戴部位类型,攻击力,防御力和速度
public DressType dressType;
public int attack;
public int defence;
public int speed;
}