通过通信方式调用bat命令unity打包exe

一个模拟器项目,cs模式,客户端通过socket发送指令让管理端执行unity打包的功能 。

首先创建一个批处理.bat文件,用来做命令执行:

echo off
@set outPath="你要输出的绝对路径"
@set unityPath="unityEditor下的可执行文件目录"
@set scenes="要打包场景的名称,可以是多个,如ascene,bscene"
echo "packaging......"
%unityPath% -quit -batchmode -executeMethod BuildEditor.BuildPacage -logFile d:\1\debug.log -projectPath “unity工程文件路径” %outpath% %scenes%

Unity工程上需要的步骤,创建一个脚本BuildEditor,放在需要打包的工程的Editor文件夹下。具体代码如下:

public Class BuildEditor:Ediotr{

    private static string[] s;    //定义一个字符串数组,存放命令行字符串
    //打包接口
    static void BuildPackage()
    {
        s=System.Environment.GetCommandLineArgs();    //返回包含当前进程的命令行参数的字符串数组
        BuildForStandaloneWindows();    
    }
    //打包windows端
    [MenuItem("a/b")]
    static void BuildForStandaloneWindows()
    {
        string[] levels=GetSceneNames();    //需要打包的场景名称
        for(int i=0; i<levels.Length; i++)    //循环存入多个场景
        {
            levels[i] = "Assets\\" + levels[i] +".unity";    //
        }
        string path = GetExportPath();    //获取打包输出地址
        var st = BuildPipeline.BuildPlayer(levels,@path,BuildTarget.standaloneWindows,BuildOptions.None);    // 打包接口,第一个参数是需要打包的场景,第二个是打包输出路径,第三个是打包平台,第四个忘了
    }
    //打包输出的路径“例如,D:/Unity/abc.exe”
    private static string GetExportPath()
    {
        string path = s[s.Length - 2];
        string name = "\\你想打包exe的名称";
        string exepath = @path + name;
        return exepath;
    }
    //获取想要打包的场景名称
    privite static string[] GetSceneNames()
    {
        string names = s[s.Length - 1];    //根据.bat命令行传入参数
        string[] sceneNames = names.Split(new char[]{','});    //将场景名数组分隔开,例如"aScene,bScene,cScene",将其分割后填入到sceneNames数组中
        for(int i = 0; i< sceneNames.Length; i++)
        {
            return sceneNames;
        }
    }
}

第三部开启一个通信工具,用来接受管理端传过来的命令,就是一个简单的通信工具,我就不具体写出了,简单的列下调用的接口

//通过管理端传递命令调用执行接口  
bool RunBat()
{
    process.StartInfo.WorkingDirectory="bat文件路径";
    process.StartInfo.FileName=".bat文件名称";
    process.StartInfo.UseShellExcute =false;
    process.StartInfo.RedirectStandardOutput=true;
    process.start();
    string s =process.StandardOutput.ReadToEnd();
    process.WaitForExit();
    string[] strs = s.Split("\");
    bool isDone=false;
    for(int i=strs.Length-1;i>=0;i--)
    {
        if(strs[i]=="\r\n")
        continue;
        if(strs[i]==build package done)
        return true;
        else
        return false;
    }
    returnn false;
}

猜你喜欢

转载自blog.csdn.net/qq_40097668/article/details/91458922
今日推荐