Skip to content

Instantly share code, notes, and snippets.

@MattRix
Created January 15, 2015 02:44
Show Gist options
  • Save MattRix/0bf8de88e16e8b494dbb to your computer and use it in GitHub Desktop.
Save MattRix/0bf8de88e16e8b494dbb to your computer and use it in GitHub Desktop.
Updates the solution .NET version and removes UnityScript projects
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System;
class SolutionFixer : AssetPostprocessor
{
public static bool SHOULD_REMOVE_UNITYPROJ = true;
private static void OnGeneratedCSProjectFiles() //secret method called by unity after it generates the solution
{
string currentDir = Directory.GetCurrentDirectory();
string[] slnFiles = Directory.GetFiles(currentDir, "*.sln");
string[] csprojFiles = Directory.GetFiles(currentDir, "*.csproj");
foreach(var filePath in slnFiles)
{
if(SHOULD_REMOVE_UNITYPROJ) FixSolution(filePath);
}
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;
}
}
//we will remove all unityproj references
static bool FixSolution(string filePath)
{
string content = File.ReadAllText(filePath);
bool didChange = false;
int nextIndex = 0;
while(true)
{
int projStartIndex = content.IndexOf("Project(",nextIndex);
if(projStartIndex == -1) break; //no more projects
int projEndIndex = content.IndexOf("EndProject",projStartIndex);
if(projEndIndex == -1) break; //unmatched opening project
string projectString = content.Substring(projStartIndex,projEndIndex-projStartIndex);
if(projectString.IndexOf(".unityproj") > -1) //is unityproj
{
content = content.Remove(projStartIndex,(projEndIndex-projStartIndex)+10);//remove the whole project tag if unityproj is found
didChange = true;
}
else
{
nextIndex = projEndIndex;
}
}
if(didChange)
{
File.WriteAllText(filePath,content);
return true;
}
else
{
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment