Is it possible to integrate F# in Unity?

I am still very new to Unity and I was able to use F# from within Unity with the help of Unity - Manual: Managed plug-ins .

But I am wondering if it is possible to fully integrate F# into the Unity editor? So that I can write my scripts in F# and drag and drop them directly onto objects.
At the moment I have to call my F# code from a C# script.

I don’t think so as the deployed version builds dll files for each language type used by mono and the webplayer probably has these built in.

E.g. on a PC build in the Data/Managed folder you have Boo.Lang.dll, UnityScript.Lang.dll and C# Assemblies.

So I believe at least at the moment the Unity / Mono build process will not allow F# assemblies to be constructed and used.

Also Unity uses an older version of Mono that might not have F# support built in?

Otherwise I think in theory it could be added, but what would be the benefits to Unity of adding another Language?

does f# have inheritance? if so try having an F# class derive from MonoBehaviour and see if that lets you drag and drop it onto a game object.

Any language that is CLI compliant and can produce DLLs with valid IL, you can use. After all, once it’s been converted to IL, it doesn’t matter what the original language was.

And I know for a fact others have used F# with Unity. You should be able to google it.

Likewise, you should be able to inherit from UnityEditor or UnityEngine just fine. You could even write a post-processor editor script so that when F# files get imported to your project, or otherwise refreshed, they get compiled to DLLs right there.

As far as dragging your scripts to gameobjects as components… I dunno. I don’t see why not but I’m not quite sure how you’d do that. Theoretically you can write your own scene files in text/xml format so you should be able to do what you’re looking for.

Yes I know, I already have a working dll but I still need to call it from c#,which is annoying. I need to add an F# to CLI compiler in Unity but I am not sure how I would do that.

Look at http://docs.unity3d.com/Documentation/ScriptReference/AssetPostprocessor.html

You can write a C# script to compile the F# code when it is added to your project or otherwise refreshed. You get a list of files that have changed and you can pull out the F# ones and run whatever you want on those files. I’ve used it to compile Boo macros that would otherwise have to be compiled seperately, convert files from one format to another, and so on.

That doesn’t solve the issue with dragging components onto gameobjects in the Editor but see my comment above about the scene file format… it should be possible. Also, once the DLL classes are inheriting from MonoBehaviour it should be available in the namespace, so yo ucould right-clight on an object, select add component, and then select your component that way (rather than drag-drop)

When you start digging into the compiler, asset, and editor pipeline I think you’ll see that you can do just about anything… Unity is far more programmer-friendly than most people realize. It’s just not documented as much as the other stuff.

Oh wow, I see. I just tested it.The script is visible in the add scripts GUI if I inherit MonoBehaviour. Many thanks!

A while back there was some work done on using the F# CodeDom provider integrated within Unity. The code worked, but calling the F# compiler was slow (and the CodeDom provider wasn’t really intended for this use case, I think), and there were a few other issues. I will try to dig up the links. I spent some time on updating it, but ended up coming to a similar conclusion as the author.

I prefer to develop all of my F# code in typical OCaml style, in modules. The few MonoBehaviours I need I define in the toplevel namespace, since Unity doesn’t seem to reflect too deeply when it pulls up behavior scripts from your dll, and it won’t display them in the Editor if they are defined in an implicit namespace.

One alternative would be to develop your F# Unity code like some develop their WinForms F# projects, where you use the WinForms GUI designer in Visual Studio to generate some C# GUI boilerplate that ties into your F# code. Likewise, your F# class library could be called by C# MonoBehaviour scripts created ad hoc in the Editor. This might be preferable if you don’t wish to recompile reimport your F# dll every time you make a change.

Ok, found the link: http://translate.google.com/translate?hl=en&sl=ru&u=http://habrahabr.ru/post/151009/&prev=/search%3Fq%3Df%2523%2Bunity3d

4 Likes