Пример #1
0
    private static void OnPostprocessBuild(BuildTarget buildTarget, string buildPath)
    {
        //是不是ios
        if (buildTarget != BuildTarget.iOS)
        {
            return;
        }
        string cfgPath = Application.dataPath + "/XcodeSet.asset";//PlayerPrefs.GetString(SETTING_DATA_PATHKEY,"");

        if (string.IsNullOrEmpty(cfgPath))
        {
            return;
        }
        //Xcode读取配置数据
        XcodeProjectSetting setting = AssetDatabase.LoadAssetAtPath <XcodeProjectSetting>(cfgPath);

        if (setting == null)
        {
            Debug.Log("Resources/" + cfgPath + "目录不存在");
            return;
        }
        else
        {
            Debug.LogError("cfgPath =" + cfgPath);
        }

        string     pbxProjPath = PBXProject.GetPBXProjectPath(buildPath);
        PBXProject pbxProject  = new PBXProject();

        pbxProject.ReadFromString(File.ReadAllText(pbxProjPath));

        //获取 target的Guid
        string targetGuid = pbxProject.TargetGuidByName(PBXProject.GetUnityTargetName());
        string projPath   = buildPath + "/Unity-iPhone.xcodeproj/project.pbxproj";

        //LinkLibraries(projPath);
        //拷贝所有文件
        if (!string.IsNullOrEmpty(setting.CopyDirectoryPath))
        {
            DirectoryProcessor.CopyAndAddBuildToXcode(pbxProject, targetGuid, setting.CopyDirectoryPath, buildPath, "");
        }

        //设置编译器标志
        foreach (XcodeProjectSetting.CompilerFlagsSet compilerFlagsSet in setting.CompilerFlagsSetList)
        {
            foreach (string targetPath in compilerFlagsSet.TargetPathList)
            {
                if (!pbxProject.ContainsFileByProjectPath(targetPath))
                {
                    Debug.Log(targetPath + "编译器标志不能被设置,因为没有这个标志");
                    continue;
                }

                string        fileGuid  = pbxProject.FindFileGuidByProjectPath(targetPath);
                List <string> flagsList = pbxProject.GetCompileFlagsForFile(targetGuid, fileGuid);

                flagsList.Add(compilerFlagsSet.Flags);
                pbxProject.SetCompileFlagsForFile(targetGuid, fileGuid, flagsList);
            }
        }

        //添加所有的 Framework
        foreach (string framework in setting.FrameworkList)
        {
            string libStr = framework;
            bool   weak   = false;
            if (framework.Contains(":"))
            {
                string[] ss = framework.Split(':');
                if (ss.Length > 1)
                {
                    libStr = ss[0];
                    weak   = ss[1] == "weak";
                }
            }
            pbxProject.AddFrameworkToProject(targetGuid, libStr, weak);
        }

        foreach (string lib in setting.LibList)
        {
            string libStr = lib;
            bool   weak   = false;
            if (lib.Contains(":"))
            {
                string [] ss = lib.Split(':');
                if (ss.Length > 1)
                {
                    libStr = ss[0];
                    weak   = ss[1] == "weak";
                }
            }

            pbxProject.AddFrameworkToProject(targetGuid, libStr, weak);
        }

        pbxProject.UpdateBuildProperty(targetGuid, XcodeProjectSetting.LINKER_FLAG_KEY, setting.LinkerFlagArray, null);

        pbxProject.UpdateBuildProperty(targetGuid, XcodeProjectSetting.FRAMEWORK_SEARCH_PATHS_KEY, setting.FrameworkSearchPathArray, null);

        //BitCode
        pbxProject.SetBuildProperty(targetGuid, XcodeProjectSetting.ENABLE_BITCODE_KEY, setting.EnableBitCode ? "YES" : "NO");

        //保存最终工程
        File.WriteAllText(pbxProjPath, pbxProject.WriteToString());

        InfoPlistProcessor.SetApplicationQueriesSchemes(buildPath, setting.ApplicationQueriesSchemes);

        //URL配置
        foreach (XcodeProjectSetting.URLIdentifierData urlData in setting.URLIdentifierList)
        {
            InfoPlistProcessor.SetURLSchemes(buildPath, urlData.name, urlData.URLSchemes);
        }

        //关闭启动图像的已设定默认的设定
        if (setting.NeedToDeleteLaunchiImagesKey)
        {
            InfoPlistProcessor.DeleteLaunchiImagesKey(buildPath);
        }

        //ATS配置
        InfoPlistProcessor.SetATS(buildPath, setting.EnableATS);

        //设置状态栏
        InfoPlistProcessor.SetStatusBar(buildPath, setting.EnableStatusBar);

        //添加特殊的key   比如麦克风//
        foreach (XcodeProjectSetting.AddToInfoStringList data in setting.addStringKeyToPlist)
        {
            InfoPlistProcessor.AddStringKey(buildPath, data.Key, data.value);
        }

        foreach (XcodeProjectSetting.AddCodeData data in setting.AddCodeList)
        {
            string fileName = GetCClassFileName(buildPath, data.FileName);

            if (System.IO.File.Exists(fileName))
            {
                XClass xc = new XClass(fileName);
                xc.WriteStart(data.StartAddCode);
                foreach (XcodeProjectSetting.AddCodeSet addSet in data.addCodes)
                {
                    Debug.LogError(addSet.addFlag + "/需要替换的文件/" + addSet.AddCode);
                    xc.WriteBelow(addSet.addFlag, addSet.AddCode);
                }
                foreach (XcodeProjectSetting.ReplaceCodeSet replaceSet in data.replaceCode)
                {
                    xc.Replace(replaceSet.oldCode, replaceSet.newCode);
                }
            }
        }
        PlayerPrefs.DeleteKey(SETTING_DATA_PATHKEY);
    }