Can F# be used with Unity (game engine)? - unity3d

I'm getting into Unity, and wondering if I can use F# instead of C#, simply because I prefer the syntax. I'm guessing not, since the environment it runs on is very different, but maybe there's something that will cross-compile code or something else I'm not thinking of.

I can't help but get the impression that the other answers were written by people who do not really know what they are talking about.
This is what you need to do to use F# in a Unity project:
Create an F# project that compiles into a dll. Have it build into the assets folder of a Unity project.
Add references to UnityEngine.dll and/or UnityEditor.dll. Change the reference properties so the dlls aren't copied to the build folder.
Of course you can't create new F# classes from within the Unity editor this way, but you can assign the ones you wrote in F#.
Currently you need to enable the (experimental) support for .NET-version 4.5. Alternatively you can download an older version of the FSharp.Core.dll that supports .NET 3.5. (see here).
I can't guarantee that you won't run into trouble because of compatibility issues with the FSharp.Core.dll, but that's just a guess since I haven't tested it myself.

I had success doing this sometime back by following this Github project: https://github.com/eriksvedang/FSharp-Unity, which I believe is roughly the same as Lars Kokemohr's answer above. I haven't tested it in more recent versions of Unity/Mono however.

Yes you can use F# as first class language as C# in unity.
Here is an article about it link
an excellent blog for One who want to use F# in unity
if you want to use F# as second class which I recommend.
let me show how to do it.
Usually what I do is to create Game Domain in F# in .net standard
2.0 library project build it.
Make unity API Compatibility level .net standard 2.0 then
Import library project dll in asset/plugins in Unity
Here is an explanation on how to do it automatically after each build : link
You will get this error, here is the solution
Having said that I recommend not to do any serious work in unity with F# because you won't be able to cross compile to other platforms.
Unity uses IL2cpp and it don't work with F#.
In simple words unity don't support F# and if you are having issues like bugs in Il2cpp unity will not fix it because they don't support F#

There is no easy way to do this but if you REALLY want to make it happen I believe you could use something like fs2cs:
https://github.com/hsharpsoftware/fs2cs
In theory you could write a script that calls fs2cs on all of your Unity C# files. Integrating this with Unity itself would be the more challenging part, but in theory you can trigger fs2cs transpilation on save of your file and Unity can use the transpiled C# file.

Only Boo, C#, and UnityScript are officially supported!

Related

How to include Unity3d libs in a non-Unity3d project?

I'm developing a C# library that will be used either as a plugin in some Unity3d projects and also used in non-Unity3d projects.
I need to use some Unity3d classes (such as UnityEngine.Matrix4x4) and standardize some functions to use valid types agnostically to whether the project is an Unity project or not.
So, as the title says: what is the right way to include Unity3d libraries in a non-Unity3d library project? Should I just include reference to the local Unity3d binaries (like UnityEngine.dll) in my project? If so, which is the right folder to look for these binaries (they appear in some different folders in the unity installation folder)?
If you are not actually running the Unity Engine for rendering or handling input it may be the case of forcing a round peg into a square hole, both technically and legally.
The most elegant solution would likely be to write your library in C# using more agnostic libraries. For example the Matrix4x4 class you mentioned has an equivalent in System.Numerics. Alongside the library all thats needed is a light wrapper converting a System.Numerics.Matrix4x4 to a UnityEngine.Matrix4x4. Sometimes the Unity devs themselves do stuff like this, for example the Unity.Mathematics.float3 struct which works better in ECS land than a standard Vector3.
FYI if you're looking for how some particular system works, check out the C# Reference GitHub Repository, for example the Matrix4x4 struct. Just be aware that 'copying and pasting' Unity source code is not allowed.

Finding built-in Unity scripts

If one would like to look into the core files of the Unity Engine, e.g: I've tried using the unity in-built "Fog" effect, but would like to see how it works on a deeper level (code). Is this something one can find, or is it encrypted in some way?
You can try to take a look at Unofficial unity decompiled repo to check if there are sources you're looking for. It's a decompiled verison so there is no guarantee that it's the actual code. Also big part of Unity's sources is written in C++ and C# scripts just call this Unity's native C++ part, so it's really incomplete.
The other option is that some of big companies, Unity's partners which have highest support level, have access to official source code. So may be you're able to find someone with access to sources.

Can protobuf-csharp-port work on webplayer?

We can't call any dll on webplayer, but protobuf-csharp-port uses dll extensions, so seems that it doesn't work on webplayer. Anyone who can solve this problem?
In fact, you can use dlls in the webplayer as long as they contain pure managed c# code. You can't use native dlls written in c++ for example.
These dlls shouldn't use any kind of reflection as it is an excluded sub-assembly in unity webplayer.
I don't know if protobuf-csharp-port matches these constraints but if you are looking for a nice protocol buffer implementation, you can have a look at protobuf-net which is a very nice one, written in pure c#. There is a specific unity-compatible build in the distribution. You can even compile your protobuf serializers in a custom dll so no reflection is used to be webplayer compliant.

Can CodeRush plugins target languages other than C# or VB?

I am trying to write a CodeRush plugin that works similar to the XML Doc Comment Painter.
My plugin, which uses a TileVisual adornment, works fine for decorating comments in C# files, but does not work for comments in F# files.
How can I specify that my plugin should be enabled for F# files?
Have you checked this thread? One of the guys there suggests that
CodeRush is working in F# editor. At least some code assistance and
code templates. But code templates are not defined yet (you have to
create your own).
If you need help with your own plugin, I think you should better contact Devexpress support.
F# language is not supported at the moment, unfortunately. So, you can not enable the plug-in for this language.

Programming Xbox games with Scala

I asked this on the XNA forums but I guess since most people there would specialize in C# I didn't get much help with this
I have some code for a game written in Scala. I compile everything into Java Bytecode at the moment but the Scala compiler has a .Net version. With that in mind, and because modular design is good practice in general, I've written a Graphics interface and right now I use a OpenGLGraphics class implementing this interface which contains every line of OpenGL code in my program. In my perfect world I would be able to swap my OpenGLGraphics class out for an XNAGraphics class and run my game on an Xbox with no further porting, well, maybe some modifying, but I imagine it's not this simple.
I've done some googling and I've failed to find an example of anyone trying this. I do have experience with XNA programming and the first problem that comes to mind is that there is no scala plugin for Visual Studio and XNA programming seems to be very Visual Studio centric, can I at least get visual studio to compile non C# code in an XNA project or setup an XNA project and content pipeline without Visual Studio?
You can configure arbitrary pre- and post-build events in the IDE by creating a C# project, right-clicking the project to open up the properties pages and selecting "Build Events". There should be a "Pre-build event command line" and "Post-build event command line" where you specify arbitrary commands to run as part of the build. However, possibly a better option would be to create MSBuild build files by hand and using the Exec task to run arbitrary commands.
There is some discussion about Scala integration into VS here which also discusses custom MSBuild tasks.
as long as you can point the compiler to the correct version of the XNA assemblies, and it produces a proper CLR assembly, then your best bet would be to refactor all of your game code into a .dll/assembly. Then just make a regular XNA project as normal, reference your scala assembly, and just reference the code from the scala assembly to start up the XNA game.
to be sure, it would take some refactoring, but it sounds like you've already done most of the work by having an abstracted renderer and all that