【问题标题】:From Unity to iOS, how to perfectly automate frameworks, settings and plist?从 Unity 到 iOS,如何完美自动化框架、设置和 plist?
【发布时间】:2019-01-25 17:41:55
【问题描述】:

在 Unity3D 中构建 iOS Xcode 项目时,

如何完美地自动化所有三个

  • 框架,
  • 设置,
  • 列出项目?

解决方案必须仅具有 2019 年最现代的语法和变体,因为多年来这在 Unity 中略有变化。

【问题讨论】:

  • 我们能否详细了解这对于 Mac 构建的工作原理?
  • hi @BoredAstronaut 不幸的是我不知道* - 我花了 10 个小时完善下面的 ios 解决方案;尽管我们确实制作了 Mac 版本,但我还没有在 Mac 方面研究过这个过程。对不起!
  • 只是想补充一点,您希望能够修改 plist 以设置 ITSAppUsesNonexemptencryption 标志developer.apple.com/documentation/bundleresources/…

标签: ios unity3d build-automation


【解决方案1】:

2019 年 ...

文件名,BuildPostProcessor.cs

把它放在文件夹 Assets/Editor/ 中。 (如果文件夹“Editor/”不存在,只需将其设置在此处即可。)

这显示了如何完成所有三个框架、设置和 plist。

// filename BuildPostProcessor.cs
// put it in a folder Assets/Editor/
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;

public class BuildPostProcessor {

    [PostProcessBuild]
    public static void ChangeXcodePlist(BuildTarget buildTarget, string path) {

        if (buildTarget == BuildTarget.iOS) {

            string plistPath = path + "/Info.plist";
            PlistDocument plist = new PlistDocument();
            plist.ReadFromFile(plistPath);

            PlistElementDict rootDict = plist.root;

            Debug.Log(">> Automation, plist ... <<");

            // example of changing a value:
            // rootDict.SetString("CFBundleVersion", "6.6.6");

            // example of adding a boolean key...
            // < key > ITSAppUsesNonExemptEncryption </ key > < false />
            rootDict.SetBoolean("ITSAppUsesNonExemptEncryption", false);

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

    [PostProcessBuildAttribute(1)]
    public static void OnPostProcessBuild(BuildTarget target, string path) {

        if (target == BuildTarget.iOS) {

            PBXProject project = new PBXProject();
            string sPath = PBXProject.GetPBXProjectPath(path);
            project.ReadFromFile(sPath);

            string tn = PBXProject.GetUnityTargetName();
            string g = project.TargetGuidByName(tn);

            ModifyFrameworksSettings(project, g);

            // modify frameworks and settings as desired
            File.WriteAllText(sPath, project.WriteToString());
        }
    }

    static void ModifyFrameworksSettings(PBXProject project, string g) {

        // add hella frameworks

        Debug.Log(">> Automation, Frameworks... <<");

        project.AddFrameworkToProject(g, "blah.framework", false);
        project.AddFrameworkToProject(g, "libz.tbd", false);

        // go insane with build settings

        Debug.Log(">> Automation, Settings... <<");

        project.AddBuildProperty(g,
            "LIBRARY_SEARCH_PATHS",
            "../blahblah/lib");

        project.AddBuildProperty(g,
            "OTHER_LDFLAGS",
            "-lsblah -lbz2");

        // note that, due to some Apple shoddyness, you usually need to turn this off
        // to allow the project to ARCHIVE correctly (ie, when sending to testflight):
        project.AddBuildProperty(g,
            "ENABLE_BITCODE",
            "false");
    }

}

这样就可以了。

注意 - 难题的最后一部分是跨文件复制(可能是数据文件或文本文件)。实际上,最好只使用“StreamingAssets/”方法,在this QA 中有详细说明。

【讨论】:

  • GetUnityTargetName 在 2019.3 中已弃用
  • @jestro,好提示 - 恐怕我不知道最新的。如果您知道,请编辑它!
  • 这个 2021 版本是什么?
【解决方案2】:

Unity 2019.3 的固定代码:

// filename BuildPostProcessor.cs
// put it in a folder Assets/Editor/
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;

public class BuildPostProcessor
{

    [PostProcessBuild]
    public static void ChangeXcodePlist(BuildTarget buildTarget, string path)
    {

        if (buildTarget == BuildTarget.iOS)
        {

            string plistPath = path + "/Info.plist";
            PlistDocument plist = new PlistDocument();
            plist.ReadFromFile(plistPath);

            PlistElementDict rootDict = plist.root;

            Debug.Log(">> Automation, plist ... <<");

            // example of changing a value:
            // rootDict.SetString("CFBundleVersion", "6.6.6");

            // example of adding a boolean key...
            // < key > ITSAppUsesNonExemptEncryption </ key > < false />
            rootDict.SetBoolean("ITSAppUsesNonExemptEncryption", false);

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

    [PostProcessBuildAttribute(1)]
    public static void OnPostProcessBuild(BuildTarget target, string path)
    {

        if (target == BuildTarget.iOS)
        {

            PBXProject project = new PBXProject();
            string sPath = PBXProject.GetPBXProjectPath(path);
            project.ReadFromFile(sPath);

            string g = project.GetUnityFrameworkTargetGuid();

            ModifyFrameworksSettings(project, g);

            // modify frameworks and settings as desired
            File.WriteAllText(sPath, project.WriteToString());
        }
    }

    static void ModifyFrameworksSettings(PBXProject project, string g)
    {

        // add hella frameworks

        Debug.Log(">> Automation, Frameworks... <<");

        project.AddFrameworkToProject(g, "blah.framework", false);
        project.AddFrameworkToProject(g, "libz.tbd", false);

        // go insane with build settings

        Debug.Log(">> Automation, Settings... <<");

        project.AddBuildProperty(g,
            "LIBRARY_SEARCH_PATHS",
            "../blahblah/lib");

        project.AddBuildProperty(g,
            "OTHER_LDFLAGS",
            "-lsblah -lbz2");

        // note that, due to some Apple shoddyness, you usually need to turn this off
        // to allow the project to ARCHIVE correctly (ie, when sending to testflight):
        project.AddBuildProperty(g,
            "ENABLE_BITCODE",
            "false");
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-03
    • 2022-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    相关资源
    最近更新 更多