Today’s tutorial gives step-by-step instructions on how to use F# as a programming language in Unity. It updates an older tutorial from 2015 that used Unity 5.2 because a lot has changed in Unity since then. With an improved IL2CPP and support for .NET Standard 2.0, it’s easier than ever to simply drop in F# support. Read on to learn how!

The following steps walk you through setting up a Unity project with both C# and F# code. The C# code will be able to call F# code and visa versa. The F# code will also be able to use Unity APIs. Command line steps are used to set up the project, but aren’t necessary afterward. The resulting build is for macOS using IL2CPP, but other environments will work just as well.

  1. Install the .NET Core SDK

Install .NET Core SDK

  1. Create a Unity project. I’ll use temp/UnityProject in my home directory.

Create Unity Project

  1. In the Project pane, click Create then C# Script and press the return key

Create C# Script

  1. Double-click NewBehaviourScript to open it in Visual Studio

Default NewBehaviourScript

  1. Replace the code with this, save the file, focus the Unity editor window, and wait for the spinner to indicate that C# has been built
public static class CSharpMath
{
    public static int Add(int a, int b)
    {
        return a + b;
    }
}

Build NewBehaviourScript

  1. Open the Terminal app from /Applications/Utilities/Terminal

Open Terminal

  1. Enter cd ~/temp/UnityProject to go to the Unity project directory

Go to Unity Project

  1. Enter mkdir FSharp to create a directory for your F# project.

Make F# Project

  1. Enter cd FSharp to go to your F# project

  1. Enter dotnet new classlib -lang F# to initialize the F# project for building a DLL

Init F# Project

  1. Enter dotnet publish to build the F# project

Publish F# Project

  1. Enter cp bin/Debug/netstandard2.0/publish/FSharp.Core.dll ../Assets/ to copy the F# language DLL to the Unity project

Copy FSharpCore

  1. Enter open FSharp.fsproj to open the F# project in Visual Studio

Open F# Project

Opened F# Project

  1. In the Solution pane, right-click on FSharp > FSharp > Dependencies and click Edit References...

Edit References

  1. Click the .NET Assembly tab and then click the Browse... button

Browse Assemblies

  1. Navigate up to the UnityProject directory, choose Library/ScriptAssemblies/Assembly-CSharp.dll, and click Open

Add AssemblyCSharp

Added AssemblyCSharp

  1. Click the Browse... button again. Press Cmd+Shift+G, enter the path to your Unity install directory, and click Go

Browse Unity Directory

  1. Navigate into Contents/Managed/UnityEngine

Navigate To Unity DLLs

  1. Select all the files ending in .dll and click Open. It helps to sort the Kind column.

Select Unity DLLs

Added Unity DLLs

  1. Click the OK button and wait for it to “restore” all the “packages”.

Packages Restored

  1. Double-click Library.fs to open its F# source

Default Library

  1. Replace the code with this and save the file:
namespace FSharpUnityTutorial
 
open UnityEngine
 
type SimpleScript() =
    inherit MonoBehaviour()
    member this.Start() = Debug.Log("F# " + CSharpMath.Add(2, 5).ToString())
 
type FSharpMath() =
    static member this.Add(a, b) = a + b

Edited Library

  1. In the Solution pane, right-click on FSharp > FSharp and click Options

F# Options

  1. Click Custom Commands on the left side

Custom Commands

  1. Click (Select a project operation) on the right side then After Build

After Build

  1. Enter cp ${TargetPath} ${SolutionDir}/../Assets/ into the Command field and click OK

After Build Command

  1. Press Cmd+B to build the F# project

Built F# Project

  1. Open the Unity editor window and wait for the spinner to indicate that C# and F# have been built

Built C# and F#

  1. In the Project pane, click on the arrow next to FSharp and drag SimpleScript onto Main Camera in the Hierarchy pane. Press Cmd+S to save the Unity project.

Attach SimpleScript

  1. Press the Play button and observe the Console pane. It should say F# 7. This confirms that F# is running successfully.

F# Running

  1. In the Project pane, click Create then C# Script. Type CallFSharp and press the return key.

Create CallFSharp

  1. Double-click CallFSharp to open it in Visual Studio. Replace the code with this and save the file:
using UnityEngine;
using FSharpUnityTutorial;
 
public class CallFSharp : MonoBehaviour
{
    void Start()
    {
        Debug.Log("C# " + FSharpMath.Add(20, 22).ToString());
    }
}

Replaced CallFSharp

  1. Open the Unity editor window and wait for the spinner to indicate that C# and F# have been built

Built CallFSharp

  1. In the Project pane, drag CallFSharp onto Main Camera in the Hierarchy pane. Press Cmd+S to save the Unity project.

Attach CallFSharp

  1. Press the Play button and observe the Console pane. It should say C# 42 and F# 7. This confirms that C# can call F# successfully.

C# and F# Running

  1. From the macOS top bar, click Edit then Project Settings.... Click Player on the left side.

Player Settings

  1. Click the drop-down for Scripting Backend and choose IL2CPP. Close the player settings window.

Choose IL2CPP

  1. From the macOS top bar, click File then Build And Run. Enter UnityProject_macOS and click Save. Wait for the build to finish and run.

Build and Run

Running

  1. Close the running game window and open the Console app from /Applications/Utilities/Console. Click ~/Library/Logs on the left side then Player.log on the right side. It should say C# 42 and F# 7. This confirms that non-editor builds and IL2CPP are working successfully.

Mac Console

From here on out you can simply edit and build the F# source in Visual Studio and click Play in the Unity editor to see the changes. Enjoy F#!