简体   繁体   English

从Unity到iOS,如何完美自动化框架、设置和plist?

[英]From Unity to iOS, how to perfectly automate frameworks, settings and plist?

in Unity3D when building to an iOS Xcode project,在 Unity3D 中构建到 iOS Xcode 项目时,

how to perfectly automate all three of如何完美地自动化所有三个

  • frameworks,构架,
  • settings,设置,
  • plist items? plist项目?

Solution must have only the most modern 2019 syntax and variations, as this has changed slightly in Unity over the years.解决方案必须只有最现代的 2019 语法和变体,因为这些年来 Unity 中的这一点略有变化。

For 2019 ...对于 2019 年...

在此处输入图片说明

Filename, BuildPostProcessor.cs文件名,BuildPostProcessor.cs

Put it in the folder Assets/Editor/ .把它放在文件夹 Assets/Editor/ 中。 (Just make the folder "Editor/" exactly there if it does not exist.) (如果它不存在,只需将文件夹“Editor/”准确地放在那里。)

This shows how to do all three of frameworks, settings and plist.这展示了如何完成所有三个框架、设置和 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");
    }

}

That will do it.这样就可以了。

Note - the final part of the puzzle is copying across files (perhaps data files or text files).注意 - 拼图的最后一部分是跨文件复制(可能是数据文件或文本文件)。 In reality it's best to just use the "StreamingAssets/" approach, explained fully in this QA .实际上,最好只使用“StreamingAssets/”方法,在本 QA 中有完整的解释。

Fixed code for Unity 2019.3: 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");
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM