/FSharp-Unity

Instructions for using F# with Unity

How to use F# libraries with Unity

Note: this has only been tested out with Xamarin Studio on Mac OS X

  1. Create a Unity Project
  2. Create a F# project (lib) and put it in the root of the Unity project
  3. In the Unity 'Assets' folder, create a folder called 'Frameworks' and add the FSharp.Core.dll to it
  4. Open the preferences for the F# project and change the 'Target Framework' to 'Mono / .NET 3.5'
  5. Remove all References in the project, except FSharp.Core (2.3.0), System and System.Core
  6. Add the unity mscorlib.dll to somewhere in the F# project folder, and then to the project References
  7. Make a custom build command (working dir: ${TargetDir}) that copies the resulting dll to the Unity 'Frameworks' folder
    cp MyFsharpLib.dll ../../../../Assets/Frameworks/
  • You can access namespaces/classes from C# components just like you'd expect

How to write Unity components in F#

  • Copy UnityEngine.dll to the F# project and add it as a reference
  • The component will show up under the DLL in Unity's Project view, click the little arrow to fold it out (or just add it to a game object using the Add Component menu)
  • Import the Unity namespace
    open UnityEngine
  • Inherit from MonoBehaviour, as usual
    type SimpleComponent() =
    inherit MonoBehaviour()
        member this.stuff = 42
  • To show properties in the inspector, make them mutable and serializable
    [<SerializeField>]
    let mutable changeSpeed = 2.0f
  • Override member functions
    member this.Start () = 
        this.transform.Translate(1.0f, -1.0f, 2.0f)
  • Mutate members
    this.renderer.material.color <- Color.red

Hey, if you like this repo maybe you'd be interested in the language I'm working on. It's a functional language aimed at game development. Read more about it here: https://github.com/eriksvedang/Carp