使用xlua读取XlsxToLua生成的配置文件

发表于2018-08-06
评论0 3.7k浏览
XLua是腾讯开发分享出来的一个开源项目,主要用于Unity 项目的热更新。下面和大家介绍的是如何使用xlua读取XlsxToLua生成的配置文件。

直接上代码:
//LuaDataInfo
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using XLua;
public class TableDataHeroPojo
{
    public int HeroId { get; set; }
    public string Name { get; set; }
    public int Rare { get; set; }
    public int Type { get; set; }
    public int DefaultStar { get; set; }
    public bool IsOpen { get; set; }
    //public LitJson.JsonData Attributes { get; set; }
}
public class LuaDataInfo : MonoBehaviour 
{
    string lua_hero = "DataConfig/Hero";
    void Start()
    {
        LuaEnv luaEnv = new LuaEnv();
        //luaEnv.AddLoader(LoadLuaByName);
        luaEnv.AddLoader(new LuaLoader((ref string filepath) =>
        {
            filepath = string.Format(GameConfig.LuaLoaderPath, Application.streamingAssetsPath, filepath);
            if (File.Exists(filepath))
            {
                return File.ReadAllBytes(filepath);
            }
            else
            {
                return null;
            }
        }));
        string path = string.Format(GameConfig.LuaPath, lua_hero);
        luaEnv.DoString(path);
        LuaTable tab = luaEnv.Global.GetInPath<LuaTable>("Hero");
        LuaTable hero1 = tab.Get<int, LuaTable>(1);
        string Name = hero1.Get<string>("name");
        int heroId = hero1.Get<int>("heroId");
        LuaTable attr = hero1.Get<LuaTable>("attributes");
        string desc = attr.Get<string>("petPhrase");
        Debug.LogError(" heroId = " + heroId + " ,name = " + Name + " ,pet phrase = " + desc);
    }
    public byte[] LoadLuaByName(ref string filepath)
    {
        string tempPath = string.Format(GameConfig.LuaDataConfigPath,filepath);
        if (File.Exists(tempPath))
        {
            return File.ReadAllBytes(tempPath);
        }
        else
        {
            return null;
        }
    }
}

-- heroId                           int                              英雄ID
-- name                             lang                             英雄名称(仅客户端用)
-- rare                             int                              稀有度(11-13)
-- type                             int                              英雄职业(1:法师,2:战士,3:牧师,4:勇士)
-- defaultStar                      int                              英雄初始星数
-- isOpen                           bool                             当前是否在游戏中开放(即可在英雄图鉴看到,可以被抽卡抽到)
-- attributes                       json                             战斗属性
Hero =
{
	[1] = {
		heroId = 1,
		name = "英雄法师",
		rare = 11,
		type = 1,
		defaultStar = 1,
		isOpen = true,
		attributes = {
			attack = {
				physical = 20,
				magic = 100,
				canCrit = true,
				hitRate = 0.9,
				ult = {
					[1] = {
						name = "CoupDeGrace",
						params = {
							[1] = "circle",
							[2] = 1,
							[3] = 0,
							[4] = true,
						},
						cd = 5,
					},
				},
			},
			defence = {
				physical = 10,
				magic = 60,
			},
			hp = 200,
			modelSize = {
				[1] = 4,
				[2] = 5,
				[3] = 10.5,
			},
			petPhrase = "I will kill you!",
		},
	},
	[2] = {
		heroId = 2,
		name = "英雄战士",
		rare = 11,
		type = 2,
		defaultStar = 1,
		isOpen = true,
		attributes = nil,
	},
	[3] = {
		heroId = 3,
		name = "英雄牧师",
		rare = 11,
		type = 3,
		defaultStar = 1,
		isOpen = false,
		attributes = nil,
	},
	[4] = {
		heroId = 4,
		name = "英雄勇士",
		rare = 11,
		type = 4,
		defaultStar = 1,
		isOpen = false,
		attributes = {
			attack = {
				physical = 140,
				magic = 0,
				canCrit = true,
				hitRate = 0.9,
				ult = {
					[1] = {
						name = "CoupDeGrace",
						params = {
							[1] = "sector",
							[2] = 150,
							[3] = 0,
							[4] = true,
						},
						cd = 5,
					},
				},
			},
			defence = {
				physical = 40,
				magic = 0,
			},
			hp = 150,
			modelSize = {
				[1] = 3.5,
				[2] = 4,
				[3] = 8,
			},
			petPhrase = "Death to all who oppose me!",
		},
	},
}

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签: