techgeek1
3/24/2018 - 6:23 PM

Post processor for excluding shader files from the csproj unity 2018 generates

Post processor for excluding shader files from the csproj unity 2018 generates

using SyntaxTree.VisualStudio.Unity.Bridge;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using UnityEditor;
using UnityEngine;

// Post processor for excluding shader files from the csproj unity 2018 generates
[InitializeOnLoad]
public class CsprojPostProcessor : MonoBehaviour {
    static CsprojPostProcessor() {
        ProjectFilesGenerator.ProjectFileGeneration += ModifyProjectFile;
    }

    private static string ModifyProjectFile(string name, string content) {
        XDocument document = XDocument.Parse(content);

        var itemGroups = document.Root.Descendants()
            .Where(node => node.Name.LocalName == "ItemGroup");

        IEnumerable<XElement> shaderGroups = itemGroups.Select(group => {
            var child = group.Descendants()
                .FirstOrDefault();

            if (child != null && child.Name.LocalName == "None")
                return group;

            return null;
        });

        foreach(XElement group in shaderGroups) {
            group?.RemoveAll();
        }

        return document.ToString();
    }
}