Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using UnityEditor;
using System.IO;
using System.Linq;
using System.Xml.Linq;
public class VisualStudioProjectPostProcess : AssetPostprocessor {
public static void OnGeneratedCSProjectFiles() {
GenerateSoultion();
}
[MenuItem("Visual Studio/Generate Soultion")]
public static void GenerateSoultion() {
string currentDir = Directory.GetCurrentDirectory();
string[] projects = Directory.GetFiles(currentDir, "*.csproj").Where(proj => !proj.Contains("VisualStudio")).ToArray();
foreach (var file in projects) {
ProcessProject(file);
}
foreach (var file in Directory.GetFiles(currentDir, "*.sln")) {
if (!file.Contains("VisualStudio"))
ProcessSoultion(file, projects);
}
}
private static void ProcessSoultion(string file, string[] projects) {
string slnFile = File.ReadAllText(file);
foreach (var project in projects) {
string projectName = Path.GetFileName(project);
slnFile = slnFile.Replace(projectName, "VisualStudio." + projectName);
}
string newFile = Path.Combine(Path.GetDirectoryName(file), "VisualStudio." + Path.GetFileName(file));
File.WriteAllText(newFile, slnFile);
}
private static void ProcessProject(string file) {
XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
XDocument csproj = XDocument.Load(file);
var hints = csproj.Descendants(ns + "HintPath");
foreach (var hint in hints) {
if (hint.Value.Contains("Unity.app")) {
CopyAssembly(hint.Value);
string winPath = String.Format("Library\\UnityAssemblies\\{0}", Path.GetFileName(hint.Value));
hint.Value = winPath;
}
}
string newFile = Path.Combine(Path.GetDirectoryName(file), "VisualStudio." + Path.GetFileName(file));
csproj.Save(newFile);
}
private static void CopyAssembly(string path) {
var unityAssembliesPath = Path.Combine(Directory.GetCurrentDirectory(), "Library/UnityAssemblies/");
if(!Directory.Exists(unityAssembliesPath))
Directory.CreateDirectory(unityAssembliesPath);
string newPath = Path.Combine(Directory.GetCurrentDirectory(), String.Format("Library/UnityAssemblies/{0}", Path.GetFileName(path)));
File.Copy(path, newPath, true);
if (File.Exists(path + ".mdb")) {
string mdbPath = newPath + ".mdb";
File.Copy(path + ".mdb", mdbPath, true);
}
if (File.Exists(Path.GetFileNameWithoutExtension(path) + ".xml")) {
string xmlPath = Path.GetFileNameWithoutExtension(newPath) + ".xml";
File.Copy(Path.GetFileNameWithoutExtension(path) + ".xml", xmlPath, true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment