Unity發(fā)布iOS,一鍵導(dǎo)出Xcode工程并且自動生成ipa文件橙凳。C#代碼如下:
using UnityEditor;
public class ExportIPAHelper
{
public static void ExeCommand(string vFileName, string vWorkingDirectory, string vArguments)
{
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
info.FileName = vFileName;
info.Arguments = vArguments;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
info.UseShellExecute = true;
info.WorkingDirectory = vWorkingDirectory;
info.ErrorDialog = true;
UnityEngine.Debug.Log(info.FileName + " " + info.Arguments);
UnityEngine.Debug.Log(info.WorkingDirectory);
System.Diagnostics.Process pro = System.Diagnostics.Process.Start(info);
pro.WaitForExit();
}
public enum ExportIOSType
{
AppStore,
TestFlight,
Internal,
}
//入口函數(shù)
[MenuItem("Tools/ExportIPA")]
public static void ExeCommandXcodeBuildIPATest()
{
ExeCommandXcodeBuildIPA(ExportIOSType.AppStore);
}
public static void ExeCommandXcodeBuildIPA(ExportIOSType exportIOSType = ExportIOSType.AppStore)
{
//export to xcode project
UnityEngine.Debug.Log("[1]export xcode project");
string xcodeProjectPathRelative = "build/iOS/XcodeProject" + exportIOSType.ToString();//相對于Unity工程的xcode工程路徑
string xcodeProjectPath = GetAssetsRootDir(xcodeProjectPathRelative);//xcode工程絕對路徑
if (!System.IO.Directory.Exists(xcodeProjectPath))
System.IO.Directory.CreateDirectory(xcodeProjectPath);
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
#error buildPlayerOptions.scenes參數(shù)巷折,請根據(jù)自己的項目自定義
buildPlayerOptions.scenes = new[] { "Assets/Splash.unity", "Assets/Update.unity", "Assets/Main.unity" };
buildPlayerOptions.locationPathName = xcodeProjectPathRelative;
buildPlayerOptions.target = BuildTarget.iOS;
buildPlayerOptions.options = BuildOptions.StrictMode;
BuildPipeline.BuildPlayer(buildPlayerOptions);
//clean xcode project
UnityEngine.Debug.Log("[2]clean xcode project");
ExeCommand("xcodebuild", xcodeProjectPath, "clean -quiet");
//export archive
UnityEngine.Debug.Log("[3]export archive");
#error 請建一個相對于工程的目錄build/iOS/Export(也就是build目錄和Assets坯钦、ProjectSettings同級)
string exportPath = System.IO.Path.Combine(GetAssetsRootDir("build/iOS/Export"), exportIOSType.ToString() + System.DateTime.Now.ToString("yyyyMMdd"));
if (System.IO.Directory.Exists(exportPath))
System.IO.Directory.Delete(exportPath, true);
string appName = string.Format("mygame_{0}_{1}_zone{2}_{3}", System.DateTime.Now.ToString("yyyyMMdd"), "1.0", "1", exportIOSType == ExportIOSType.Internal ? "debug" : "release");
string archivePath = System.IO.Path.Combine(exportPath, appName + ".xcarchive");
ExeCommand("xcodebuild", xcodeProjectPath, string.Format("archive -scheme \"Unity-iPhone\" -configuration \"Release\" -archivePath {0} -quiet", archivePath));
//export ipa
UnityEngine.Debug.Log("[4]export ipa");
string ipaPath = exportPath;
#error 請建一個相對于工程的目錄build/iOS/Info(也就是build目錄和Assets为鳄、ProjectSettings同級)
string exportOptionsPlist = GetAssetsRootDir("build/iOS/Info/ExportOptions.plist");//自己Xcode手動打一次ipa爸舒,在ipa同目錄下會看ExportOptions.plist,把它copy過來build/iOS/Info目錄下
ExeCommand("xcodebuild", xcodeProjectPath, string.Format("-exportArchive -archivePath {0} -configuration \"Release\" -exportPath {1} -exportOptionsPlist {2} -quiet", archivePath, ipaPath, exportOptionsPlist));
//open dir
System.Diagnostics.Process.Start(exportPath);
}
public static string GetAssetsRootDir(string append = null)
{
string dataPath = UnityEngine.Application.dataPath;
dataPath = dataPath.Replace("\\", "/");
int index = dataPath.IndexOf("/Assets");
string rootDir = dataPath.Substring(0, index);
if (string.IsNullOrEmpty(append))
return rootDir;
return System.IO.Path.Combine(rootDir, append);
}
}