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
    }
  ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lq1340817945

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值