Unity - A计划(一年有效期) 扫二维码继续学习 二维码时效为半小时

(61评价)
价格: 2208.00元
《黑暗之光》第40课时,在程序中读取文本,将物品信息读取在内存中。
开发者微光发起了问答2018-02-21
8
回复
871
浏览

出现BUG.

FormatException: Input string was not in the correct format
System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Int32.cs:629)
ObjectsInfo.ReadInfo () (at Assets/ObjectsInfo.cs:56)
ObjectsInfo.Awake () (at Assets/ObjectsInfo.cs:14)


已检查TXT文本是否有空格,分隔符号是否正确

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectsInfo : MonoBehaviour {

    public static ObjectsInfo _instance;

    private Dictionary<int, ObjectInfo> objectInfoDict = new Dictionary<int, ObjectInfo>();
    public TextAsset ObjectsInfoListText;

    void Awake() {
        _instance = this;
        ReadInfo();
        print(objectInfoDict.Keys.Count);
    }

    public ObjectInfo GetObjectsInfoBuId(int id)
    {
        ObjectInfo info = null;
        objectInfoDict.TryGetValue(id, out info);
        return info;
    }

    

    public enum ObjectType
    {
        Drug,
        Equip,
        Mat,
    }
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_sell;
        public int price_buy;

    }
    private void ReadInfo()
    {
        string text = ObjectsInfoListText.text;//获取文本内容
        string[] strArray = text.Split('\n');//以换行符为界,存储到字符串中

        foreach (string str in strArray)//遍历数组
        {
      
            string[] proArray = str.Split(',');//以点符号拆分
          
            ObjectInfo info = new ObjectInfo();
             int  id = int.Parse(proArray[0]);
            string name = proArray[1];
            string icon_name = proArray[2];
            string str_type = proArray[3];
            ObjectType type = ObjectType.Drug;

            switch (str_type)
            {
                case "Drug":
                    type = ObjectType.Drug;
                    break;
                case "Equip":
                    type = ObjectType.Equip;
                    break;
                case "Mat":
                    type = ObjectType.Mat;
                    break;
            }
            info.id = id;
            info.name = name;
            info.icon_name = icon_name;
            info.type = type;

            if (type==ObjectType.Drug)
            {
                int hp = int.Parse(proArray[4]);
                int mp = int.Parse(proArray[5]);
                int price_sell = int.Parse(proArray[6]);
                int price_buy = int.Parse(proArray[7]);


                info.hp = hp;
                info.mp = mp;
                info.price_buy = price_buy;
                info.price_sell = price_sell;
            }
            objectInfoDict.Add(id, info);//添加到字典中,id为key,可以很方便的根据id查找到这个物品的信息
        }

    }
}

 

所有回复
发表回复
你还没有登录,请先 登录或 注册!