Unity发布iOS,一键导出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,
}
//入口函数
[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参数,请根据自己的项目自定义
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);
}
}