I use VS2012 Pro when working with Unity at work. At home, though, I only have MonoDevelop and Unity Free. So, today, I decided to configure MonoDevelop so that it’s a little closer to what I’m used to at work. And that’s when I ran into this problem.
So, to begin, MonoDevelop actually has three places for setting the formatting options, which is very confusing. There are the settings found in Tools->Options->Source Code->Code Formatting. There are the Policy settings under Tools->Custom Policies->Source Code->Code Formatting. Then there are the solution settings found by right-clicking the solution and choosing Options->Source Code->Code Formatting.
It looks like Unity is generating the solution file (.sln) with the solution options already set, and those seem to override any of the others.
Unity generates two .sln files, one is .sln and the other is -csharp.sln. The former is the one you want. Open the .sln file in a format friendly text editor (I use Notepad++) and see this section:
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Assembly-CSharp.csproj
Policies = $0
$0.TextStylePolicy = $1
$1.inheritsSet = null
$1.scope = text/x-csharp
$0.CSharpFormattingPolicy = $2
$2.inheritsSet = Mono
$2.inheritsScope = text/x-csharp
$2.scope = text/x-csharp
$0.TextStylePolicy = $3
$3.FileWidth = 120
$3.TabWidth = 4
$3.EolMarker = Unix
$3.inheritsSet = Mono
$3.inheritsScope = text/plain
$3.scope = text/plain
EndGlobalSection
Notice that line that says “EolMarker = Unix”. That’s probably the culprit in this particular case. But, I really don’t want Unity deciding on those other settings for me either!
What I did was just remove all of the settings from that section and save the file. Make sure that MonoDevelop is closed first. Mine now looks like this:
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Assembly-CSharp.csproj
EndGlobalSection
That got rid of all of the solution overrides, and it looks like everything that I set through the Options menu is being used, including my code formatting options like braces and whitespace.
Another thing I tried was creating a Custom Policy with the settings that I want and then applying it to the solution file. Right click solution, Options->Source Code->Code Formatting, and select your own policy from the drop-down. The solution file will save. Check it out in a text editor again to see the changes.
So far, it hasn’t gotten overwritten when new C# files or JavaScript files files have been added. Adding a JS file where there weren’t any before will add a new project to the solution, which does write to the .sln file, and it still didn’t break.
Hope it lasts.
[NOTE: I’m using Unity 4.2.1f4 Free, and MonoDevelop 2.8.2 that comes with Unity]
I figured out that you need to do the options for each language as well as the overall settings. A very complete answer. Thanks.
– KrissI was just experiencing these same issues in Mono 4. I would set the code formatting in Tools-Options and it was being overridden by the Unity solution settings. So you need to create a custom policy and then right click your solution and select Tools->Apply Policy (then choose your custom policy).
– China