新建一个文件没有后缀然后输入如下内容
#!/usr/bin/env sh
/Applications/Unity/Hub/Editor/2020.1.5f1c1/Unity.app/Contents/MacOS/Unity -quit -batchmode -projectPath /Users/zyt/Test -executeMethod Test.BuildEditor.BuildApp
cd /Users/zyt/Test/Xcode/unity-xcode
xcodebuild clean -project Unity-iPhone.xcodeproj -alltargets
xcodebuild -project Unity-iPhone.xcodeproj -scheme Unity-iPhone -destination generic/platform=ios archive -archivePath /Users/zyt/Desktop/Ipa/Unity-iPhone.xcarchive
xcodebuild -exportArchive -archivePath /Users/zyt/Desktop/hoc/Unity-iPhone.xcarchive -exportPath /Users/zyt/Desktop/hoc/test -exportOptionsPlist /Users/zyt/Test/Xcode/unity-xcode/Info.plist
该行命令就是MacOS Unity app的路径 根据自身版本进行更改
该行命令是Unity的项目工程目录 Assets级别
该行命令 Test是命名空间有则写 没有则去掉 BuildEditor是脚本名字 BuildApp是函数名字 由该脚本进行Unity 资源打包以及自动化出XCode工程
该行命令是进入到Xcode工目录
该行命令清除掉Xcode的缓存
xcodebuild -project Unity-iPhone.xcodeproj -scheme Unity-iPhone -destination generic/platform=ios archive -archivePath /Users/zyt/Desktop/Ipa/Unity-iPhone.xcarchive
该行命令是导出Archive缓存文件
Unity-iPhone.xcodeproj
就是Xcode项目的名称
-scheme Unity-iPhone
就是打包的场景
-archivePath /Users/zyt/Desktop/Ipa/Unity-iPhone.xcarchive
就是Archive缓存文件存放的路径
xcodebuild -exportArchive -archivePath /Users/zyt/Desktop/hoc/Unity-iPhone.xcarchive -exportPath /Users/zyt/Desktop/hoc/test -exportOptionsPlist /Users/zyt/Test/Xcode/unity-xcode/Info.plist
该行命令是导出Ipa文件
-archivePath /Users/zyt/Desktop/hoc/Unity-iPhone.xcarchive
这个就是刚刚导出的Archive文件的路径
-exportPath /Users/zyt/Desktop/hoc/test
这个就是导出的Ipa存放目录 没有会新建
-exportOptionsPlist /Users/zyt/Test/Xcode/unity-xcode/Info.plist
这个就是Xcode里面的.plist文件 填入路径即可
Unity脚本如下
using System;
using System.IO;
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;
namespace Test
{
public class BuildEditor
{
[MenuItem("Build/Build")]
public static void BuildApp()
{
BuildGameRes();
#if UNITY_ANDROID
Build_Apk();
#else
Build_Ipa();
#endif
}
private static void BuildGameRes()
{
//写自己的资源打包
}
private static void Build_Apk()
{
PlayerSettings.Android.keystoreName = Application.dataPath.Replace("/Assets", "") + "/test.keystore";
PlayerSettings.Android.keystorePass = "123456";
PlayerSettings.Android.keyaliasName = "test";
PlayerSettings.Android.keyaliasPass = "123456";
PlayerSettings.Android.useAPKExpansionFiles = false;
PlayerSettings.bundleVersion = "1";
PlayerSettings.Android.bundleVersionCode = 1;
PlayerSettings.applicationIdentifier = "com.test.test01";
PlayerSettings.Android.targetArchitectures = AndroidArchitecture.All;
string time = DateTime.Now.ToString("yyyyMMddHHmmss");
string[] scenes = {
@"Assets/Res/Resources/scene/test.unity",
};
string fileName = "test";
fileName = Application.dataPath.Replace("/Assets", "") + "/App/" + fileName;
string dir = Path.GetDirectoryName(fileName);
if (Directory.Exists(dir))
Directory.Delete(dir, true);
Directory.CreateDirectory(dir);
BuildOptions buildOptions = BuildOptions.CompressWithLz4;
BuildReport report = BuildPipeline.BuildPlayer(scenes, fileName, BuildTarget.Android, buildOptions);
if (report.summary.result != BuildResult.Succeeded)
{
Debug.LogError("build fail - " + fileName);
return;
}
AssetDatabase.Refresh();
string path = Application.dataPath.Replace("/Assets", "") + "/App";
path = path.Replace("/", @"\");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "explorer.exe";
p.StartInfo.Arguments = path;
p.Start();
Debug.LogError("build success - " + fileName);
}
private static void Build_Ipa()
{
PlayerSettings.bundleVersion = "1";
PlayerSettings.iOS.buildNumber = "1";
PlayerSettings.applicationIdentifier = "com.test.test01";
string[] scenes = {
@"Assets/Res/Resources/scene/test.unity",
};
string path = EditorUtility.SaveFolderPanel("save xcode", "", "");
BuildOptions buildOptions = BuildOptions.CompressWithLz4;
BuildReport report = BuildPipeline.BuildPlayer(scenes, path, BuildTarget.iOS, buildOptions);
if (report.summary.result != BuildResult.Succeeded)
{
Debug.LogError("build fail - " + path);
return;
}
AssetDatabase.Refresh();
Debug.LogError("build success - " + path);
}
}
}
Unity命令行参数链接:
命令行参数 - Unity 手册