Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,16 @@ To disable the automatic referencing of assemblies of a NuGet package you can se
</packages>
```

## Define constraints
To restrict when a NuGet package is referenced based on Scripting Define Symbols, you can set the `defineConstraints` attribute of a package inside the `packages.config`. All defines listed in `defineConstraints` must be present for the package assemblies to be referenced, just like in Unity’s `.asmdef` files. If the attribute is omitted, the package is always referenced.
_Currently this setting is not available from UI._
```xml
<?xml version="1.0" encoding="utf-8" ?>
<packages>
<package id="Serilog" version="2.12.0" autoReferenced="false" defineConstraints="UNITY_EDITOR;DEVELOPMENT_BUILD" />
</packages>
```

When this setting is set to `false` the assemblies of the NuGet package are only referenced by Unity projects that explicitly list them inside there `*.asmdef` file.

# How do I create my own NuGet packages from within Unity?
Expand Down
9 changes: 9 additions & 0 deletions src/NuGetForUnity/Editor/Configuration/PackageConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ public class PackageConfig : NugetPackageIdentifier
/// </summary>
public bool AutoReferenced { get; set; } = true;

/// <summary>
/// Gets or sets additional compile-time constraints associated with this package.
/// This property contains a string representing extra compile-time constraints related to the package,
/// such as conditional compilation symbols or conditions for applying the package. Multiple constraints
/// can be combined using a delimiter (a semicolon ';').
/// An empty string means no constraints. The default value is an empty string.
/// </summary>
public string DefineConstraints { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the configured target framework moniker that is used to install this NuGet package instead of
/// automatically determining the best matching target framework from the Unity settings ('Api Compatibility Level').
Expand Down
8 changes: 8 additions & 0 deletions src/NuGetForUnity/Editor/Configuration/PackagesConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class PackagesConfigFile

private const string AutoReferencedAttributeName = "autoReferenced";

private const string DefineConstraintsAttributeName = "defineConstraints";

private const string TargetFrameworkAttributeName = "targetFramework";

[CanBeNull]
Expand Down Expand Up @@ -73,6 +75,7 @@ public static PackagesConfigFile Load()
packageElement.Attribute("manuallyInstalled")?.Value.Equals("true", StringComparison.OrdinalIgnoreCase) ?? false,
AutoReferenced = (bool)(packageElement.Attributes(AutoReferencedAttributeName).FirstOrDefault() ??
new XAttribute(AutoReferencedAttributeName, true)),
DefineConstraints = packageElement.Attribute(DefineConstraintsAttributeName)?.Value ?? string.Empty,
TargetFramework = packageElement.Attribute(TargetFrameworkAttributeName)?.Value,
};
Comment thread
JoC0de marked this conversation as resolved.
configFile.Packages.Add(package);
Expand Down Expand Up @@ -148,6 +151,11 @@ public void Save()
packageElement.Add(new XAttribute(AutoReferencedAttributeName, package.AutoReferenced));
}

if (!string.IsNullOrWhiteSpace(package.DefineConstraints))
{
packageElement.Add(new XAttribute(DefineConstraintsAttributeName, package.DefineConstraints));
}

if (!string.IsNullOrEmpty(package.TargetFramework))
{
packageElement.Add(new XAttribute(TargetFrameworkAttributeName, package.TargetFramework));
Expand Down
5 changes: 5 additions & 0 deletions src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ private static string[] ModifyImportSettingsOfGeneralPlugin([NotNull] PackageCon
{
PluginImporterIsExplicitlyReferencedProperty.SetValue(plugin, !packageConfig.AutoReferenced);

if (!string.IsNullOrWhiteSpace(packageConfig.DefineConstraints))
{
plugin.DefineConstraints = packageConfig.DefineConstraints.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
}
Comment on lines +295 to +298
Copy link
Copy Markdown
Collaborator

@JoC0de JoC0de Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point two options:

  1. We accept this behaviour so user can manually set DefineConstraints in the unity settings (if he commits the .meta file to git) even when he doesn't use the setting in package-config. But unfortunately this will lead to the behaviour that a if a user uses the setting and later removes it from package.config it will not be applied.
  2. We use the suggestion from Copilot -> a user that has set DefineConstraints in the .meta manually needs to mirror the setting to the package.config


return new[] { ProcessedLabel };
}

Expand Down
Loading