Guest User

Untitled

a guest
Dec 10th, 2025
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | Help | 0 0
  1. # Unity Monorepo
  2.  
  3. How to organize a monorepo structure to have a Unity project alongside other .NET projects.
  4.  
  5. As prerequisites, I would recommend:
  6. 1. Get familiar with Assembly Definitions in Unity — `.asmdef` files: https://docs.unity3d.com/6000.2/Documentation/Manual/assembly-definition-files.html
  7. 2. It's worth digging into the basics of MSBuild syntax — XML format of `.csproj` files, in particular so called “SDK-style project”: https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview#project-files
  8.  
  9. By default, Unity creates a project `Assembly-CSharp.csproj` for all the gameplay C# scripts.
  10. (Set aside plugins and Editor scripts for now.)
  11. Asmdefs allow you to split your code into multiple assemblies, each with its own `.csproj` file.
  12. This is useful for modularization, faster compilation times, and better dependency management.
  13. It's easier to reference this code from outside of Unity.
  14. I'd suggest having at least two assemblies — let's name them `MyGame.UnityAgnostic` and `MyGame.UnityAware`.
  15.  
  16. The first one will be used for code that does not depend on UnityEngine or UnityEditor assemblies (`"noEngineReferences": true`).
  17. This code can be referenced from outside of Unity, for example from a .NET backend app, or from unit tests running in a separate test runner.
  18. The second one will contain the Unity-specific code, and will reference UnityEngine and UnityEditor assemblies.
  19. This code can also be referenced from outside of Unity, but with some caveats and more restrictions.
  20. Scripts outside of these two assemblies will become part of the default assembly `Assembly-CSharp.dll`, which can reference both `MyGame.UnityAgnostic.dll` and `MyGame.UnityAware.dll`.
  21.  
  22. It's better to put as much code as possible into the Unity-agnostic assembly.
  23. Use abstractions, inversion of control, and dependency injection.
  24.  
  25. Unity will generate `.csproj` files for these assemblies, for example `MyGame.UnityAgnostic.csproj` and `MyGame.UnityAware.csproj`.
  26. You should not commit these files to source control, as they are regenerated by Unity.
  27. Instead, you can create your own `.csproj` files like `MyGame.UnityAgnostic.team-shared.csproj`.
  28. Being an "SDK-style project" means you don't need to list all source files explicitly, as all `*.cs` files in the same folder and subfolders are included automatically.
  29.  
  30. Both projects should target `netstandard2.1`.
  31. Although you can add some other target frameworks as well, for example `net10.0`, if you need to use some APIs that are not available in `netstandard2.1` on your backend logic.
  32. In this case you will need to use conditional compilation symbols.
  33.  
  34. The structure of the monorepo can look like this:
  35.  
  36. ```
  37. MyGame-Monorepo/
  38.     MyGame-Unity/
  39.         Assets/
  40.             Scripts/
  41.                 MyGame.UnityAgnostic/
  42.                     MyGame.UnityAgnostic.asmdef
  43.                     MyGame.UnityAgnostic.team-shared.csproj
  44.                     ...
  45.                 MyGame.UnityAware/
  46.                     MyGame.UnityAware.asmdef
  47.                     MyGame.UnityAware.team-shared.csproj
  48.                     ...
  49.         ProjectSettings/
  50.         ...
  51.     MyGame-Dotnet/
  52.         src/
  53.             MyGame.Backend/
  54.                 MyGame.Backend.csproj
  55.         tests/
  56.         MyGame-Dotnet.sln
  57.         ...
  58. ```
  59.  
  60. You can add `MyGame.UnityAgnostic.team-shared.csproj` and `MyGame.UnityAware.team-shared.csproj` projects to your `MyGame-Dotnet.sln` solution file.
  61. And add references to these projects from your .NET projects in `MyGame-Dotnet/`, for example from `MyGame.Backend.csproj`.
Tags: Unity
Advertisement
Add Comment
Please, Sign In to add comment