How to create a class library in Unity/UnityVS/MonoDevelop

I am trying to create a class library for my project. I create the new project in MonoDevelop, and add a reference to my Assembly-CSharp project. Write the code in in MonoDevelop, it compiles fine at both a project and solution level. Everything is referenced properly, there are no warnings or compile errors.

But once I get back into Unity and click Play, I get “The type or namespace name ‘SimulationLibrary’ could not be found. Are you missing a directive or an assembly reference?”

How do I reference other projects so that Unity can “find them” properly? I referenced UnityVS, because I also want this to work in UnityVS so I can use my Resharper abilities and test suites.

The way it currently works with UnityVS is as follows:

  • In Visual Studio, you create a normal class library project
  • You modify its properties to reference the Unity “target framework”
  • You either change the output directory of the class library to be in the Assets folder of your Unity project, or you add a postbuild step to copy the resulting dll and its pdb to the Assets folder
  • You add the class library as an existing project to the UnityVS generated solution

This way you have your class library in your project, UnityVS will transform the .pdb into something Unity understands, so you’ll be able to put breakpoints in your class library on top of your game code.

My guess would be that MonoDevelop doesn’t include all the dependencies so that Unity can access them. Regardless of what actually happens, you should create a DLL of your class library and put that in a Plugins folder within your Unity Project.

This page explains the process: Using Mono DLLs in a Unity Project.

Thanks for you answer @Elenesski. I’m starting to regret splitting my code up into separate libraries!

I have a follow up question, what do you do with the dll.meta files and pdb.meta files that are generated by Unity. If I do a Git clean (or a fresh checkout) then open Unity, it promptly deletes all my .dll.meta and .pdb.meta files from my Assets folder as it can’t find the class library files. Nor does it build as, of course, the class libraries haven’t been built yet. When I do then build the class libraries in Visual Studio, Unity then regenerates all the .meta files with new GUIDs, which I believe will break things.

Is there an elegant way around this? Or do I have to something grim like putting the .meta files next to the dll in the class library and copying them across into the Assets folder too? Do you even need to commit the dll.meta files?

I’m wondering whether I should stop fighting the system. Do most Unity devs just use one big assembly (Assembly-CSharp) for all their code?