[Unity热更新]tolua# & LuaFramework(五):.proto转换为.lua

这次介绍一下在LuaFramework中,将.proto文件转化为.lua文件的步骤


1.Python的下载与安装

下载:https://www.python.org/downloads/   (推荐选择2.x版本)

安装:http://www.runoob.com/python/python-install.html


2.下载protoc-gen-lua.zip并解压到d盘:http://pan.baidu.com/s/1eSyMki2

3.下载protobuf-2.5.0.zip并解压到d盘:http://pan.baidu.com/s/1nuIA1YT

因为框架的关系要放在d盘,当然也可以放在自定义路径,这样就需要修改框架中的源码,默认就好;如果使用的是VS2013,那么推荐protobuf的版本要在2.5.0以上,否则会报很多错误!


4.找到D:\protobuf-2.5.0\vsprojects,然后双击protobuf.sln打开,菜单栏生成/ 生成解决方案,进行编译,期间会报一些错误:

a.min找不到标识符

解决:在错误的文件里加上#include "minmax.h"

b.如果要将多个 CL.EXE 写入同一个 .PDB 文件,请使用 /FS

解决:

打开项目的“属性页”对话框,选择 C/C++ 文件夹,选择“命令行”属性页,修改“其它选项”属性以包括/FS,然后选择“确定”。


右击项目 --> "属性”
1. “C/C++” --> "常规” -->”调试信息格式” 设置为 “C7 兼容(/Z7)”
2. “C/C++” --> "代码生成” -->”启用字符串池” 设置为 “是(/GF)”


c.cannot open file ' ?.obj'

解决:把debug目录删掉


那么,如无意外,生成时就不会报错了。


5.

命令行cmd,定位到D:\protobuf-2.5.0\python,然后执行python setup.py build,如果成功的话,就会这样:


然后执行python setup.py install,如果成功的话,最后会看到:



6.打开unity工程,先把Assets\LuaFramework\Lua\3rd\pblua中的login_pb.lua移动到别的地方,以便观察结果。找到Packager类中的BuildProtobufFile方法,修改一下路径:

    [MenuItem("LuaFramework/Build Protobuf-lua-gen File")]
    public static void BuildProtobufFile()
    {
        if (!AppConst.ExampleMode)
        {
            Debugger.LogError("若使用编码Protobuf-lua-gen功能,需要自己配置外部环境!!");
            return;
        }
        string dir = AppDataPath + "/LuaFramework/Lua/3rd/pblua";
        //UnityEngine.Debug.Log(dir);
        paths.Clear(); files.Clear(); Recursive(dir);

        string protoc = "d:/protobuf-2.5.0/src/protoc.exe";
        string protoc_gen_dir = "\"d:/protoc-gen-lua/plugin/protoc-gen-lua.bat\"";

        foreach (string f in files)
        {
            string name = Path.GetFileName(f);
            string ext = Path.GetExtension(f);
            //UnityEngine.Debug.Log(name + "   " + ext);
            if (!ext.Equals(".proto")) continue;

            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = protoc;
            info.Arguments = " --lua_out=./ --plugin=protoc-gen-lua=" + protoc_gen_dir + " " + name;
            info.WindowStyle = ProcessWindowStyle.Hidden;
            info.UseShellExecute = true;
            info.WorkingDirectory = dir;
            info.ErrorDialog = true;
            Util.Log(info.FileName + " " + info.Arguments);

            Process pro = Process.Start(info);
            pro.WaitForExit();
        }
        AssetDatabase.Refresh();
    }


关于Process & ProcessStartInfo:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace TestProcess
{
    class Program
    {
        static void Main(string[] args)
        {
            //使用IE浏览器打开百度
            Process process = new Process();
            process.StartInfo.FileName = "iexplore.exe";
            process.StartInfo.Arguments = "http://www.baidu.com";
            process.Start();

            //打开D盘
            ProcessStartInfo processStartInfo = new ProcessStartInfo("");
            processStartInfo.FileName = "explorer.exe";
            processStartInfo.Arguments = @"D:\";
            Process.Start(processStartInfo);

            //string fileName
            Process.Start(@"D:\Program Files\QQ\Bin\QQ.exe");
            //string fileName, string arguments
            Process.Start("explorer.exe", @"D:\Program Files\QQ\gf-config.xml");
        }
    }
}


点击菜单栏LuaFramework/Build Protobuf-lua-gen File,如无意外,应该会看到:


那么把D:\protobuf-2.5.0\vsprojects\Debug中的protoc.exe复制过去即可,再次点击,就会看到Assets\LuaFramework\Lua\3rd\pblua多了一个login_pb.lua,而且跟框架之前那个是一样的!上面的代码中,info.Arguments = " --lua_out=./,意思就是将生成的.lua放到当前目录,而这个当前目录,是根据info.WorkingDirectory而定的。


这个环境搭建花了我一天的时间了,弄得身心疲惫,果然是一个体力活啊!不过总算成功了,付出的没有白费!此时的我,高兴得手舞足蹈啊!

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值