Unity 导出 xcode 工程 并给 Info.plist 文件添加字段

Unity 导出 xcode 工程 并给 Info.plist 文件添加字段

在 Editor 文件夹下新建 xxx.cs 脚本

实现静态方法

csharp 复制代码
    [PostProcessBuild]
    public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
    {
        // Unity 导出 Xcode 工程自动调用这个方法 
    }

例子 一

csharp 复制代码
using UnityEngine;
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;

public class AFUpdatePlist
{
    // 在导出 xcode 工程后调用此方法
    [PostProcessBuild]
    public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
    {
        if (target == BuildTarget.iOS)
        {
            // pathToBuiltProject 是导出的 xcode 工程目录
            // 拼接 Info.plist 文件目录
            string plistPath = pathToBuiltProject + "/Info.plist";
            // 使用 Unity 提供的 plist 文件读取工具解析文件
            PlistDocument plist = new PlistDocument();
            plist.ReadFromString(File.ReadAllText(plistPath));

            // 获取根节点
            PlistElementDict rootDict = plist.root;
            // 设置一个 bool 类型的键值对
            rootDict.SetBoolean("AppsFlyerShouldSwizzle", true);
            // 设置一个字符串类型的键值对
            rootDict.SetString("NSAdvertisingAttributionReportEndpoint", "https://appsflyer-skadnetwork.com/");
            // 修改完毕,将内容写入到文件
            File.WriteAllText(plistPath, plist.WriteToString());
            Debug.Log("Info.plist updated with AppsFlyerShouldSwizzle");
        }

    }
}

例子 二

csharp 复制代码
using UnityEngine;
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;

public class ModifyIOSInfoPlist : Editor
{
    [PostProcessBuild]
    public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
    {
        Debug.Log("ModifyIOSInfoPlist");
        if (target == BuildTarget.iOS)
        {
            Debug.Log("ModifyIOSInfoPlist Info.plist");
            string plistPath = pathToBuiltProject + "/Info.plist";
            PlistDocument plist = new PlistDocument();
            //plist.ReadFromString(File.ReadAllText(plistPath));
            plist.ReadFromFile(plistPath);

            PlistElementDict rootDict = plist.root;
            rootDict.SetString("LSMinimumSystemVersion", "13.0");
            File.WriteAllText(plistPath, plist.WriteToString());
            Debug.Log("Info.plist updated with LSMinimumSystemVersion");
        }

    }
}