Unity插件学习(九) ------ unity基于Protobuf导出/读取Excel表格数据

24 篇文章 1 订阅
22 篇文章 3 订阅

最近在将本地应用更改成资源打包成AB包放在后端实时下载,看了一段时间底层资源加载的视频,然后看到配置表的集中实现方式 :

1.基于Python的Excel与类的映射(保存成python文件)
2.基于Protobuf的Excel映射成类(二进制映射成类)
3.程序生成xml,策划配置Excel使用vb转成xml,运行时使用的是二进制(但是要改变的话就需要策划和程序一起改变且重新填写)
前两者比较常用,因为比较方便,速度最快的是Protobuf

虽然视频里有第三种实现方式,不过感觉后期有点麻烦,因此想自己实现下第二种实现方式,其实在之前我就已经写了Unity插件学习(一) ------ Protobuf-net插件学习,本文只研究一下Protobuf读取Excel表格数据以及导出Excel表格数据

一.安装环境

1.Mac Python及sublime开发环境安装

2.Mac 安装Protobuf

①.安装brew

ruby -e "$(curl -fsSL  https://raw.githubusercontent.com/Homebrew/install/master/install)"

②.brew install protobuf

brew install protobuf

 但是这安装的是最新版的

dengshunhaodeMacBook-Pro:~ icey$ protoc --version
libprotoc 3.7.0

如果想安装指定版本,首先卸载 :

brew uninstall protobuf

 查看版本:

brew search protobuf

安装指定版本 :

brew reinstall protobuf@版本号

 3.安装xlrd

sudo pip install xlrd

验证安装是否成功:

①.在mac终端输入 python  进入python环境

②.然后输入 import xlrd,不报错说明模块安装成功

 4.下载release源代码 :

下载地址 : https://github.com/protocolbuffers/protobuf/releases

 

二.基于Protobuf导出Excel表格数据

使用xls_deploy_tool.py转化成.proto文件,再通过protogen进行转化成.cs文件,但是这是转化成proto2,如果有知道怎么转protobuf3的大大请教教博主

 

1.生成.data格式的数据文件

首先下载xls_deploy_tool.py : https://github.com/jameyli/tnt/tree/master/python

自己新建一个文件夹,将Python下的两个文件拷贝进来

 启动终端,进入该文件夹

执行命令:

python xls_deploy_tool.py GOODS_INFO xls/goods_info.xls

成功后如下图所示 : 

2.生成.cs文件

protogen github地址 : https://github.com/floatinghotpot/protogen

mono依赖 : https://www.mono-project.com/download/stable/

mono下载完直接安装即可,protogen我是通过命令行安装的过程如下:

①.安装npm

brew install node

②.安装protogen

npm install -g protogen

③.打开生成的.proto,增加syntax

syntax = "proto2";

④.生成.cs文件

protogen -i:tnt_deploy_goods_info.proto -o:output.cs

-I:.proto路径
-o:输出路径

 因为还只是试着研究一下,批处理以后用到了再看

 

三.unity读取数据

这里其实省去了一步,就是根据源码生成.dll文件,因为博主用QFramework框架,里面集成了,不过网上资料也挺多的,看官们自己搜索一下就行啦

①.将生成的.cs与.data文件放入unity工程中

②.读取脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ProtoBuf;
using tnt_deploy;
using System.IO;

public class ReadProtodata : MonoBehaviour {

	// Use this for initialization
	void Start () {
        GOODS_INFO_ARRAY infos = ReadOneDataConfig<GOODS_INFO_ARRAY>("goods_info");
        Debug.Log(infos.items.Count);

        Debug.Log(infos.items[0].goods_id);
    }
	
	// Update is called once per frame
	void Update () {
		
	}
    private T ReadOneDataConfig<T>(string FileName)
    {
        FileStream fileStream;
        fileStream = GetDataFileStream(FileName);
        if (null != fileStream)
        {
            T t = Serializer.Deserialize<T>(fileStream);
            fileStream.Close();
            return t;
        }
        else
        {
            Debug.Log("fileStream为空");
        }

        return default(T);
    }
    private FileStream GetDataFileStream(string fileName)
    {
        string filePath = GetDataConfigPath(fileName);
        if (File.Exists(filePath))
        {
            FileStream fileStream = new FileStream(filePath, FileMode.Open);
            return fileStream;
        }
        else
        {
            Debug.Log("未找到文件");
        }

        return null;
    }
    private string GetDataConfigPath(string fileName)
    {
        return Application.streamingAssetsPath + "/ConfigData/" + fileName + ".data";
    }

}

运行结果 :

 

 

 

参考 :

Mac安装protobuf 流程

《Unity 3D游戏客户端基础框架》protobuf 导excel表格数据

《从零开始搭建游戏服务器》 Protobuf读取Excel表格数据

Unity —— protobuf 导excel表格数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

千喜

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

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

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

打赏作者

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

抵扣说明:

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

余额充值