Skip to content

Instantly share code, notes, and snippets.

@MattRix
Created November 24, 2014 19:33
Show Gist options
  • Save MattRix/daf67d66227c61501397 to your computer and use it in GitHub Desktop.
Save MattRix/daf67d66227c61501397 to your computer and use it in GitHub Desktop.
This upgrades your csproj files to .NET 4.0 so you can use default parameters (etc) while building in MonoDevelop
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Collections;
using System;
class RXSolutionFixer : AssetPostprocessor
{
private static void OnGeneratedCSProjectFiles() //secret method called by unity after it generates the solution
{
string currentDir = Directory.GetCurrentDirectory();
string[] csprojFiles = Directory.GetFiles(currentDir, "*.csproj");
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;
}
}
}
@mviranyi
Copy link

That looks amazing! ^^

A work colleague tried something similar and ended up in a infinite loop of fixing and reloading the project 😵 This looks rather correct!

@datacoda
Copy link

datacoda commented Feb 8, 2015

That worked like a charm. Thank you.

@OverworldOwl
Copy link

Thank you so much! Unity kept syncing with MonoDevelop, reverting back to 3.5 and sometimes crashing it in the process. Completely absurd! This worked perfectly though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment