namespace LhorionIOSSDK { #if UNITY_IOS using UnityEngine; using UnityEditor; using UnityEditor.Callbacks; using UnityEditor.iOS.Xcode; using System.IO; using UnityEditor.Build; using UnityEditor.Build.Reporting; using System; public class BuildPostprocessor : IPreprocessBuildWithReport { public int callbackOrder => 0; public void OnPreprocessBuild(BuildReport report) { CopyDlls(); } private static void CopyDlls() { string cur = "Assets/LhorionSDK/StreamingAssets"; string des = "Assets/StreamingAssets/LhorionSDK/"; CheckDirectory(des); CopyDirctory(cur, des, "StreamingAssets"); AssetDatabase.Refresh(); } static public void CheckDirectory(string path) { var direct = Path.GetDirectoryName(path); if (!Directory.Exists(direct)) { Directory.CreateDirectory(direct); } } public static void CopyDirctory(string sourceDirectory, string targetDirectory, string tag) { try { string[] fileList = Directory.GetFiles(sourceDirectory, "*", SearchOption.AllDirectories); foreach (string f in fileList) { if (f.EndsWith(".meta")) { continue; } // Remove path from the file name. string fName = f.Substring(sourceDirectory.Length + 1); // Use the Path.Combine method to safely append the file name to the path. // Will overwrite if the destination file already exists. File.Copy(Path.Combine(sourceDirectory, fName), Path.Combine(targetDirectory, fName), true); Debug.Log($"Copy [{tag}] {Path.Combine(sourceDirectory, fName)} => {Path.Combine(targetDirectory, fName)}"); } } catch (Exception ex) { Debug.LogError($"拷贝文件夹:{sourceDirectory} To {targetDirectory} 出现异常:{ex.Message}"); } } [PostProcessBuild] public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) { if (target == BuildTarget.iOS) { AddLibraryToXcodeProject(pathToBuiltProject); } } [PostProcessBuild] public static void OnPostprocessBuild2(BuildTarget target, string pathToBuiltProject) { if (target == BuildTarget.iOS) { AddTess(target, pathToBuiltProject); } } public static void AddTess(BuildTarget target, string pathToBuiltProject) { if (target != BuildTarget.iOS) return; string projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject); PBXProject proj = new PBXProject(); proj.ReadFromFile(projPath); string unityFrameworkTargetGuid = proj.GetUnityFrameworkTargetGuid(); #if UNITY_2019_3_OR_NEWER string targetGuid = proj.GetUnityMainTargetGuid(); // 或 GetUnityFrameworkTargetGuid 取决于需求 #else string targetGuid = proj.TargetGuidByName("Unity-iPhone"); #endif // 设置路径(Unity 工程中 Assets 的路径) string tesseractLibPath = "LhorionSDK/Plugins/iOS/tesseract/lib/"; string fullLibPath = Path.Combine(Application.dataPath, tesseractLibPath); if (!Directory.Exists(fullLibPath)) { UnityEngine.Debug.LogWarning($"Tesseract lib directory not found at: {fullLibPath}"); return; } string destLibPath = Path.Combine(pathToBuiltProject, "TesseractLibs"); Directory.CreateDirectory(destLibPath); // 确保目录存在 string[] libFiles = Directory.GetFiles(fullLibPath, "*.a"); if (libFiles.Length == 0) { UnityEngine.Debug.LogWarning($"No .a files found in Tesseract lib directory: {fullLibPath}"); return; } foreach (string libFile in libFiles) { string fileName = Path.GetFileName(libFile); string dstFilePath = Path.Combine(destLibPath, fileName); File.Copy(libFile, dstFilePath, true); // 复制到 Xcode 工程路径 string relativePathInProj = "TesseractLibs/" + fileName; string fileGuid = proj.AddFile(relativePathInProj, relativePathInProj, PBXSourceTree.Source); proj.AddFileToBuild(targetGuid, fileGuid); UnityEngine.Debug.Log($"Added Tesseract lib to Xcode project: {relativePathInProj}"); } // 设置头文件搜索路径 string libSearchPath = "$(SRCROOT)/TesseractLibs"; proj.AddBuildProperty(targetGuid, "LIBRARY_SEARCH_PATHS", libSearchPath); proj.AddBuildProperty(unityFrameworkTargetGuid, "LIBRARY_SEARCH_PATHS", libSearchPath); proj.WriteToFile(projPath); } public static void AddLibraryToXcodeProject(string pathToBuiltProject) { // 获取 Xcode 项目路径 string projectPath = PBXProject.GetPBXProjectPath(pathToBuiltProject); // 初始化 PBXProject PBXProject project = new PBXProject(); project.ReadFromFile(projectPath); // 获取主 target GUID #if UNITY_2019_3_OR_NEWER string targetGuid = project.GetUnityMainTargetGuid(); string frameworkTargetGuid = project.GetUnityFrameworkTargetGuid(); #else string targetGuid = project.TargetGuidByName("Unity-iPhone"); string frameworkTargetGuid = targetGuid; #endif // 添加 libz.tbd 到 Link Binary With Libraries project.AddFrameworkToProject(frameworkTargetGuid, "libz.tbd", false); // ✅ 添加 -lz 到整个 Xcode 工程(project 层面) project.AddBuildProperty(project.ProjectGuid(), "OTHER_LDFLAGS", "-lz"); // 如果你想确保 framework target 也有 -lz,可以额外加: project.AddBuildProperty(frameworkTargetGuid, "OTHER_LDFLAGS", "-lz"); project.WriteToFile(projectPath); Debug.Log("✅ 成功将 libz.tbd 添加到工程,并将 -lz 设置在 project 和 framework target 上"); } } #endif }