57 lines
2.2 KiB
C#
57 lines
2.2 KiB
C#
using UnityEngine;
|
|||
|
|
using UnityEditor;
|
||
|
|
using UnityEditor.Callbacks;
|
||
|
|
using System.IO;
|
||
|
|
#if UNITY_IOS
|
||
|
|
using UnityEditor.iOS.Xcode;
|
||
|
|
#endif
|
||
|
|
|
||
|
|
public class MoveLibToClasses
|
||
|
|
{
|
||
|
|
// PostProcessBuild 属性确保这段代码在 Unity 导出 Xcode 工程完成后自动执行
|
||
|
|
[PostProcessBuild(999)]
|
||
|
|
public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
|
||
|
|
{
|
||
|
|
if (target != BuildTarget.iOS) return;
|
||
|
|
|
||
|
|
#if UNITY_IOS
|
||
|
|
// 1. 定义源文件路径和目标文件路径
|
||
|
|
string fileName = "libweb.a";
|
||
|
|
string sourcePath = Path.Combine(Application.dataPath, "Editor", fileName);
|
||
|
|
string destDir = Path.Combine(pathToBuiltProject, "Classes");
|
||
|
|
string destPath = Path.Combine(destDir, fileName);
|
||
|
|
|
||
|
|
// 检查源文件是否存在
|
||
|
|
if (!File.Exists(sourcePath))
|
||
|
|
{
|
||
|
|
Debug.LogError($"[MoveLib] 找不到源文件: {sourcePath}");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2. 物理复制文件到 Xcode 工程的 Classes 文件夹中
|
||
|
|
if (!Directory.Exists(destDir))
|
||
|
|
{
|
||
|
|
Directory.CreateDirectory(destDir);
|
||
|
|
}
|
||
|
|
File.Copy(sourcePath, destPath, true);
|
||
|
|
Debug.Log($"[MoveLib] 成功将库复制到: {destPath}");
|
||
|
|
|
||
|
|
// 3. 修改 Xcode 工程文件(PBXProject),将文件注册到 Xcode 中
|
||
|
|
string pbxProjectPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
|
||
|
|
PBXProject project = new PBXProject();
|
||
|
|
project.ReadFromString(File.ReadAllText(pbxProjectPath));
|
||
|
|
|
||
|
|
string targetGuid = project.GetUnityFrameworkTargetGuid(); // 或者 project.TargetGuidByName(PBXProject.GetUnityTargetName())
|
||
|
|
|
||
|
|
// 将文件添加到 Xcode 的 Classes 引用中 (相对于工程根目录路径为 "Classes/libwebview_jiami.a")
|
||
|
|
string fileGuid = project.AddFile("Classes/" + fileName, "Classes/" + fileName, PBXSourceTree.Source);
|
||
|
|
|
||
|
|
// 将该 .a 文件加入到编译链接中
|
||
|
|
project.AddFileToBuild(targetGuid, fileGuid);
|
||
|
|
|
||
|
|
// 保存对 Xcode 工程的修改
|
||
|
|
File.WriteAllText(pbxProjectPath, project.WriteToString());
|
||
|
|
Debug.Log("[MoveLib] Xcode 工程配置已成功更新!");
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
}
|