using UnityEditor; using UnityEditor.Callbacks; using UnityEditor.iOS.Xcode; using System.IO; using UnityEditor.Build; using UnityEditor.Build.Reporting; using UnityEngine; public class PostProcessBuild : IPostprocessBuildWithReport { public int callbackOrder => 1; public void OnPostprocessBuild(BuildReport report) { // 检查是否为 iOS 平台构建 if (report.summary.platform == BuildTarget.iOS) { string projectPath = report.summary.outputPath; string projFilePath = PBXProject.GetPBXProjectPath(projectPath); PBXProject proj = new PBXProject(); proj.ReadFromFile(projFilePath); string targetGuid = proj.GetUnityFrameworkTargetGuid(); // 示例:复制自定义 UnityAppController.mm string customControllerPath = "Assets/Editor/UnityAppController.mm"; string destinationPath = Path.Combine(projectPath, "Classes/UnityAppController.mm"); if (File.Exists(customControllerPath)) { File.Copy(customControllerPath, destinationPath, overwrite: true); Debug.Log("Custom UnityAppController.mm has been copied to Xcode project."); } else { Debug.LogError("Custom UnityAppController.mm file not found at " + customControllerPath); } // 示例:添加插件文件时避免重复 string pluginPath = Path.Combine(projectPath, "Libraries/Plugins/iOS/pluginIOs.mm"); if (!proj.ContainsFileByRealPath(pluginPath)) { string fileGuid = proj.AddFile(pluginPath, "Libraries/Plugins/iOS/pluginIOs.mm", PBXSourceTree.Source); proj.AddFileToBuild(targetGuid, fileGuid); Debug.Log("pluginIOs.mm added to UnityFramework target."); } else { Debug.Log("pluginIOs.mm already exists, skip adding."); } File.WriteAllText(projFilePath, proj.WriteToString()); } } }