Now start processing the game's backpack system.
The first step in handling the backpack system is to read the item information and read the items we have designed into memory to implement the call.
Idea: ① set up txt text, design item information; ② establishes the Objectsinfo class to read the information in the TXT text in the class.
The script is as follows:
① Creating txt text
Ordinal name Icon name Type HP MP Price_sell.price_buy
1001, potion of Life, icon_01,drug,100,0, 50,100
1002, Magic Potion, icon_02,drug,0,100, 60,120
② establishing Objectsinfo Class
Class Objectsinfo
{
public static Objectsinfo _intance;
Public Textasset Objecttext;
Public Dictionary <int,ObjectInfo> objectinfodict = new dictionary<int,objectinfo> ();
void Awake ()
{
_intance = this;
}
Public Objectinfo Getobjectinfobyid (int id)
{
Objectinfo = null;
Objectinfodict.trygetvalue (id,out info);
return info;
}
public void Readinfo ()
{
string text = Objecttext.text;
string[] Strarray = text. Split (' \ n ');
foreach (String str in strarray)
{
Objectinfo info = new Objectinfo ();
stringp[] Strpro = str. Split (', ');
int id = Int. Parse (Strpro[0]);
String name = Strpro[1];
String icon_name = Strpro[2];
String strtype = Strpro[3];
Info.id = ID; Info.name = name; Info.icon_name = Icon_name;
ObjectType type = Objecttype.drug;
Switch (strtype)
{
Case "Drug":
Type = Object.drug;
Break
Case "Equip":
Type = Object.equip;
Break
Case "Mat":
Type = Object.mat;
Break
}
Info.type = type;
if (type = = Object.drug)
{
int hp = Strpro[4];
In MP = strpro[5];
int price_sell = strpro[6];
int price_buy = strpro[7];
INFO.HP = hp;info.mp =mp;info.price_sell = Price_sell; Info.price_buy = Price_buy;
}
Objectinfodict.add (Id,info);
}
}
Public EUNM Objecttype{drug,equip,mat}
Public Class Objectinfo
{
public int id,hp,mp,price_sell,price_buy;
public string Name,icon_name,type;
}
The above will realize the storage of the item information into memory space, this day summary ends.
UNITY3D-RPG Project Study notes (ix)