I am using the Unity 5.2.2
Every time I click on the “Open C# Project” button on the menu, unity generates the Visual Studio solution and projects, and by default they are called “.CSharp.csproj”
My goal is to auto-modify the generated C# projects’ name by the Unity. For example change them from “trunk.CSharp.csproj” to “MyGame.CSharp.csproj”
I searched on the internet and found materials saying that if I derive a class from the AssetPostprocessor and create a static method OnGeneratedCSProjectFiles, then this method is going to be called by unity when the solution and projects are generated. In this way I can do whatever I can to the generated project and solution files.
So I created a class derived from the AssetPostprocessor and put it into the Editor folder under my project folder.
I tried clicking “Open C# projects” menu item but it seems that my method is not called by the unity.
I can tell that because if my test method is called then a text file is generated on the D drive.
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
namespace Assets.Editor
{
[InitializeOnLoad]
class RXSolutionFixer : AssetPostprocessor
{
[MenuItem("My Editor Window/Test Fix Projects")]
private static void TestFixProejct()
{
OnGeneratedCSProjectFiles();
}
private static void OnGeneratedCSProjectFiles() //secret method called by unity after it generates the solution
{
string currentDir = Directory.GetCurrentDirectory();
string[] csprojFiles = Directory.GetFiles(currentDir, "*.csproj");
var file = new StreamWriter("D:\\Test.txt");
file.WriteLine("Good");
file.Close();
// foreach (var filePath in csprojFiles)
// {
// FixProject(filePath);
// }
}
static bool FixProject(string filePath)
{
string content = File.ReadAllText(filePath);
string searchString = "<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>";
string replaceString = "<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>";
if (content.IndexOf(searchString) != -1)
{
content = Regex.Replace(content, searchString, replaceString);
File.WriteAllText(filePath, content);
return true;
}
else
{
return false;
}
}
}
}
I don’t understand what I do wrong here, can anyone have a look and help me?
Actually the strange thing is that I successfully get the text file generated on D drive, but only once. And after that no matter how I modified my code it stopped working again.