- Home /
Is there a way to stop Unity3 from regenerating the Visual Studio Project file on every compile?
Everytime I make script changes and flip back to Vis Studio 2010, it asks me if I'd like to reload the project, and I have to re-convert every time (to 2010).
Is there a way to stop Unity from constantly regenerating the project file even though no scripts have been added/removed?
Thank you
Answer by killroy · Jul 12, 2011 at 09:33 PM
Very annoying.
I use this small script that I put into Assets/Editor
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Text.RegularExpressions;
class UpgradeVSProject : AssetPostprocessor
{
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
Debug.Log("Reimported Asset");
string currentDir = Directory.GetCurrentDirectory();
string[] slnFile = Directory.GetFiles(currentDir, "*.sln");
string[] csprojFile = Directory.GetFiles(currentDir, "*.csproj");
if(slnFile.Length > 0 && csprojFile.Length > 0)
{
ReplaceInFile(slnFile[0], "Format Version 10.00", "Format Version 11.00");
ReplaceInFile(csprojFile[0], "ToolsVersion=\"3.5\"", "ToolsVersion=\"4.0\"");
ReplaceInFile(csprojFile[0], "<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>", "<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>");
Debug.Log("Upgraded to Visual Studio 2010 Solution");
}
}
static private void ReplaceInFile(string filePath, string searchText, string replaceText)
{
StreamReader reader = new StreamReader(filePath);
string content = reader.ReadToEnd();
reader.Close();
content = Regex.Replace(content, searchText, replaceText);
StreamWriter writer = new StreamWriter(filePath);
writer.Write(content);
writer.Close();
}
}
It waits until Unity is finished with compilation and asset import. At this point VS files should already be generated, so it simply takes the solution and project file and replaces a few strings so that VS2010 doesn't complain. I tested this only with a basic project and on Visual C# Express 2010. It's not the best workaround, but I hope it helps.
Although I had to modify it to loop through all the slnFile and csprojFile ins$$anonymous$$d of just doing the 0th one of each.
Yeah, this is needed with the latest Unity 3.4 as it generates several solution and project files. I'm glad this works for you.
Answer by Noise crime · Jul 10, 2011 at 01:26 AM
Its annoying isn't it.
The workaround i've found is ...
Sync the unity project with VS2010.
Open VS2010, let it convert the project file.
In VS2010, select the solution, then 'save solution as' and append 'VS2010' to the file name.
Select the project in the solution pane and rename it, appending 'VS2010'.
Do a 'save all'.
Quit VS.
In your project folder delete the old none renamed sln, which should leave you with just the renamed sln and csproj files.
You can now open the project via the sln file. Editing the files in vs2010 or unity editor should no longer force a re-convert. However you will have to repeat this process everytime you add a new script via Unity, but not if you add one via VS2010.
Answer by michiel_frankfort · Jan 19, 2012 at 12:12 PM
Thanks Killroy for your help, your script was very usefull!
But there it does not entirely convert the project and I had to adjust your script to make it run properly. Besides, after I got it to work, i get a reload prompt each time I switchback to VisualStudio. So I created a little version-check to the script, that only performs the conversion when versions missmatch. It makes life a whole lot easier and thats why I want to share this super-solid update with you:"
using UnityEngine;
using System.Collections; using UnityEditor; using System.IO; using System.Text.RegularExpressions;
class UpgradeVSProject : AssetPostprocessor { private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { string currentDir = Directory.GetCurrentDirectory();
string[] slnFile = Directory.GetFiles(currentDir, ".sln"); string[] csprojFile = Directory.GetFiles(currentDir, ".csproj");
bool hasChanged = false;
if (slnFile != null)
{
for (int i = 0; i < slnFile.Length; i++)
{
if (ReplaceInFile(slnFile[i], "Format Version 10.00", "Format Version 11.00"))
hasChanged = true;
}
}
if (csprojFile != null)
{
for (int i = 0; i < csprojFile.Length; i++)
{
if (ReplaceInFile(csprojFile[i], "ToolsVersion=\"3.5\"", "ToolsVersion=\"4.0\""))
hasChanged = true;
if (ReplaceInFile(csprojFile[i], "<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>", "<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>"))
hasChanged = true;
}
}
if (hasChanged)
{
Debug.LogWarning("Project is now upgraded to Visual Studio 2010 Solution!");
}
else
{
Debug.Log("Project-version has not changed...");
}
}
static private bool ReplaceInFile(string filePath, string searchText, string replaceText)
{
StreamReader reader = new StreamReader(filePath);
string content = reader.ReadToEnd();
reader.Close();
if (content.IndexOf(searchText) != -1)
{
content = Regex.Replace(content, searchText, replaceText);
StreamWriter writer = new StreamWriter(filePath);
writer.Write(content);
writer.Close();
return true;
}
return false;
}
}
Hey $$anonymous$$ichiel,
That is great! Thanks for the update. Yes, reload prompt is annoying, because the sln is regenerated every time you do something in Unity. This version check is a great idea!
Hey michiel_frankfort you should fix the formatting of your answer :). Thanks!