Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save starikcetin/2d5531dd7252d35136230f7a707acb18 to your computer and use it in GitHub Desktop.
Save starikcetin/2d5531dd7252d35136230f7a707acb18 to your computer and use it in GitHub Desktop.
Reference Unity DLLs in standalone C# project

Option 1 - Add a reference directly to the DLLs in Unity installation folder

Create a Directory.Build.props file right next to your .sln file:

<Project>
  <PropertyGroup>
    <UnityDllsPath>path_to_unity_installation\Editor\Data\Managed\</UnityDllsPath>
  </PropertyGroup>
</Project>

Add this to your .csproj:

<ItemGroup>
  <Reference Include="UnityEngine">
    <HintPath>$(UnityDllsPath)\UnityEngine.dll</HintPath>
  </Reference>
  <Reference Include="UnityEditor">
    <HintPath>$(UnityDllsPath)\UnityEditor.dll</HintPath>
  </Reference>
</ItemGroup>

Option 2 - Via Rabadash8820/UnityAssemblies NuGet package

Create a Directory.Build.props file right next to your .sln file:

<Project>
  <PropertyGroup>
    <UnityProjectPath>$(MSBuildProjectDirectory)\relative_path_to_unity_project\</UnityProjectPath>
  </PropertyGroup>
</Project>

The path will be relative from the folder that contains the csproj file. If have different folder structures for different csproj files, you might need to push some or all of that path down to individual csproj files.

Add this to your csproj file:

  <ItemGroup>
    <PackageReference Include="Unity3D" Version="3.0.0" />
    <Reference Include="$(UnityEditorPath)" Private="false" />
  </ItemGroup>

Remove the reference with UnityEditorPath unless you are writing an editor-only DLL. UnityEngine is automatically referenced, you don't have to write anything for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment