I'm just putting this here for maybe future use and save somebody the effort. It's mostly about unlocking C# 7.x in Unity but maybe we can add more info as it becomes relevant. Bepu v2 uses the latest C# 7.3 features, and though Unity has some C# 7.x features, they are not enabled by default. V2 wont work in Unity without substantial changes(aka removing C# new features, like replacing all 'in' argument modifiers with 'ref' perhaps) but that is way beyond the scope of this post.
I'm not totally sure if Unity's C# 7 is directly tied to mono but the progress is here https://github.com/mono/mono/issues/6854 and might give an idea for when an attempt is more appropriate.
So to enable C#7 in Unity you need to add a .rsp file to the Assets folder with the line
mcs.rsp
Code: Select all
-langversion:experimental
But we need to set the language in our VS solution and prevent Unity from overwriting it.
If the Visual Studio Unity tools aren't loaded in your Unity project, for VS 2017 load the package located here
C:\Program Files (x86)\Microsoft Visual Studio Tools for Unity\15.0\Visual Studio 2017 Tools.unitypackage
This will create a UnityVS folder with some libraries for controlling how Unity handles VS projects.
Then place the following script titled CsProjFixer.cs in an Editor folder, you should now already have UnityVS/Editor folder. It must be in a folder titled Editor or it wont work, the VS tools are editor only tools.
CsProjFixer.cs
Code: Select all
using System;
using System.IO;
using System.Text;
using SyntaxTree.VisualStudio.Unity.Bridge;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class CsProjFixer
{
static CsProjFixer()
{
ProjectFilesGenerator.ProjectFileGeneration += FixProj;
}
private static String FixProj(String name, String content)
{
try
{
StringBuilder sb = new StringBuilder();
string line = null;
using (var sr = new StringReader(content))
{
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("LangVersion"))
sb.AppendLine(" <LangVersion>latest</LangVersion>");
else
{
sb.AppendLine(line);
}
}
}
return sb.ToString();
}
catch (Exception ex)
{
Debug.LogError("[csProjFixer] - FixProj: Fix Fail for file: " + name);
Debug.LogException(ex);
}
return content;
}
}