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.
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.
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.
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.