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

(61评价)
价格: 2165.00元
黑暗之光_40_在程序中读取文本,将物品信息读取到内存中
a56085564发起了问答2018-07-17
4
回复
862
浏览

运行游戏后出现这个错误应该怎么解决

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)
GameInfo.ReadInfo () (at Assets/GameInfo.cs:39)
GameInfo.Awake () (at Assets/GameInfo.cs:16)
代码里39行和16行都检查过没发现格式错误

所有回复
  • siki 2018-07-19

    这个意思是转型的时候出错,就是应该是一个数字,但是给了不是一个数字,就转型失败

    检查下txt里面是不是有空白行,在末尾

    检查数据是否有出错的

    可以在报错行输出一下要转型的数据是什么再判断是什么错误

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • a56085564 2018-07-19

    按照老师说的还是没发现错误,下面是源码,报错的地方是在

    int id = int.Parse(proArray[0]);这行

    public void ReadInfo()
        {
            string text = objectInfoTexte.text;
            string[] strArray = text.Split('\n');
            foreach (var str in strArray)
            {
                string[] proArray = str.Split(',');
                ObjectsInfo objectsInfo = new ObjectsInfo();
                //获得数据
                int id = int.Parse(proArray[0]);
                string name = proArray[1];
                string icon_name = proArray[2];
                string str_type = proArray[3];
                ObjectType thisObject = ObjectType.Drag;
                switch (str_type)
                {
                    case "Drag":
                        thisObject = ObjectType.Drag;
                        break;
                    case "Equip":
                        thisObject = ObjectType.Equip;
                        break;
                    case "Mat":
                        thisObject = ObjectType.Mat;
                        break;
                }
    
                //保存数据
                objectsInfo.id = id;
                objectsInfo.name = name;
                objectsInfo.icon_Name = icon_name;
                objectsInfo.type = thisObject;
                if (thisObject == ObjectType.Drag)
                {
                    //获得数据
                    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]);
                    //保存数据
                    objectsInfo.hp = hp;
                    objectsInfo.mp = mp;
                    objectsInfo.price_Sell = price_sell;
                    objectsInfo.price_Bug = price_buy;
                }
                //数据保存在字典中
                objectsInfoByDict.Add(id,objectsInfo);//Key为id,可以很方便地通过id查找到物品
            }
        }

     

    这是存储物品属性的txt文件

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • siki 2018-07-19

    看数据是没有问题的

    可能是你的末尾多了一个空白行

    检查下,末尾是否可以鼠标点击到,点击到说明有空白行

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • hzxslyk 2018-12-28

    文本文档保存的时候把编码换成UTF-8就可以了

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!