使用shell脚本实现unity自动打包ipa工具


先说下我这的需求:
1.一键生成ipa ( 废话
2.info.plist和.xcodeproj文件使用已设置完成的标准模板替换 (省去代码设置xcode工程配置的工作量, 偷懒
3.需要保留字符表文件 ( 什么是字符表文件?
4.需要在打包时使用配置文件,控制xcode中的 VersionBuild打包格式(测试包/正式包)

shell脚本中重点代码逻辑

以下是shell脚本调用unity中代码打包成xcode工程和调用xcodebuild打包生成ipa和字符表文件。

#!/bin/sh

#参数判断  
if [ $# != 1 ];then
    echo "需要传入打包配置文件"  
    exit     
fi  

#Log颜色处理-------------start
function infolog()
{
    echo -e Log: "\033[32m$1\033[0m"
}

#输出报错log
function errorlog()
{
    echo -e Error: "\033[01;31m$1\033[0m"
}

#输出报错log并停止运行
function errorlogExit()
{
    echo -e Error: "\033[01;31m$1\033[0m"
    echo -e Error: "\033[01;31m打包失败\033[0m"
	exit
}
#Log颜色处理-------------end

#解析打包配置文件
CONFIG_PLIST=$1

# 版本号
CONFIG_Version=$(/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' $CONFIG_PLIST)

# 打包格式文件
CONFIG_exportOptionsPlist=$(/usr/libexec/PlistBuddy -c 'Print :exportOptionsPlist' $CONFIG_PLIST)

# 是否为Il2cpp模式打包
CONFIG_isIl2cpp=$(/usr/libexec/PlistBuddy -c 'Print :isIl2cpp' $CONFIG_PLIST)

# build号
CFBundleVersion=$(/usr/libexec/PlistBuddy -c 'Print :buildVersion' $CONFIG_PLIST)

# 拼接游戏名称
ipa_name="xxxx_ios_publish_"${CFBundleShortVersionString}"."${CFBundleVersion}  

#UNITY程序的路径#
UNITY_PATH=/Applications/Unity/Unity.app/Contents/MacOS/Unity

#游戏程序路径 需要将本文件放入unity工程目录中#
PROJECT_PATH=$(dirname "$0")


#复制文件目录
COPY_FILE_PATH="要复制替换的文件路径"
COPY_FILE_PATH_PLIST=${COPY_FILE_PATH}/info.plist
COPY_FILE_PATH_XCODEPROJECT=${COPY_FILE_PATH}"/Unity-iPhone.xcodeproj"


#Xcode工程版本号
CFBundleShortVersionString=$CONFIG_Version

# 传给unity的XCODE导出路径
Xcode_Untiy_ProjectPath="build/iOS/XcodeProject"

#Xcode工程目录
Xcode_ProjectPath=${PROJECT_PATH}"/"${Xcode_Untiy_ProjectPath}

#将unity导出成xcode工程-----------------------------------start
	{
		#try 
		$UNITY_PATH -projectPath $PROJECT_PATH -executeMethod AutoBuild.BuildIos "Path_"${Xcode_Untiy_ProjectPath} -quit
	} || { 
		#catch
		errorlogExit "unity导出Xcode工程失败"
	}
	infolog "----------XCODE工程生成完毕,路径:"${Xcode_ProjectPath}
#将unity导出成xcode工程-----------------------------------end

#将复制文件拷贝到工程中-----------------------------------start
	{
		#try 
		
		infolog "----------开始复制/替换文件,替换文件路径:"${COPY_FILE_PATH}
		cp $COPY_FILE_PATH_PLIST ${Xcode_ProjectPath}
		cp -R $COPY_FILE_PATH_XCODEPROJECT ${Xcode_ProjectPath}
	} || { 
		#catch
		errorlogExit "复制资源失败"
	}
#将复制文件拷贝到工程中-----------------------------------end

#设置Xcode info.plist-----------------------------------start
	infolog "----------开始修改plist配置"
	{
		#try
		#修改plist中版本号
		#获取到info.plist
		PLIST_PATH=${Xcode_ProjectPath}/info.plist

		#使用PlistBuddy 修改plist中内容 注:PlistBuddy为绝对路径
		/usr/libexec/PlistBuddy -c 'Set :CFBundleShortVersionString '${CFBundleShortVersionString} $PLIST_PATH
		/usr/libexec/PlistBuddy -c 'Set :CFBundleVersion '${CFBundleVersion} $PLIST_PATH
	}||{
		#catch
		errorlogExit "修改plist内容失败"
	}
#设置Xcode info.plist-----------------------------------end

#开始打包ipa-----------------------------------start
	{
		#try
		#清理编译工程
		EXPORT_PATH="ipa包导出地址"
		xcodebuild clean -quiet

		cd $Xcode_ProjectPath
		#编译工程
		xcodebuild archive -scheme "Unity-iPhone" -configuration "Release" -archivePath  ${EXPORT_PATH}${ipa_name}"/xxxx_Archive.xcarchive" -quiet || exit

		#打包  -archivePath Xcode导出的文件路径   -configuration 导出格式(Debug/Release)   -exportPath ipa导出路径  -exportOptionsPlist 导出模式,使用plist文件控制 在xcode手动打包时会在ipa同目录下生成对应格式的plist文件 可以直接使用改pist文件并根据不同格式备份多份
		xcodebuild -exportArchive -archivePath ${EXPORT_PATH}${ipa_name}"/xxxx_Archive.xcarchive" -configuration "Release" -exportPath ${EXPORT_PATH}${ipa_name} -exportOptionsPlist ${CONFIG_exportOptionsPlist} -quiet || exit
	}||{
		#catch
		errorlogExit "xcode工程清理/打包/导出失败 请查看上层error log"
	}
#开始打包ipa-----------------------------------start

#复制字符表文件到导出ipa的文件夹------------------start
	if [ ! -d ${EXPORT_PATH}${ipa_name}"/xxxx_Archive.xcarchive/dSYMs" ]; then
		errorlog "----------dSYM字符表文件未导出,检查Xcode设置!!!!!!!!!"
	else
		cd ${EXPORT_PATH}${ipa_name}"/xxxx_Archive.xcarchive/dSYMs"
		if [ ! -e "xxxx.app.dSYM" ]; then
			#字符表文件不存在
			errorlog "----------dSYM字符表文件未导出,检查Xcode设置!!!!!!!!!"
		else
			cp -R "xxxx.app.dSYM" ${EXPORT_PATH}${ipa_name}
		fi
	fi
#复制字符表文件到导出ipa的文件夹------------------end

infolog "Ipa导出成功!!!!!!!!!"
#打开指定文件夹
open ${EXPORT_PATH}${ipa_name}

-exportOptionsPlist要传入打包参数的plist文件,用来控制是导出测试包还是正式包。可以手动xcode打包ipa,在ipa导出的文件夹下就躺着plist文件了,复制出来保存。保留一份正式包一份测试包的,方便使用。
在这里插入图片描述

plist配置文件设置

在这里插入图片描述
为什么配置文件使用plist格式?因为别的不会?
exportOptionsPlist为打包格式plist文件路径。

C#中重点代码

	/// <summary>
	/// ios自动打包测试
	/// </summary>
	public static void BuildIos()
	{
	    // 工程导出路径
	    string nXcodeOutputPath = string.Empty;
	    // 解析shell传入的参数
	    foreach (string commandLineArg in System.Environment.GetCommandLineArgs())
	    {
	        if (commandLineArg.Contains("Path_"))
	        {
	            nXcodeOutputPath = commandLineArg.Split('_')[1];
	        }
	    }
	    // 设置需要打包的场景
	    string launchScene = "Assets/Scenes/GameStart.unity";
	    
	    var buildPlayerOptions = new BuildPlayerOptions();
	    buildPlayerOptions.locationPathName = nXcodeOutputPath;
	    buildPlayerOptions.options = BuildOptions.None;
	    buildPlayerOptions.options |= BuildOptions.AcceptExternalModificationsToPlayer;
	    buildPlayerOptions.scenes = new[] { launchScene };
	    buildPlayerOptions.target = BuildTarget.iOS;
	    // 调用开始打包	
	    BuildPipeline.BuildPlayer(buildPlayerOptions);
	}

!!!!该方法所在类需要引用using UnityEditor;

使用方法

将shell脚本拖入终端,然后将plist配置文件拖入终端,回车等待即可!
如果提示shell脚本没有权限,cd到脚本目录,chmod +x 脚本名.sh即可

其他

如果想要使用代码控制xcode工程配置,就删除复制替换部分shell代码,使用XUPorter,使用方法请自行google。
如果你的工程使用了cocoapods,如使用了firebase_unity(坑爹的货),xcodebuild调用代码会有修改

#编译工程
xcodebuild archive -workspace ${project_name}.xcworkspace -scheme ${scheme_name} -configuration ${development_mode} -archivePath build/${project_name}.xcarchive -quiet  || exit
#打包
xcodebuild -exportArchive -archivePath build/${project_name}.xcarchive -configuration ${development_mode} -exportPath ${exportFilePath} -exportOptionsPlist ${exportOptionsPlistPath} -quiet || exit

本人shell/ios都是小白,哪里有愚蠢之处请指出。
有什么问题请留言,着急的邮件conan_watson@163.com

参考

https://www.cnblogs.com/purple-sweet-pottoes/p/6947500.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值