Programming in c#; how do you do it with unity?

Ok, I get it. Despite the absolute fail in explaining this at any level in the tutorials:

  • You can create script objects that extend MonoBehaviour.
  • You can have other generic C# files, fully namespaced and all and import and use them.
  • However, only MonoBehaviour derived classes can be bound to assets.
  • You can bind a script to an asset by (and only by) dragging-dropping the script over the asset.

All good.

However, I dont get is:

  1. How you can write a c# assembly and then import it into unity?

(Yes, I have no problem building a mono assembly, I just don’t understand how to import it)

  1. What’s the best way to apply good programming practice like unit tests and IoC in unity?

  2. Is there some way I can pull the UnityEngine assemblies out and work with them, so I can compile code using VS / command line / etc?

(re: 3: specifically for autocompletion in VS for the UnityEngine namespace)

thanks!

  1. Create a new C# library. Add references to UnityEngine.dll and/or UnityEditor.dll. These are found inside the Unity installation folder. Then place compiled dll into your Unity project as any other asset.

  2. Unity dependent code can’t be unit tested off-engine since it require the engine to be running. You can either find an existing testing framework or create your own. Personally, I only test stuff that are hard to get right the first attempt as coding in Unity is pretty trivial and I feel comfortable coding without testing in many cases. IoC is another story. I guess in the traditional sense, there aren’t any existing solutions specifically for Unity. Maybe there are, otherwise you’re up to do the coding yourself. Using components and the inspector binding to abstract component types can be one way of getting away with a kind of IoC if you feel like it, and I guess it’s quite a standard approach.

  3. Yes, just add the UnityEngine.dll reference to your project. You won’t be able to run the code unless it’s played through Unity.