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
  1. Create an new Unity project as normal. I’ll refer to its location as /path/to/project for the rest of this tutorial.
  2. 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
  1. Open Xamarin Studio
  2. File > New > Solution…
  3. Click Other on the left side
  4. Choose Library on the right side
  5. Change the Library drop down to “F#”
  6. Click the Next button
  7. Enter your project and solution name to your liking
  8. Change Location to your project: /path/to/project
  9. Click the Create button
Make the Project Use .NET 3.5 and Deploy to Assets
  1. From the top bar, click Project
  2. Click YourProjectName Options from the drop-down
  3. Under Build on the left side, click General
  4. On the right side, change Target framework to Mono/.NET 3.5
  5. Under Build on the left side, click Output
  6. On the right side, change Output path to /path/to/project/Assets
  7. Click OK
Make the Project Use FSharp.Core.dll and UnityEngine.dll
  1. In the Solution pane, click the triangle next to References
  2. Right-click on FSharp.Core and click Delete
  3. Right-click on System.Numerics and click Delete
  4. Right-click on References itself and click Edit References...
  5. Click the .NET Assembly tab
  6. Click the Browse... button
  7. Navigate to /path/to/project/FSharp
  8. Click on FSharp.Core.dll
  9. Click the Open button
  10. Click the Browse... button
  11. Navigate to /Applications/Unity/Unity.app/Contents/Frameworks/Managed
  12. Click on UnityEngine.dll
  13. Click the Open button
  14. Click the OK button
  15. In the Solution pane under References, right-click UnityEngine and click Local Copy to uncheck it
Create a Simple Script
  1. Replace the contents of the Component1.fs file that’s already open with this simple script:
    namespace FSharpUnityTutorial
     
    open UnityEngine
     
    type SimpleScript() = 
        inherit MonoBehaviour()
        member this.Start() = Debug.Log("Hello from F#")
  2. From the top bar, click Build
  3. Click Build All
Try out the Simple Script
  1. Switch back to Unity with your project open
  2. In the Project pane, click the triangle next to YourProjectName
  3. Drag SimpleScript onto Main Camera in the Hierarchy pane
  4. Save the scene
  5. Press the play button to play the scene
  6. Observe that the Console pane has logged Hello from F#
(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:

namespace FSharpUnityTutorial
 
open UnityEngine
 
type SimpleScript() = 
    inherit MonoBehaviour()
 
    // Inspector pane property
    [<SerializeField>]
    let mutable numCubes = 3
 
    // Use the Unity3D API
    let spawnCube x =
        let cube = GameObject.CreatePrimitive(PrimitiveType.Cube)
        cube.transform.position <- new Vector3(x, 0.0f, 0.0f)
 
    member this.Start() =
        // Spawn the number of cubes specified in the Inspector pane
        [1..numCubes] |> List.map (fun x -> float32 x * 1.5f) |> List.iter spawnCube

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.