Unity and Bepu v2 notes

Discuss topics related to BEPUphysics or physics engine development.
Post Reply
User avatar
cosmixy
Posts: 213
Joined: Fri Jul 07, 2006 6:51 am

Unity and Bepu v2 notes

Post by cosmixy » Fri Jul 13, 2018 7:29 am

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;
    }
}
You might still need to set the language in the VS project the first time. but the script is supposed to take care of it.
The fourth dimension is money.

Norbo
Site Admin
Posts: 4570
Joined: Tue Jul 04, 2006 4:45 am

Re: Unity and Bepu v2 notes

Post by Norbo » Sat Jul 14, 2018 4:45 am

Notably:
The team at Unity is now working with the Roslyn team to adopt the Roslyn C# compiler in Unity. Because Roslyn is a larger compiler, it is a slower compiler to startup, and Unity does many small incremental compilations. So the team is working towards adopting the server compilation mode of Roslyn. This runs the Roslyn C# compiler as a reusable service which can compile code very quickly, without having to pay the price for startup every time.
I'm not sure how far along that work is, but that would hopefully resolve all the language issues.

(SIMD intrinsics related codegen, on the other hand, is still a question mark as far as I know.)

Juzzaga
Posts: 2
Joined: Sun Jan 06, 2019 7:39 pm

Re: Unity and Bepu v2 notes

Post by Juzzaga » Sun Jan 06, 2019 7:46 pm

Hi fellas,

I did the following and added System.Runtime.CompilerServices.Unsafe.dlls to the project.
I got rid of all the errors that Unity was giving me - however when I try to run it Unity crashes. (It's wip naturally)

I thought I should probably ask has anyone actually got v2 working on Unity before spending too much time on it? I would love to use Bepu for networked physics,

Cheers,
- J

Norbo
Site Admin
Posts: 4570
Joined: Tue Jul 04, 2006 4:45 am

Re: Unity and Bepu v2 notes

Post by Norbo » Mon Jan 07, 2019 12:44 am

I'm not sure if anyone has managed it with v2 yet. If you want to try a smaller scale test, you may want to check if the System.Numerics.Vectors types work well yet. It may still be a little tricky- there might be configuration options you'd have to fiddle with to enable the relevant supporting runtimes, if they exist at all.

Getting that to work is a necessary prerequisite to getting v2 to work anyway, so it's a good starting point. I suspect it will run, but I wouldn't be surprised if it still doesn't output optimized results for the System.Numerics.Vectors types (in particular Vector<T>). It might even output assembly significantly worse than naively doing the same computations in a scalar fashion. In other words, without a compiler able to handle those types, performance will tank by an order of magnitude (or two) and there isn't much point in using v2 compared to v1 or another physics engine.

(Still got my fingers crossed that this will become a nonissue at some point!)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest