Skip to content

Instantly share code, notes, and snippets.

@ncthbrt
Last active July 7, 2025 09:22
Show Gist options
  • Save ncthbrt/640f876e58a7221e1b39b7a75b74ef14 to your computer and use it in GitHub Desktop.
Save ncthbrt/640f876e58a7221e1b39b7a75b74ef14 to your computer and use it in GitHub Desktop.
Nuget Unity MSBuild Targets Example
<Project DefaultTargets="Restore">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<UnityPluginOutputPath>$(ProjectDir)\Editor\Plugins\</UnityPluginOutputPath>
<SkipCopyBuildProduct>true</SkipCopyBuildProduct>
</PropertyGroup>
<Import Project="NugetUnityPlugins_Presets.targets" />
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
<Import Project="NugetUnityPlugins_Tasks.targets" />
<ItemGroup>
<!-- Put your Nuget dependencies here as per usual. However the UnityGuid is required to ensure a stable meta file -->
<PackageReference Include="FuzzySharp" Version="2.0.2"
UnityGuid="64b010eced4ce40f2a588ce02030c284" />
</ItemGroup>
<!-- This project only exists to copy dependencies to the plugins dir -->
<ItemGroup>
<Compile Remove="**/*.cs" />
</ItemGroup>
<!-- We don't want to build. Only restore packages. Add an empty build target to override the
default one -->
<Target Name="Build"
DependsOnTargets="Restore"
/>
</Project>
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<RestorePackagesPath>.\Temp\$(RootNamespace)\Plugins\</RestorePackagesPath>
<OutDir>.\Temp\$(RootNamespace)\bin\</OutDir>
<BaseIntermediateOutputPath>.\Temp\$(RootNamespace)\obj\</BaseIntermediateOutputPath>
<IntermediateOutputPath>$(BaseIntermediateOutputPath)$(Configuration)\</IntermediateOutputPath>
<RestoreNoCache>true</RestoreNoCache>
</PropertyGroup>
</Project>
<?xml version="1.0" encoding="utf-8"?>
<Project>
<Target Name="CleanDlls" BeforeTargets="Restore">
<ItemGroup>
<NugetFiles Include="$(RestorePackagesPath)**\*" />
</ItemGroup>
<Delete Files="@(NugetFiles)" />
</Target>
<Target Name="CopyDlls" AfterTargets="Restore">
<ItemGroup>
<NugetDlls Include="$(RestorePackagesPath)\*\*\lib\$(TargetFramework)\*.dll" />
</ItemGroup>
<Copy SourceFiles="@(NugetDlls)"
SkipUnchangedFiles="true"
DestinationFolder="$(UnityPluginOutputPath)" />
</Target>
<Target Name="GenerateMetaFiles" Inputs="@(PackageReference)"
Outputs="$(UnityPluginOutputPath)%(Identity.Include).dll.meta"
AfterTargets="CopyDlls">
<PropertyGroup>
<DllMetaTemplate>
fileFormatVersion: 2
guid: %(PackageReference.UnityGuid)
</DllMetaTemplate>
</PropertyGroup>
<ItemGroup>
<!-- Splits the templated metafile into its respective lines -->
<DllMetaFileLine Include="$(DllMetaTemplate.Split(`%0a`))" />
</ItemGroup>
<WriteLinesToFile
File="$(UnityPluginOutputPath)%(PackageReference.Identity).dll.meta"
Overwrite="true"
WriteOnlyWhenDifferent="true"
Lines="@(DllMetaFileLine)" />
</Target>
</Project>
dotnet build ./YourProjectSln.sln
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExamplePackageOnlyCsProj", ""ExamplePackageOnlyCsProj.csproj", "{1012CCAD-2C8B-48F0-A9A5-21DD2AE5CED5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1012CCAD-2C8B-48F0-A9A5-21DD2AE5CED5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1012CCAD-2C8B-48F0-A9A5-21DD2AE5CED5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1012CCAD-2C8B-48F0-A9A5-21DD2AE5CED5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1012CCAD-2C8B-48F0-A9A5-21DD2AE5CED5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment