Last active
August 9, 2018 01:53
-
-
Save samizzo/e07552d33710f43518f14a6e7f4f94ab to your computer and use it in GitHub Desktop.
A small class to add JSON files to the Visual Studio project that Unity auto-generates.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if ENABLE_VSTU | |
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Xml.Linq; | |
using UnityEditor; | |
using SyntaxTree.VisualStudio.Unity.Bridge; | |
[InitializeOnLoad] | |
public class VisualStudioProjectCustomisation | |
{ | |
// necessary for XLinq to save the xml project file in utf8 | |
class Utf8StringWriter : StringWriter | |
{ | |
public override Encoding Encoding | |
{ | |
get { return Encoding.UTF8; } | |
} | |
} | |
static VisualStudioProjectCustomisation() | |
{ | |
ProjectFilesGenerator.ProjectFileGeneration += OnGenerateProjectFile; | |
} | |
private static string OnGenerateProjectFile(string name, string content) | |
{ | |
var filename = Path.GetFileNameWithoutExtension(name); | |
// parse the document and make some changes | |
var document = XDocument.Parse(content); | |
if (filename == "Assembly-CSharp") | |
{ | |
// Find the <ItemGroup> that contains the files. We search for the first ItemGroup that has any "Compile" entries. | |
var itemGroups = document.Root.Elements().Where(element => element.Name.LocalName == "ItemGroup"); | |
var compileItemGroup = itemGroups.Where(itemGroup => itemGroup.Elements().Any(child => child.Name.LocalName == "Compile")).FirstOrDefault(); | |
if (compileItemGroup != null) | |
{ | |
// Get all json files under the Assets directory. Note that this will also include json files under Editor directories. | |
var assetsDir = Directory.GetCurrentDirectory(); | |
assetsDir = Path.Combine(assetsDir, "Assets"); | |
var jsonFiles = Directory.GetFiles(assetsDir, "*.json", SearchOption.AllDirectories); | |
// Construct a Uri instance for the Assets root. | |
var root = new Uri(assetsDir, UriKind.Absolute); | |
// Add each json file to the project. | |
foreach (var file in jsonFiles) | |
{ | |
// Make the path relative to the Assets directory. | |
var uri = new Uri(file, UriKind.Absolute); | |
var relativePath = root.MakeRelativeUri(uri).ToString(); | |
// VS requires the path separator to be backslash, otherwise it uses the full path of the file as the filename. | |
relativePath = relativePath.Replace("/", "\\"); | |
// Make a new XML node. | |
// Nodes for files that aren't compiled look like this: <None Include="Assets\TextMesh Pro\Resources\Shaders\TMP_SDF.shader" /> | |
var element = new XElement("None"); | |
element.SetAttributeValue("Include", relativePath); | |
compileItemGroup.Add(element); | |
} | |
} | |
} | |
// Save the changes using the Utf8StringWriter | |
var str = new Utf8StringWriter(); | |
document.Save(str); | |
return str.ToString(); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment