I recently started working on an Unity game again and I needed to configure the project structure. I wanted Visual Studio as my main environment since I’m used to the ReSharper goodness it offers and working in Unity is much slower, not to mention the clamped project structure (Assembly-CSharp-vs.proj) – there are many benefits to having many smaller projects.
I use git as my version control system and I followed the instructions on Unity’s website for my first commit and to decide which files to ignore in git. I created some game objects in Unity until I needed some backend classes.
I wanted to work with every class except the ones deriving from MonoBehavior in Visual Studio. So I created a folder named Source beside the Assets folder of the Unity project. Then I created a separate Visual Studio solution in this new folder. The projects in this purely Visual Studio solution will be named similar to the following fashion: GameName.Map, GameName.Randomization, etc. These new projects in the Visual Studio solution need to use .NET 3.5 as target framework so change it in Project Settings -> Application -> Target framework.
The classes in the Visual Studio solution do not need to refer to the classes in the Unity solution and thus will be independent of Unity making them easier to be tested.
The classes in the Unity solution need to be able to use the classes in the Visual Studio solution though. Since the new Visual Studio projects are not inside Unity’s Asset folder, they will not be visible in Unity. Which is a good thing since Unity created meta files for each and every file and folder inside Asset folder in order to track connections between different classes and properties made inside Unity.
To solve this issue, change the build path of the Visual Studio projects that need to be referenced from Unity to a folder inside Unity’s Asset folder, for example “..\..\Assets\Build\Debug\” for Debug configuration (done via Project Settings -> Build -> Output -> Output path). You only need to change the projects that need to be referenced from Unity, so the build path of your utility projects and test projects can be set to outside Assets folder.
After configuring the Visual Studio projects, we need to edit the .gitconfig file so that sln and csproj files for the Visual Studio solution are included in the source control. Here is my latest .gitignore file:
# Created by https://www.gitignore.io ### Unity ### [Ll]ibrary/ [Tt]emp/ [Oo]bj/ [Bb]uild/ # Autogenerated VS/MD solution and project files /*.csproj *.unityproj /*.sln *.suo *.tmp *.user *.userprefs *.pidb *.booproj # Unity3D generated meta files *.pidb.meta # Unity3D Generated File On Crash Reports sysinfo.txt .vs bin obj /Assets/Build.meta