Unity 命令行发Android包
创始人
2024-05-28 14:03:53
0

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 names = new List();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,threadingg_bStop = False
class OutputLogThread(threading.Thread):m_logFilePath = ''def run(self):global g_bStopnPosRead = 0fp = Noneprint '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 = Nonefor lines in allLines:print linestime.sleep(0.5)def __init__(self, logPath):threading.Thread.__init__(self)self.m_logFilePath = logPathif __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 ' + logFilePathif os.path.isfile(logFilePath):os.remove(logFilePath)logThread = OutputLogThread(logFilePath)logThread.start()os.system(unityRunParm)g_bStop = TruelogThread.join()

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

相关内容

热门资讯

喜闻乐见,2026年“国补”政... 12月30日,国家发展改革委、财政部印发的《关于2026年实施大规模设备更新和消费品以旧换新政策的通...
女子与表姐夫婚外情获赠三百余万... 一桩发生在海南的民间借贷纠纷,将一段持续二十余年的婚外情推到台前。相关材料显示,男子林森(化名)在婚...
诉讼纠纷频发!中央商场子公司又... 继控股子公司新亚百货面临补缴税款及滞纳金7392万元后,百货零售巨头中央商场(600280.SH)另...
从明天起,楼市迎来两大利好政策... 作者:暴哥 来源:暴财经pro 同志们,2025年要过去了! 今年,各位在股市里应该收获不少,把过去...
国台办回应台湾网红“馆长”大陆... 12月31日,国台办举行例行新闻发布会,国台办发言人张晗就近期两岸热点问题回答记者提问。 有记者提问...
中钢天源:股东中钢热能院907... 雷达财经 文|冯秀语 编|李亦辉 12月30日,中钢天源(证券代码:002057)发布公告称,其控股...
宇树科技因侵权责任纠纷案件被起... 天眼查法律诉讼信息显示,近日,杭州宇树科技股份有限公司及邵某新增1条开庭公告,原告为孙某,涉及侵权责...
今日视点:“制度创新+科技突破... 2025年,A股市场交易活跃度持续提升。截至12月30日,全年累计成交金额达417.8万亿元,同比增...
专业文章丨第二顺位抵押权人实现... 【珠海律师、珠海法律咨询、珠海律师事务所、京师律所、京师珠海律所】 (本文转载自北京市京师郑州律师事...
企业就网络谣言报案并追究法律责... 2025-12-31 09:41:29 作者:狼叫兽 声明指出,目前相关网络平台已对上述不实信息进...