Xcode 自动化打包
.sh文件:build_ios.sh
注意:luajit-mac需要单独生成(后续添加生成的文章),放在.sh文件同级即可
./luajit-mac build_ios_ipa.lua
lua脚本:build_ios_ipa.lua
io = io
local print = print
local unity = "/Applications/Unity/Unity.app/Contents/MacOS/Unity "
local begin_time = os.time()
local client_path= "游戏Assets文件所在目录"
-- 这里是版本相关
local bundleVersion= "0000"
local gameVersion="1.0.0.0"
local toReplace_productName= "游戏名称"
local toReplace_identifier = "xxx.xxx.xxx"
-- 读取ProjectSettings.asset
local f = io.open(client_path .. "/ProjectSettings/ProjectSettings.asset", "r")
local lines = {}
while true do
local line = f:read("*l")
if line == nil then break end
-- 替换bundleVersion
if string.match(line, "bundleVersion:") then
line = string.gsub(line, "^(%s*bundleVersion:%s*)([%.%d]+)$", "%1" .. bundleVersion)
print("--->" .. line)
elseif string.match(line, "iOS:%s*%d*$") then
line = string.gsub(line, "^(%s*iOS:)%s*[^%s]*$", "%1 " .. gameVersion)
print("--->" .. line)
elseif string.match(line, "productName:") then
line = string.gsub(line, "^(%s*productName:)%s*[^%s]*$", "%1 " .. toReplace_productName)
print("--->" .. line)
elseif string.match(line, "iOS:%s*[^%d]*$") then
line = string.gsub(line, "^(%s*iOS:)%s*[^%s]*$", "%1 " .. toReplace_identifier)
print("--->" .. line)
end
lines[#lines + 1] = line
end
f:close()
-- 写回ProjectSettings.asset
local f = io.open(client_path .. "/ProjectSettings/ProjectSettings.asset", "wb")
f:write(table.concat(lines, "\n"))
f:write("\n")
f:close()
local xcodeProjectPath = "__ios_project/Unity-iPhone.xcodeproj"
local archivePath = "./bcgm_archive.xcarchive"
local ipaOutputDir = "导出目录"
local ipaFileName = ipaOutputDir .. ".ipa" -- ipaFileName
-- 证书的名称:双击证书复制标题就可以了
local codeSingIdentity = "\"iPhone Distribution: XXXXXX Technology Co. Ltd (xxxxx)\""
-- 这里是证书描述文件的uuid,需要用引号引起来
-- uuid获取方法在文章最后有说明
local provisioningProfile = "\"123456789\""
local unity_serial = "unity的激活码"
local unity_id = "'unity的账号'"
local unity_password = "'unity的密码'"
-- 0. 开始调用Bachmod Unity方法,生成xcode工程
print("构建ios xcode工程: ")
run_cmd(unity .. " -batchmode -serial .. unity_serial .. -username unity_id -password .. unity_password .. -quit -nographics -executeMethod Batchmode.BuildIos -logFile build_ios_ipa.log -projectPath=" .. client_path)
print("构建ios xcode工程: " .. " Completed !!!")
-- 编译xcode工程
-- 1. clean XCode
run_cmd("sudo xcodebuild clean -project " .. xcodeProjectPath .. " -scheme Unity-iPhone -verbose")
run_cmd("xcodebuild archive -project " .. xcodeProjectPath .. " -scheme Unity-iPhone -archivePath " .. archivePath .. " CODE_SIGN_IDENTITY=" .. codeSingIdentity.. " PROVISIONING_PROFILE=" .. provisioningProfile .. " -verbose")
-- 3. 生成ipa
run_cmd("xcodebuild -exportArchive -archivePath " .. archivePath .. " -exportPath " .. ipaOutputDir .. " -exportOptionsPlist ExportOptions.plist -verbose")
local t = os.time() - begin_time
print("\n\n")
print("---------------------------------------------------------")
print("------ 完成打 IOS 整包 ------")
print("---------------------------------------------------------")
print(string.format("total time = %d 分 %d 秒", t / 60, t % 60))
CS脚本:
此脚本放在Editor下
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// 命令行批处理工具类
/// </summary>
public class Batchmode
{
static List<string> levels = new List<string>();
[MenuItem("Tools/MakeIos")]
public static void BuildIos()
{
//这里要使用全路径导出 ../可能会有问题,在接入Bugly时使用XUPorter工具进行导出
//发现在XCode,target --> BuildSetting --> search Paths 里面的XUPoter的目录是基于Unity并不是基于.sh文件的
string exportPath = ".sh 文件所在目录"
foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
{
if (!scene.enabled) continue;
levels.Add(scene.path);
}
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.iOS);
string res = BuildPipeline.BuildPlayer(levels.ToArray(), exportPath, BuildTarget.iOS, BuildOptions.None);
if (res.Length > 0)
throw new Exception("BuildPlayer failure: " + res);
}
}
ExportOptions.plist文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>provisioningProfiles</key>
<dict>
<key>xxx.xxx.xxx(游戏的包名)</key>
<string>证书的UUID</string>
</dict>
<key>method</key>
<string>ad-hoc</string>
</dict>
</plist>
注:.sh文件,.lua文件, .plist文件在同一目录下
打包时遇到的一些问题
1、使用luajit-mac时需要: chomd 700 luajit-mac ,否则会出现没有权限的报错
(1)luajit-mac 所在文件夹右键打开控制台:服务/新建位于文件夹位置的终端窗口或者新建位于文件夹位置的终端窗口(根据mac设置而定)
(2)执行 chomd 700 luajit-mac
(3)执行 ls -al
(4)发现 luajit-mac 前面出现 -rwx 证明权限已经打开了
2、如果在导出archive 文件时出现profile文件不一致的情况,请重新下载profile文件,双击
3、出现Missing 证书的报错 :
在其他mac上已经打过一次包,现在在新的mac打包需要在之前打包的电脑,点击钥匙扣
找到我的证书,然后找到你需要的证书右键导出.p12文件(点击下一步需要设置密码)
复制这个.p12文件到当前打包的mac,双击输入设置的密码即可在我的证书登陆证书
4、打开Unity项目工程,需要打包的工程需要在第一个,打开需要打包的工程点击左上角Unity—>Preferences—>External Tools
下面的Xcode Default Settings
去掉Automatically Sign
证书:iPhone Distribution: XXXXXX Technology Co. Ltd (xxxxx)
输入Automatically Signing Team Id:xxxxx(证书括号里面的)
IOS Manual Provisioning Profile (选择下载的描述文件就可以了)
UUID会在下面显示复制即可