Unity一鍵打包ipa(shell文件)

本教程環(huán)境為:Unity2017.3.1f1、Xcode10.1

1.設置開發(fā)者賬號

using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;


public class XCodeProjectMod : MonoBehaviour {
    [PostProcessBuild]
    public  static  void OnPostprocessBuild (BuildTarget BuildTarget, string path) {
        if (BuildTarget == BuildTarget.iOS) {
            //這里是固定的
            string projPath = PBXProject.GetPBXProjectPath(path);
            PBXProject proj = new PBXProject();
            proj.ReadFromString(File.ReadAllText(projPath));
            string target = proj.TargetGuidByName("Unity-iPhone");

            // set code sign identity & provisioning profile
            proj.SetBuildProperty (target, "CODE_SIGN_IDENTITY", "iPhone Developer: ming xing (9V4FTD5US7)");//替換你的code sign identity
            proj.SetBuildProperty (target, "PROVISIONING_PROFILE_SPECIFIER", "com.companyname");//替換你的provisioning profile specifier
            //proj.SetBuildProperty (target, "CODE_SIGN_ENTITLEMENTS", "KeychainAccessGroups.plist");
            proj.SetBuildProperty (target, "DEVELOPMENT_TEAM", "A3A6A4A6DB");//替換你的development team
            proj.SetBuildProperty (target, "CODE_SIGN_STYLE", "Manual");

            //ulua 庫不支持 bitcode必須要關閉,才能打包
            proj.SetBuildProperty(target,"ENABLE_BITCODE", "NO");

            File.WriteAllText(projPath, proj.WriteToString());
        }
    }
}

2.編寫一鍵打包shell文件

(不明白請查看https://blog.csdn.net/qq_14974975/article/details/83825522,本教程大部分引用此網頁內容)
建一個.sh文件,放到工程根目錄

#!/bin/sh

#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

#游戲程序路徑 需要將本文件放入unity工程目錄中#
PROJECT_PATH=$(dirname "$0")

#解析打包配置文件
CONFIG_PLIST=${PROJECT_PATH}"/build/iOS/Info/exportinfo.plist"

# 版本號
CONFIG_Version=$(/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' $CONFIG_PLIST)
#Xcode工程版本號
CFBundleShortVersionString=$CONFIG_Version

# 打包格式文件
CONFIG_exportOptionsPlist_Name=$(/usr/libexec/PlistBuddy -c 'Print :exportOptionsPlist' $CONFIG_PLIST)
CONFIG_exportOptionsPlist=${PROJECT_PATH}"/build/iOS/Info/"${CONFIG_exportOptionsPlist_Name}

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

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

#這是游戲導出的ipa名字,記得修改這里
app_name="appname"
# 拼接游戲名稱
ipa_name=${app_name}"_ios_publish_"${CFBundleShortVersionString}"."${CFBundleVersion}  

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


# 傳給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

#設置Xcode info.plist-----------------------------------start
#       /usr/libexec/PlistBuddy -c 'Set :CFBundleShortVersionString '${CFBundleShortVersionString} $PLIST_PATH
#       /usr/libexec/PlistBuddy -c 'Set :CFBundleVersion '${CFBundleVersion} $PLIST_PATH
#設置Xcode info.plist-----------------------------------end

#開始打包ipa-----------------------------------start
    {
        #try
        #清理編譯工程
        EXPORT_PATH=${PROJECT_PATH}"/build/iOS/Export/"

        infolog "----------enter Xcode Project:"${Xcode_ProjectPath}
        cd $Xcode_ProjectPath

        infolog "----------clean Xcode Project"
        xcodebuild clean -quiet

        infolog "----------archive Xcode Project:"${EXPORT_PATH}${ipa_name}
        #編譯工程
        xcodebuild archive -scheme "Unity-iPhone" -configuration "Release" -archivePath  ${EXPORT_PATH}${ipa_name}"/"${app_name}"_Archive.xcarchive" -quiet || exit

        infolog "----------ipa Xcode Project"
        #打包  -archivePath Xcode導出的文件路徑   -configuration 導出格式(Debug/Release)   -exportPath ipa導出路徑  -exportOptionsPlist 導出模式,使用plist文件控制 在xcode手動打包時會在ipa同目錄下生成對應格式的plist文件 可以直接使用改pist文件并根據不同格式備份多份
        xcodebuild -exportArchive -archivePath ${EXPORT_PATH}${ipa_name}"/"${app_name}"_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}"/"${app_name}"_Archive.xcarchive/dSYMs" ]; then
        errorlog "----------dSYM字符表文件未導出,檢查Xcode設置!!!!!!!!!(文件夾不存在)"
    else
        cd ${EXPORT_PATH}${ipa_name}"/"${app_name}"_Archive.xcarchive/dSYMs"
        if [ ! -e ${app_name}".app.dSYM" ]; then
            #字符表文件不存在
            errorlog "----------dSYM字符表文件未導出,檢查Xcode設置!!!!!!!!!(文件不存在)"
        else
            cp -R ${app_name}".app.dSYM" ${EXPORT_PATH}${ipa_name}
        fi
    fi
#復制字符表文件到導出ipa的文件夾------------------end

infolog "Ipa導出成功!!!!!!!!!"
#打開指定文件夾
open ${EXPORT_PATH}${ipa_name}

2.1Unity打包

上面"2.編寫一鍵打包shell文件"的.sh文件有一行代碼:

$UNITY_PATH -projectPath $PROJECT_PATH -executeMethod AutoBuild.BuildIos "Path_"${Xcode_Untiy_ProjectPath} -quit

提到AutoBuild.BuildIos是Unity打包的代碼,需要在Unity里面編寫:

public class AutoBuild
    /// <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);
    }
}

2.2打包配置

上面"2.編寫一鍵打包shell文件"的.sh文件有一行代碼:

CONFIG_PLIST=${PROJECT_PATH}"/build/iOS/Info/exportinfo.plist"

提到exportinfo.plis是打包配置文件,只是為了一鍵打包shell配置分開2個文件,修改配置的時候,不影響一鍵打包shell
配置文件exportinfo.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>exportOptionsPlist</key>
    <string>dev.plist</string>
    <key>buildVersion</key>
    <string>24</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0.10</string>
    <key>isIl2cpp</key>
    <true/>
</dict>
</plist>

本教程exportinfo.plist路徑為Unity工程根目錄/build/iOS/Info

2.2.1archive導出ipa配置文件

上面"2.2打包配置"的.plist文件有一行代碼:

<string>dev.plist</string>

提到dev.plist是archive導出ipa需要用的配置文件。自己Xcode手動打一次ipa,在ipa同目錄下會看ExportOptions.plist,ExportOptions.plist就是這里提到的dev.plist。本教程dev.plist路徑為Unity工程根目錄/build/iOS/Info

2、2.2、2.2.1提到的3個文件路徑目錄如下

.sh.plist目錄結構.png

3.導出ipa

"2.編寫一鍵打包shell文件"的.sh文件拖入終端,回車等待即可!
如果提示.sh文件沒有權限,cd到腳本目錄,chmod +x 腳本名.sh即可

郵箱:chasing2moro@qq.com

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容