Unity学习第一篇 之 将Json转为C#类的方法(持续更新)

根据sikiA计划课程学习

定义一个和Json对应的接口
AllEnemyData.cs

using System;

public enum EnemyType
{
    Normal,
    Elites,
    Boss,
    Item
}

public class AllEnemyData
{
    public EnemyData[] Boss;
    public EnemyData[] Elites;
    public EnemyData[] Normal;
	
	public EnemyData[] GetData(EnemyType type)
    {
        switch (type)
        {
            case EnemyType.Normal:
                return Normal;
            case EnemyType.Elites:
                return Elites;
            case EnemyType.Boss:
                return Boss;
            default:
                return null;
        }
    }
}

public class EnemyData
{
    public int id;
    public int attack;
    public double attackTime;
    public double fireRate;
    public int life;
    public double speed;
    public int trajectoryID;
}

读取json表后使用

	private AllEnemyData _allEnemyData;
    private EnemyData _enemyData;
    private ResourcesLoader _loader;

    //
    private void Start()
    {
        string _path = Application.streamingAssetsPath + "/Config/EnemyConfig.json";
        //读取json的方式有很多,该方法后续会更新
        _loader = new ResourcesLoader();
        _loader.LoadConfig(_path, (value) =>
        {
            var json = (string)value;
            Debug.Log(json);
            _allEnemyData = JsonMapper.ToObject<AllEnemyData>(json);

            Callback();
        });
    }
    //回调函数
    private void Callback()
    {
    	int _id = 3;
        var Type = EnemyType.Normal;
        if (_allEnemyData == null)
            return;
        var allData = _allEnemyData.GetData(Type);
        _enemyData = allData.FirstOrDefault(u => u.id == _id);
        if (_enemyData == null)
        {
            Debug.LogError("不存在ID为" + _id + "的敌方单位数据,type:" + Type);
        }
        else
        {
            Debug.Log("存在ID为" + _id + "的敌方单位数据,_enemyData:" + _enemyData.life);
        }
    }

相关的Json数据
EnemyConfig.json


{
  "Normal": [
    {
      "id": 0,
      "attackTime":1,
      "attack": 0,
      "fireRate": 0,
      "life": 100,
      "speed": 1,
      "trajectoryID": -1
    },
    {
      "id": 1,
      "attackTime":1,
      "attack": 0,
      "fireRate": 0,
      "life": 120,
      "speed": 1,
      "trajectoryID": -1
    },
    {
      "id": 2,
      "attackTime":1,
      "attack": 10,
      "fireRate": 1,
      "life": 140,
      "speed": 1,
      "trajectoryID": -1
    },
    {
      "id": 3,
      "attackTime":1,
      "attack": 12,
      "fireRate": 0.8,
      "life": 200,
      "speed": 1,
      "trajectoryID": -1
    },
    {
      "id": 4,
      "attackTime":1,
      "attack": 14,
      "fireRate": 0.8,
      "life": 220,
      "speed": 1,
      "trajectoryID": -1
    },
    {
      "id": 5,
      "attackTime":1,
      "attack": 20,
      "fireRate": 0.7,
      "life": 240,
      "speed": 1,
      "trajectoryID": -1
    }
  ],
  "Elites":[
    {
      "id": 0,
      "attackTime":1,
      "attack": 30,
      "fireRate": 0.8,
      "life": 400,
      "speed": 1,
      "trajectoryID": -1
    },
    {
      "id": 1,
      "attackTime":1,
      "attack": 40,
      "fireRate": 0.8,
      "life": 500,
      "speed": 1,
      "trajectoryID": -1
    },
    {
      "id": 2,
      "attackTime":1,
      "attack": 50,
      "fireRate": 0.8,
      "life": 600,
      "speed": 1,
      "trajectoryID": -1
    }
  ],
  "Boss":[
    {
      "id": 0,
      "attackTime":1,
      "attack": 0,
      "fireRate": 0,
      "life": 100,
      "speed": 1,
      "trajectoryID": -1
    },
    {
      "id": 1,
      "attackTime":1,
      "attack": 0,
      "fireRate": 0,
      "life": 100,
      "speed": 1,
      "trajectoryID": -1
    }
  ]
}
发布了16 篇原创文章 · 获赞 12 · 访问量 197

猜你喜欢

转载自blog.csdn.net/lq1340817945/article/details/105143396