码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3d - RPG项目学习笔记(九)

时间:2016-08-28 18:14:00      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

现在开始处理游戏的背包系统。

处理背包系统的第一步是读取物品信息,即将我们设计好的物品读取到内存中,实现调用。

思路:①建立txt文本,设计物品信息;②建立ObjectsInfo类,在类中读取txt文本中的信息。

脚本如下:

①建立txt文本

序号      名称      icon名称    类型  hp mp price_sell.price_buy

1001,生命药水,icon_01,Drug,100,0, 50,100

1002,魔法药水,icon_02,Drug,0,100, 60,120

②建立ObjectsInfo类

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;

}

以上便实现了物品信息存储到内存空间,本日总结到此为止。

Unity3d - RPG项目学习笔记(九)

标签:

原文地址:http://www.cnblogs.com/yanbenxin/p/5815493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!