这里需要用到 Unity3D Editor 提供的 PostProcessBuildAttribute 来实现。PostProcessBuildAttribute的本质是在unity编译完成之后,你可以利用这个做一件事情。
直接上代码:
新建一个文件
TestBuildPostprocessor.cs
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.IO;
using System.Diagnostics;
#if UNITY_EDITOR_OSX
using UnityEditor.iOS.Xcode;
using UnityEditor.iOS.Xcode.Extensions;
#endif
public class TestBuildPostprocessor {//类名随便取
[PostProcessBuildAttribute(1)]//括号里的数字是有多个PostProcessBuildAttribute时执行顺序,0是第一个执行的
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) {
if (target != BuildTarget.iOS) {
UnityEngine.Debug.LogWarning ("Target is not iPhone. XCodePostProcess will not run");
return;
}
#if UNITY_EDITOR_OSX
//EmbedFrameworks
string projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
string targetGuid = proj.TargetGuidByName("Unity-iPhone");
const string defaultLocationInProj = "Plugins/iOS";//framework 存放的路径
const string coreFrameworkName = "test.framework";// framework 的文件名
string framework = Path.Combine(defaultLocationInProj, coreFrameworkName);
string fileGuid = proj.AddFile(framework, "Frameworks/" + framework, PBXSourceTree.Sdk);
PBXProjectExtensions.AddFileToEmbedFrameworks(proj, targetGuid, fileGuid);
proj.SetBuildProperty(targetGuid, "LD_RUNPATH_SEARCH_PATHS", "$(inherited) @executable_path/Frameworks");//如果没有这句话,运行会崩溃,报 image not found 错误
proj.WriteToFile (projPath);
//EmbedFrameworks end
#endif
}
}
#endif
这样 Dynamic Library framework 的 Embedded Binaries 添加完成了。