Is it possible to modify the Visual Studio solution and project that is generated by unity? What I would like to do is hookup our code analysis to the Visual Studio solution automatically when generating the solution. I can't seem to find a VS project template that Unity uses to generate the project, so I'm not sure if this is even possible.
2 Answers
2You can use an undocumented AssetPostprocessor that is called after the solution synchronization.
This is an example script that fixes the Visual Studio Version Selector Problem in Unity 3.4 (double clicking won't open the solution):
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Text;
public class PostProcessVisualStudioCSProject : AssetPostprocessor
{
static public void OnGeneratedCSProjectFiles () {
// Open the solution file
string projectDirectory = System.IO.Directory.GetParent(Application.dataPath).FullName;;
string projectName = Path.GetFileName(projectDirectory);
string slnFile = Path.Combine(projectDirectory, string.Format("{0}.sln", projectName));
try {
StringBuilder sb = new StringBuilder();
string line = null;
using (StreamReader sr = new StreamReader(slnFile)) {
while ((line = sr.ReadLine()) != null)
sb.AppendLine(line.Replace("
“, Environment.NewLine).Replace(”\r", Environment.NewLine).Replace(“# Visual Studio 2008”, “# Visual Studio 2010”));
}
using (StreamWriter sw = new StreamWriter(slnFile, false, Encoding.UTF8)) {
sw.Write(sb.ToString());
}
}
catch (Exception e) {
Console.WriteLine(“The file could not be read:”);
Console.WriteLine(e.Message);
}
}
}
No. I reckon it is not.
Lucas Meier originally made a script on his blog (see it here) that could generate the visual studio project file.
Since Lucas became a Unity employee, I guess the current built-in VS project and solution generator is derivative of his work.
You can still download his work, make your own generator and call it from your own Editor menu entry. In this way you can support things currently not supported e.g. the AssemblyInfo.cs file and build events.
That link is bad, and could not find it in his GitHub history, but I did find a valid link via archive.org: http://web.archive.org/web/20100110121628/http://lucasmeijer.com/posts/visualstudio-integration-for-unity25-itemtemplates/
– CreepyGnome
Well, the problem is that Unity recreates the solution when any project updates happens (like new scripts are added or old ones have been changed). A template would be nice but i'm not sure if there is one. I just use C# Express so i don't even have it integrated in Unity. Maybe someone else have a solution or workaround.
– Bunny83