Unity 命令行发Android包

unity.exe 只允许存在一个 如果开了ide 或者之前的没关掉 就不能运行了

C:
cd C:\Program Files\Unity\Editor\2021.3.6f1c1\Editor\
Unity.exe ^
-quit ^
-batchmode ^
-projectPath E:\puerts\UnityJenkins ^
-executeMethod Main.Build

C#代码放到任意Editor目录里

using System;
using UnityEditor;
using UnityEngine;
using UnityEditor.Build.Reporting;
class Main
{
    
    
    static void Build()
    {
    
    
        BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
        buildPlayerOptions.scenes = new[] {
    
     "Assets/Scenes/SampleScene.unity" };
        buildPlayerOptions.locationPathName = "AndroidBuild.apk";
        buildPlayerOptions.target = BuildTarget.Android;
        buildPlayerOptions.options = BuildOptions.None;

        BuildReport report = BuildPipeline.BuildPlayer(buildPlayerOptions);
        BuildSummary summary = report.summary;

        if (summary.result == BuildResult.Succeeded)
        {
    
    
            Debug.Log("Build succeeded: " + summary.totalSize + " bytes");
        }

        if (summary.result == BuildResult.Failed)
        {
    
    
            Debug.Log("Build failed");
        }
    }
    
	static string[] GetBuildScenes()
    {
    
    
        List<string> names = new List<string>();
        foreach (EditorBuildSettingsScene e in EditorBuildSettings.scenes)
        {
    
    
            if (e == null)
                continue;
            if (e.enabled)
                names.Add(e.path);
        }
        return names.ToArray();
    }
}

也可以在命令行里加参数
c#读 String[] arguments = Environment.GetCommandLineArgs();
c#里输出的内容 都在Editor.log里
地址是
C:\Users\Administrator\AppData\Local\Unity\Editor
可以在ide的这里打开
里面有输出堆栈和内容方便调试
在这里插入图片描述
日志讲解
https://docs.unity3d.com/cn/current/Manual/LogFiles.html

用 jenkins 发布

第一种方法

可以安装 unity的插件
在这里插入图片描述
路径里写到版本号就可以了 插件会自己进去读unity.exe

增加步骤
在这里插入图片描述
在这里插入图片描述
这样就可以了
优点是打印到 Editor.log 里的内容 会直接输出到 jenkins 里,方便查看。
缺点是 不能使用 jenkins 里的自定义参数

第二种方法

自己写批处理
在这里插入图片描述
更灵活,符合要求。但是不能打印输出,遇到问题了看不到。
需要自己再写一个脚本
名字自己起 比如a.py 放到unity.exe平级目录
然后
在这里插入图片描述

import os,sys,string,datetime,time,threading
 
g_bStop = False
class OutputLogThread(threading.Thread):
    m_logFilePath = ''
    def run(self):
        global g_bStop
        nPosRead = 0
        fp = None
        print 'OutputLogThread Start'
        while g_bStop == False:
            if os.path.isfile(self.m_logFilePath):
                if fp == None:
                    fp = open(self.m_logFilePath, 'r')
 
            if fp != None:
                fp.seek(nPosRead)
                allLines = fp.readlines()
                nPosRead = fp.tell()
                fp.close()
                fp = None
                for lines in allLines:
                    print lines
            time.sleep(0.5)
 
    def __init__(self, logPath):
        threading.Thread.__init__(self)
        self.m_logFilePath = logPath
 
if __name__ == '__main__':
	if len(sys.argv) < 2:
		print 'not find unity path'
		sys.exit(-1)
	logFilePath = 'editor.txt'
	unityRunParm = ''
	for i in range(len(sys.argv)):
		if i > 0:
			unityRunParm += ' ' + sys.argv[i]
	unityRunParm += ' -logfile ' + logFilePath
	if os.path.isfile(logFilePath):
		os.remove(logFilePath)
	logThread = OutputLogThread(logFilePath)
	logThread.start()
	os.system(unityRunParm)
	g_bStop = True
	logThread.join()

也有一个工具
https://github.com/mr-kelly/unity_realtime_log
可以试试

猜你喜欢

转载自blog.csdn.net/qq_38913715/article/details/129304711