Programming for Unity without the IDE

The Unity technology stack is impressive, and while it may be the IDE that makes most users so passionate about it, the playback platforms have some features that really make them stand out. The simple combination of .NET/mono and a cross-platform 3D API, packaged in a stable player, is in itself a noteworthy effort.

In some scenarios, it may even be better to use the playback mechanism directly without going through the Unity IDE:


 * You already have a .NET engine/framework, but are restricted to standalone applications, maybe only on Windows, and want to publish content to other platforms
 * The IDE's framework and workflow forces you into a style you're uncomfortable with
 * You don't want to be locked to a single software vendor, and want an abstraction layer between your application and the underlying player technology, to make it easier to switch between backends (e.g. XNA, Silverlight 3, SlimDX depending on the target platform)

Pattern
The simplest approach to accomplish this is to create a generic "launcher" project, that loads .NET DLLs and relays Unity events to them. This project, and the resulting executables/web.unity3d file, needs to be created only once, and a separate .config file can be used to define which assemblies to load (including where the application's entry point can be found).

I'm still working on a good launcher project that works well for both the desktop and the web, but I'll add the code and its executables to this article once it's in a decent shape.

How to create a launcher
In order to avoid constant use of reflection, it's best to define an interface for the communication. In the experiments I've performed so far, this simple declaration has been sufficient: Create a class library (e.g. in Visual Studio) that contains this single class, and drop the resulting DLL into your Unity project. Start a new project in the Unity IDE, create a GameObject, and attach a script to it. The script's Start method should find and load the needed DLLs (dependencies first!), identify the IEntryPoint class, instantiate it and store the reference. Add Update and OnGUI methods to the script, and call the reference's Update and UnityEvent methods from there respectively.

Now create another class library that implements the IEntryPoint interface, and that references the UnityEngine.dll, and you're pretty much good to go.

Well, this is just a stub, and I've skipped quite a lot of details in this brief description of the process, but if there seems to be an interest (PM me - Jonas B - in the Unity forums) I will add more information.