保存json读表

2019-05-06  本文已影响0人  沉麟
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using Constants;
using Newtonsoft.Json;
using System;

public class PlaneConfig : BaseConfig
{
    public List<AirPlaneInfo> infos;
    internal override void ReSet(List<string> headContents, List<List<string>> data)
    {
        if (infos == null)
        {
            infos = new List<AirPlaneInfo>();
        }
        infos.Clear();

        foreach (var dataRow in data)
        {
            AirPlaneInfo airPlaneInfo = new AirPlaneInfo {
                id = int.Parse(dataRow[0]),
                name = dataRow[1],
                speed = float.Parse(dataRow[2]),
                atk = int.Parse(dataRow[3]),
                fireRate = float.Parse(dataRow[4]),
                profit = int.Parse(dataRow[5]),
                //planeCost = int.Parse(dataRow[6]),
                imgPlane = dataRow[7],
                uiPlane = dataRow[8],
                prePlane = dataRow[9],
            };

            JsonData jsonData = JsonMapper.ToObject(dataRow[6]);
            foreach (JsonData item in jsonData)
            {
                PlaneCost jo = new PlaneCost()
                {
                    type = int.Parse(item["type"].ToString()),
                    cost = int.Parse(item["cost"].ToString()),
                };

                if (airPlaneInfo.planeCost == null)
                {
                    airPlaneInfo.planeCost = new List<PlaneCost>();
                }
                airPlaneInfo.planeCost.Add(jo);
                //Debug.LogError(jo);
            }
            infos.Add(airPlaneInfo);
        }
    }
    
    public AirPlaneInfo GetAirPlaneInfo(int id)
    {
        AirPlaneInfo airPlaneInfo = infos.Find(item => item.id == id);
        return airPlaneInfo;
    }
}

序列化类

[Serializable]
    public class AirPlaneInfo {
        public int id;
        public string name;
        public float speed;
        public int atk;
        public float fireRate;
        public int profit;
        public List<PlaneCost> planeCost;
        public string imgPlane;
        public string uiPlane;
        public string prePlane;
          
    }

    [Serializable]
    public class PlaneCost
    {
        public int type;
        public int cost;
    }

使用

planeCost.text = info.planeCost[0].cost.ToString();
上一篇下一篇

猜你喜欢

热点阅读