您的位置:首页 > 移动开发 > Unity3D

Unity3D iOS 使用 Dynamic Library 的 framework 时需要 添加 Embedded Binaries 的解决方案

2018-02-08 16:53 453 查看
这里需要用到 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 添加完成了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐