美文网首页
保存json读表

保存json读表

作者: 沉麟 | 来源:发表于2019-05-06 21:02 被阅读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();
    

    相关文章

      网友评论

          本文标题:保存json读表

          本文链接:https://www.haomeiwen.com/subject/dukjoqtx.html