Skip to content

KuraiAndras/CsprojToAsmdef

Repository files navigation

CsprojToAsmdef Publish Nuget openupm

Generate asmdef files from csproj files inside Unity3D. Features:

  • Use project references from outside or inside of your Unity project
  • Add NuGet packages
  • Support Roslyn analyzers
  • Build Unity code using the .NET CLI. Does not support creating a runnable application

Initialize Unity project

About

The project utilizes a combination of a few hacks. You references Unity assemblies, and UPM packages using the UnityAssemblies project. The common properties are by default stored in Directory.Build.props files. If you don't like this approach you can freely customize your build as long as you provide the expected project properties. The asmdef files for the Unity projects are generated using a .NET CLI tools, which is invoked when building the projects under the Assets folder.

Installation

  • Make sure a recent version of the .NET cli is installed, and dotnet is available from the PATH
  • Use OpenUPM to install the package
  • Install the CsrpojToAsmdef.Cli tool globally
dotnet tool install -g CsrpojToAsmdef.Cli
openupm add com.kuraiandras.csprojtoasmdef
  1. In the Csproj Tools menu click the button: Initialize Unity project
  2. Decide whether to create a gitignore file for the generated DLLs. If you intend to use a UPM package which depends on a NuGet package it is advised to not ignore the DLLs and keep them in version control
  3. Create a new Class Library project inside the Assets folder. Makes sure that the project is using .NET Standard 2.0 (.NET Framework 4.7 should also be supported, but it is not tested)
  4. Add the created csproj to a solution. This preferably should be located above the base unity folder (or higher in hierarchy). Do not use the solution generated by Unity
  5. Click the generate all asmdef files button
  6. Make sure the .bin and .obj folders are ignored in version control
  7. Make sure that .csproj files under the Assets folder are NOT ignored by version control
  8. Reference NuGet packages or other .NET projects
  9. In the Csproj Tools menu click the button: Fix up all projects. Do this every time when you make changes to the project structure, add NuGet packages, add project references or make changes to code located outside of the assets folder.

Project properties and includes

The project largely relies on the UnityAssemblies project. You can reference additional unity assemblies with the project properties described in that project.

You can configure the asmdef generation via csproj properties. When the property should include multiple items each item must be separated with the ';' character. Supported properties:

asmdef csproj
includePlatforms IncludePlatforms
excludePlatforms ExcludePlatforms
allowUnsafeCode AllowUnsafeBlocks
autoReferenced AutoReferenced
defineConstraints DefineConstraints
noEngineReferences NoEngineReferences

Unsupported asmdef properties

  • VersionDefines

Upgrade guide

  • Upgrade the UPM package to the desired version
  • Install the global tool with the same version as the package

Current State

  • VSTU is not supported yet
  • Debugging works, if you attach the Unity debugger manually (Debug -> Attach Unity Debugger)
  • ReSharper (and possibly Rider) works partially
  • VSCode can attach debugger, can't start editor. Debugging editor code is not supported
  • Unity 2019.2 and up is supported
  • Requires a Unity installation. If you want to use this in a CI/CD environment you will need to install the Unity Editor and possibly activate it with a valid license.

Contribute if you can. Fixing the Visual Studio, Visual Studio Code and Rider integration packages seems hard.