#unity/代码缓存 ## PostProcessBuildClass ``` cs #if UNITY_IOS using System.Diagnostics; using System.IO; using System.Threading; using UnityEditor; using UnityEditor.Callbacks; using UnityEditor.iOS.Xcode; using Debug = UnityEngine.Debug; /// /// xcode打包后处理 /// static class PostProcessBuildClass { private static Thread podFileCheckThread; private const int timeOutSeconds = 10; [PostProcessBuild(1999)] public static void OnPostProcessBuild(BuildTarget pBuildTarget, string pPath) { if (pBuildTarget == BuildTarget.iOS) { InitPodFile(pPath); AddSurveyMonkey(pPath); PodInstall(pPath); } } /// /// 如果podfile不存在,就自动创建一个新的 /// private static void InitPodFile(string pPath) { string podfilePath = Path.Combine(pPath, "Podfile"); if (!File.Exists(podfilePath)) { string podFileContent = @" source 'https://cdn.cocoapods.org/' platform :ios, '12.0' target 'UnityFramework' do end target 'Unity-iPhone' do end"; File.WriteAllText(podfilePath, podFileContent); } } private static void AddSurveyMonkey(string pPath) { string varPodfilePath = Path.Combine(pPath, "Podfile"); string varPodfileContent = File.ReadAllText(varPodfilePath); // 检查是否已包含SurveyMonkey if (!varPodfileContent.Contains("pod 'surveymonkey-ios-sdk', '~> 2.1.2'")) { string varInsertionPoint1 = "target 'UnityFramework' do"; int varIndex1 = varPodfileContent.IndexOf(varInsertionPoint1) + varInsertionPoint1.Length; string varToInsert1 = "\n pod 'surveymonkey-ios-sdk', '~> 2.1.2'"; varPodfileContent = varPodfileContent.Insert(varIndex1, varToInsert1); string varInsertionPoint2 = "target 'Unity-iPhone' do"; int varIndex2 = varPodfileContent.IndexOf(varInsertionPoint2) + varInsertionPoint2.Length; string varToInsert2 = "\n pod 'surveymonkey-ios-sdk', '~> 2.1.2'"; varPodfileContent = varPodfileContent.Insert(varIndex2, varToInsert2); File.WriteAllText(varPodfilePath, varPodfileContent); } } /// /// 在pod 设置插件库之后. 执行pod install /// /// private static void PodInstall(string pPath) { Debug.Log(pPath); string varPodfilePath = Path.Combine(pPath, "Podfile"); void CheckFile() { int elapsedSeconds = 0; while (elapsedSeconds < timeOutSeconds) { if (File.Exists(varPodfilePath)) { // Run pod install or update string code = $"-c \"cd '{pPath}' && pod install\""; ProcessStartInfo varProcessStartInfo = new ProcessStartInfo("sh", code) { UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true }; Process varProcess = new Process(); varProcess.StartInfo = varProcessStartInfo; varProcess.StartInfo.EnvironmentVariables["LC_ALL"] = "en_US.UTF-8"; varProcess.StartInfo.EnvironmentVariables["LANG"] = "en_US.UTF-8"; varProcess.EnableRaisingEvents = true; varProcess.Start(); string varOutput = varProcess.StandardOutput.ReadToEnd(); string varErr = varProcess.StandardError.ReadToEnd(); varProcess.WaitForExit(); Debug.Log("======> Pod install output: " + varOutput); if (!string.IsNullOrEmpty(varErr)) { Debug.LogError("======> Pod install error: " + varErr); } break; } Thread.Sleep(1000); // 等待一秒再检查 elapsedSeconds++; } } podFileCheckThread = new Thread(CheckFile); podFileCheckThread.Start(); } } #endif ```