17

I'm getting into Unity, and wondering if I can use F# instead of C#, simply because I prefer the syntax. I'm guessing not, since the environment it runs on is very different, but maybe there's something that will cross-compile code or something else I'm not thinking of.

2

5 Answers 5

41

I can't help but get the impression that the other answers were written by people who do not really know what they are talking about.

This is what you need to do to use F# in a Unity project:

  1. Create an F# project that compiles into a dll. Have it build into the assets folder of a Unity project.
  2. Add references to UnityEngine.dll and/or UnityEditor.dll. Change the reference properties so the dlls aren't copied to the build folder. Of course you can't create new F# classes from within the Unity editor this way, but you can assign the ones you wrote in F#.

Currently you need to enable the (experimental) support for .NET-version 4.5. Alternatively you can download an older version of the FSharp.Core.dll that supports .NET 3.5. (see here).

I can't guarantee that you won't run into trouble because of compatibility issues with the FSharp.Core.dll, but that's just a guess since I haven't tested it myself.

1
  • 1
    This appears to be the best answer based on instructions I've found elsewhere to accomplish exactly what you have described.
    – Greg
    Dec 17, 2017 at 23:55
3

I had success doing this sometime back by following this Github project: https://github.com/eriksvedang/FSharp-Unity, which I believe is roughly the same as Lars Kokemohr's answer above. I haven't tested it in more recent versions of Unity/Mono however.

3

Yes you can use F# as first class language as C# in unity. Here is an article about it link
an excellent blog for One who want to use F# in unity if you want to use F# as second class which I recommend. let me show how to do it.

  1. Usually what I do is to create Game Domain in F# in .net standard 2.0 library project build it.
  2. Make unity API Compatibility level .net standard 2.0 then
  3. Import library project dll in asset/plugins in Unity

Here is an explanation on how to do it automatically after each build : link

You will get this error, here is the solution

Having said that I recommend not to do any serious work in unity with F# because you won't be able to cross compile to other platforms. Unity uses IL2cpp and it don't work with F#. In simple words unity don't support F# and if you are having issues like bugs in Il2cpp unity will not fix it because they don't support F#

-2

There is no easy way to do this but if you REALLY want to make it happen I believe you could use something like fs2cs:

https://github.com/hsharpsoftware/fs2cs

In theory you could write a script that calls fs2cs on all of your Unity C# files. Integrating this with Unity itself would be the more challenging part, but in theory you can trigger fs2cs transpilation on save of your file and Unity can use the transpiled C# file.

-2

Only Boo, C#, and UnityScript are officially supported!

1
  • Currently, only C# is supported. Apr 11, 2021 at 22:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.