Mastering C# and Unity3D

Tutorial: Using F# with Unity3D

One of the advantages of Unity using Mono and IL2CPP as scripting engines is that any .NET language can be used to code your game or app. Today I’ll show an example of that in the form of F#. How do you go about using an unofficially supported language like this? Read on to for the step-by-step tutorial!

I’ll be using Unity 5.2.1f1 on Mac OS X 10.10.5 with Xamarin Studio 5.9.7 for this tutorial. The steps are likely to be the same for versions in the near future, the MonoDevelop IDE, and on Windows, but they’re not covered here. With that disclaimer out of the way, let’s dive in!

Create a Unity Project

Create an new Unity project as normal. I’ll refer to its location as /path/to/project for the rest of this tutorial.

Save the default scene as TestScene

Download the F# Core Library

F# code relies on FSharp.Core.dll. The latest versions of Mono only build this library for .NET 4.0 and up, which Unity doesn’t support. So you’ll need to get the library from an older version of Mono. You can do that yourself, or use my snapshot from Mono 3.12.1. Either way, you’ll want the DLL and its related files:

FSharp.Core.dll

FSharp.Core.dll.mdb

FSharp.Core.optdata

FSharp.Core.sigdata

FSharp.core.xml

If you want to get it from an older version of Mono yourself, those files can be found in /Library/Frameworks/Mono.framework/Versions/X.Y.Z/lib/mono/2.0/ where X.Y.Z is the version of Mono you have installed.

Once you have these files, put them in your Unity project: /path/to/project/FSharp.

Create a Xamarin Studio Project

Open Xamarin Studio

File > New > Solution…

Click Other on the left side

Choose Library on the right side

Change the Library drop down to “F#”

Click the Next button

Enter your project and solution name to your liking

Change Location to your project: /path/to/project

Click the Create button

Make the Project Use .NET 3.5 and Deploy to Assets

From the top bar, click Project

Click YourProjectName Options from the drop-down

Under Build on the left side, click General

On the right side, change Target framework to Mono/.NET 3.5

Under Build on the left side, click Output

On the right side, change Output path to /path/to/project/Assets

Click OK

Make the Project Use FSharp.Core.dll and UnityEngine.dll

In the Solution pane, click the triangle next to References

Right-click on FSharp.Core and click Delete

Right-click on System.Numerics and click Delete

Right-click on References itself and click Edit References...

Click the .NET Assembly tab

Click the Browse... button

Navigate to /path/to/project/FSharp

Click on FSharp.Core.dll

Click the Open button

Click the Browse... button

Navigate to /Applications/Unity/Unity.app/Contents/Frameworks/Managed

Click on UnityEngine.dll

Click the Open button

Click the OK button

In the Solution pane under References, right-click UnityEngine and click Local Copy to uncheck it

Create a Simple Script

Replace the contents of the Component1.fs file that’s already open with this simple script:

(Optional) Do More in the Script

From here on it’s just a matter of doing more in your scripts to implement your game or app. Here’s a little script showing how to integrate with the editor’s Inspector pane and use the Unity API a little bit:

Remember to build the project each time you make changes to your F# scripts.

Conclusion

Integrating F# into Unity is pretty easy. The trickiest part is working around Unity’s old, forked version of Mono. Once you’ve got that one-time setup issue fixed, you’re off to using F# to code your game or app. To learn more about the language itself, I’d recommend checking out F# For Fun and Profit as well as the resources on FSharp.org.

Would you consider using F# in your games and apps? Have you actually used it before? What was your experience like? Let me know in the comments section below.

While IL2CPP has gotten a lot better since its launch, it still has many issues. I recommend filing this one as a bug with Unity. As its name indicates, IL2CPP should be capable of using any IL, not just IL from C# or UnityScript.

That’s a good question and one I haven’t personally tested. MonoDevelop was updated to a very current version with Unity 5.3 and the new version has official support for F#, so there’s a good chance it can also debug F#. I’m just not sure if it will also debug F# inside Unity and as a DLL.