unity3d Unity iOS开发:如何通过构建后脚本从pod添加框架?

rn0zuynd  于 2023-01-26  发布在  iOS
关注(0)|答案(1)|浏览(386)

我们在Unity 2019.4.40f1中使用的是Facebook Unity SDK 15.1.0,这个SDK存在一些严重的bug,其中一个是不会将所需的框架添加到Unity-iPhone目标中,项目会构建,但启动时会立即崩溃。
框架已经存在,但在Pod中:

我可以在目标的General =〉Frameworks,Libraries,and Embedded Content部分手动添加它们:

一切正常。
然而,在每次构建之后这样做是相当乏味的,所以我想通过一个构建后脚本来自动化这项任务。我对此感到挠头,因为我在网上找不到真正有效的好例子。
所以我的问题是:如何添加隐藏在Facebook SDK的pod中的. xcframework,以便它正确地显示在目标中?

dnph8jn4

dnph8jn41#

下面是我将如何做。这可能不是100%正确,但足以修改它,使其为您工作。基本上,您需要使用PBXProject.AddFrameworkToProject添加框架。

#if UNITY_IOS
[PostProcessBuild(1)]
public static void ChangeXcodePlist(BuildTarget buildTarget, string pathToBuiltProject) {

    if (buildTarget == BuildTarget.iOS) {
   
        // get pbx project path
        var projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
        if (File.Exists(projPath))
        {
            var proj = new PBXProject();
            proj.ReadFromString(File.ReadAllText(projPath));

            string mainTargetGuid = null, testTargetGuid = null, frameworkTargetGuid = null;

#if UNITY_2019_4_OR_NEWER // APIs are different for getting main unity targets changes based on versions
            mainTargetGuid = proj.GetUnityMainTargetGuid();
            frameworkTargetGuid = proj.GetUnityFrameworkTargetGuid();
#else
            mainTargetGuid = 
proj.TargetGuidByName(PBXProject.GetUnityTargetName());
            testTargetGuid = 
proj.TargetGuidByName(PBXProject.GetUnityTestTargetName());
            frameworkTargetGuid = proj.TargetGuidByName("UnityFramework");
#endif
            
            // add your frameworks here
            if (!String.IsNullOrEmpty(mainTargetGuid))
            {
                Debug.Log("Adding targets to mainTargetGuid")
                proj.AddFrameworkToProject(mainTargetGuid, "FBSDKCoreKit.xcframework", false);
                proj.AddFrameworkToProject(mainTargetGuid, "FBSDKGamingServicesKit.xcframework", false);
            }
            // add to test target aswell if exists
            if (!String.IsNullOrEmpty(testTargetGuid))
            {
                Debug.Log("Adding targets to testTargetGuid")
                proj.AddFrameworkToProject(mainTargetGuid, "FBSDKCoreKit.xcframework", false);
                proj.AddFrameworkToProject(mainTargetGuid, "FBSDKGamingServicesKit.xcframework", false);
            }

            if (!String.IsNullOrEmpty(frameworkTargetGuid))
            {
                Debug.Log("Adding targets to frameworkTargetGuid")
                proj.AddFrameworkToProject(mainTargetGuid, "FBSDKCoreKit.xcframework", false);
                proj.AddFrameworkToProject(mainTargetGuid, "FBSDKGamingServicesKit.xcframework", false);
            }

            proj.WriteToFile(projPath);
        }
    }
}
#endif

您也可以在包含框架之前使用PBXProject.ContainsFramework

相关问题