前言
背包系统这个地方坑点还是很多的,照着视频做也费了很多劲.这个地方以后肯定是经常要碰到的,所以学到了什么东西就记录下来吧.
物品信息管理
物品信息管理的一大要求就是利用txt文件储存物品的属性,这些属性在背包系统的管理中非常有用.物品属性填写的格式可以按照下面这个表格:
0
1
2
3
4
5
6
7
id
名称
icon名称
类型(Drug)
加血量
加魔量
出售价
购买价
代码如下:
public class ObjectInfoListManager : MonoBehaviour {
public static ObjectInfoListManager _instance;
public TextAsset objectInfoListText;
public Dictionary objectInfoDictById = new Dictionary();
public Dictionary objectInfoDictByName = new Dictionary();
private void Awake()
{
_instance = this;
ReadInfo();
//print(objectInfoDict.Keys.Count);
}
public ObjectInfo GetInfoById(int id)
{
ObjectInfo info = null;
objectInfoDictById.TryGetValue(id, out info);
return info;
}
public ObjectInfo GetInfoByIconName(string iconName)
{
ObjectInfo info = null;
objectInfoDictByName.TryGetValue(iconName, out info);
return info;
}
public void ReadInfo()
{
string text = objectInfoListText.text;
string[] strArray = text.Split('\n');
foreach(string str in strArray)
{
ObjectInfo info = new ObjectInfo();
ObjectType type = ObjectType.Drug;
string[] proArray = str.Split(',');
int id = int.Parse(proArray[0]);
string name = proArray[1];
string iconName = proArray[2];
string str_type =