美文网首页
unity build iOS 自动添加设置属性

unity build iOS 自动添加设置属性

作者: 小明0326 | 来源:发表于2019-12-18 23:12 被阅读0次

    由于iOS中引入各个静态SDK时,需要引入系统的一些framework和lib,如果每次打包后都手动引入、修改,会很耗时也容易出错,所以就有了本篇文章

有这样的问题,就有这样的解决方案:[PostProcessBuild]   

很多都可以在unity层去设置,例如Info.plist文件,build setting,引入系统库,甚至可以修改oc层面的code

基本的操作就在下面,Talk is cheap, show me the code

public class iOSPostProcessBuild

{

[PostProcessBuild]

    public static void OnPostprocessBuild(BuildTarget buildTarget, string pathToBuildProject)

    {

        if (buildTarget != BuildTarget.iOS) {

            return;

        }

        string proPath = pathToBuildProject + "/Unity-iPhone.xcodeproj/project.pbxproj";

        PBXProject project = new PBXProject();

        project.ReadFromFile(proPath);

        string targetGuid = project.TargetGuidByName("Unity-iPhone");

        project.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-ObjC");

        project.AddFrameworkToProject(targetGuid, "libxml2.2.tbd", false);

        project.WriteToFile(proPath);

        string plistPath = Path.Combine(pathToBuildProject, "Info.plist");

        PlistDocument plist = new PlistDocument();

        plist.ReadFromFile(plistPath);

        PlistElementDict rootDic = plist.root;

        PlistElement nspel;

        if (!rootDic.values.TryGetValue("", out nspel))

        {

            rootDic.SetString("abc", "");

        }

        if (!rootDic.values.TryGetValue("def", out nspel))

        {

            rootDic.SetString("def", "ghi");

        }

        File.WriteAllText(plistPath, plist.WriteToString());

        project.WriteToFile(proPath);

    }

}

只要用心看一下上面的code,相信大家就会明白

但要说的一点是,引入libxml2.2.tbd,因为有些framework供应商提供的文档上写的是引入libxml2.tbd,但此时如果在这里也这样写就不会成功,只能加到项目navigation的引入,link binary with libraries 里面并不会有,这是因为libxml2.tbd 是libxml2.2.tbd的软链如图:

Google,百度查了好久没查到这个,所以今天记录在这里,以备以后查看,也为有同样疑问的人解惑。

如果文中有不当的地方,还请不吝指教,大家共同进步😊

相关文章

网友评论

      本文标题:unity build iOS 自动添加设置属性

      本文链接:https://www.haomeiwen.com/subject/okmzectx.html