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
可以试试

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值