Unity 批量设置iOS本地插件编译标志

    [MenuItem("一键打包/设置 -fno-objc-arc")]
    static void 设置编译标志()
    {
        SetCompileFlags("-fno-objc-arc");
        Debug.Log("完成设置编译标志");
    }

    [MenuItem("一键打包/清除 -fno-objc-arc")]
    static void 清除编译标志()
    {
        SetCompileFlags("");
        Debug.Log("完成清除编译标志");
    }

    static void SetCompileFlags(string flags)
    {
        if (Selection.objects.Length != 1)
        {
            Debug.LogError("请选中一个文件夹或者文件");
            return;
        }

        string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
        if(File.Exists(assetPath))
            SetCompileFlags(assetPath, flags);
        else
        {
            string[] includeExtensions = { ".m", ".mm", ".h" };
            foreach (var file in Directory.GetFiles(assetPath, "*.*", SearchOption.AllDirectories).Where(path => includeExtensions.Contains(Path.GetExtension(path))))
            {
                SetCompileFlags(file.Replace(Application.dataPath, "Assets"), flags);
            }
        }
    }

    static void SetCompileFlags(string assetPath, string flags)
    {
        var pluginImporter = AssetImporter.GetAtPath(assetPath) as PluginImporter;
        if (pluginImporter == null || !pluginImporter.GetCompatibleWithPlatform(BuildTarget.iOS))
            return;

        pluginImporter.SetPlatformData(BuildTarget.iOS, "CompileFlags", flags);
        pluginImporter.SaveAndReimport();
    }

为什么我会知道PluginImporter的存在?Unity的资源都有对应的AssetImporter。只需要反射出来取出来看看都有哪些就知道了。

        var type = typeof(AssetImporter);
        Debug.Log(type + " 向下的继承链 : ");

        IEnumerable types = type.Assembly.GetTypes().Where(item => type.IsAssignableFrom(item) && item != type);

        foreach (Type t in types)
            Debug.Log(t);
Unity 批量设置iOS本地插件编译标志_第1张图片
Paste_Image.png

你可能感兴趣的:(Unity 批量设置iOS本地插件编译标志)