From 1d35112cda51e249945ee4374a543d13eb6bd3f5 Mon Sep 17 00:00:00 2001 From: Masatake Iwasaki <78918084+kochounoyume@users.noreply.github.com> Date: Fri, 17 Oct 2025 00:06:54 +0900 Subject: [PATCH 1/7] Add DefineConstraints property to PackageConfig --- src/NuGetForUnity/Editor/Configuration/PackageConfig.cs | 2 ++ .../Editor/Configuration/PackagesConfigFile.cs | 8 ++++++++ src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs b/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs index b5931d59..282f6a70 100644 --- a/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs +++ b/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs @@ -16,6 +16,8 @@ public class PackageConfig : NugetPackageIdentifier /// public bool AutoReferenced { get; set; } = true; + public string DefineConstraints { get; set; } = string.Empty; + /// /// 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'). diff --git a/src/NuGetForUnity/Editor/Configuration/PackagesConfigFile.cs b/src/NuGetForUnity/Editor/Configuration/PackagesConfigFile.cs index b0b08b55..9d609b69 100644 --- a/src/NuGetForUnity/Editor/Configuration/PackagesConfigFile.cs +++ b/src/NuGetForUnity/Editor/Configuration/PackagesConfigFile.cs @@ -22,6 +22,8 @@ public class PackagesConfigFile private const string AutoReferencedAttributeName = "autoReferenced"; + private const string DefineConstraintsAttributeName = "defineConstraints"; + private const string TargetFrameworkAttributeName = "targetFramework"; [CanBeNull] @@ -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, }; configFile.Packages.Add(package); @@ -148,6 +151,11 @@ public void Save() packageElement.Add(new XAttribute(AutoReferencedAttributeName, package.AutoReferenced)); } + if (!string.IsNullOrEmpty(package.DefineConstraints)) + { + packageElement.Add(new XAttribute(DefineConstraintsAttributeName, package.DefineConstraints)); + } + if (!string.IsNullOrEmpty(package.TargetFramework)) { packageElement.Add(new XAttribute(TargetFrameworkAttributeName, package.TargetFramework)); diff --git a/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs b/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs index 6d747892..190d70ff 100644 --- a/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs +++ b/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs @@ -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(';', StringSplitOptions.RemoveEmptyEntries); + } + return new[] { ProcessedLabel }; } From c73edb235d1a78b1278648d2867d1fb6802edf6e Mon Sep 17 00:00:00 2001 From: Masatake Iwasaki <78918084+kochounoyume@users.noreply.github.com> Date: Fri, 17 Oct 2025 00:17:30 +0900 Subject: [PATCH 2/7] Fix DefineConstraints split to use string literal for delimiter --- src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs b/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs index 190d70ff..7def8446 100644 --- a/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs +++ b/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs @@ -294,7 +294,7 @@ private static string[] ModifyImportSettingsOfGeneralPlugin([NotNull] PackageCon if (!string.IsNullOrWhiteSpace(packageConfig.DefineConstraints)) { - plugin.DefineConstraints = packageConfig.DefineConstraints.Split(';', StringSplitOptions.RemoveEmptyEntries); + plugin.DefineConstraints = packageConfig.DefineConstraints.Split(";", StringSplitOptions.RemoveEmptyEntries); } return new[] { ProcessedLabel }; From ac83bf236819d82f6299b492aa3b4eeb1036081e Mon Sep 17 00:00:00 2001 From: Masatake Iwasaki <78918084+kochounoyume@users.noreply.github.com> Date: Fri, 17 Oct 2025 00:23:44 +0900 Subject: [PATCH 3/7] Fix DefineConstraints split to use char array for delimiter --- src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs b/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs index 7def8446..e9194595 100644 --- a/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs +++ b/src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs @@ -294,7 +294,7 @@ private static string[] ModifyImportSettingsOfGeneralPlugin([NotNull] PackageCon if (!string.IsNullOrWhiteSpace(packageConfig.DefineConstraints)) { - plugin.DefineConstraints = packageConfig.DefineConstraints.Split(";", StringSplitOptions.RemoveEmptyEntries); + plugin.DefineConstraints = packageConfig.DefineConstraints.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); } return new[] { ProcessedLabel }; From 63d319f58892719372956d14e02902e5010d770c Mon Sep 17 00:00:00 2001 From: Masatake Iwasaki <78918084+kochounoyume@users.noreply.github.com> Date: Fri, 17 Oct 2025 00:53:59 +0900 Subject: [PATCH 4/7] Update README --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 0e7813e9..d3599fc1 100644 --- a/README.md +++ b/README.md @@ -301,6 +301,16 @@ To disable the automatic referencing of assemblies of a NuGet package you can se ``` +## 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 + + + + +``` + 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? From 94f4f893b3a514e915e21045d6ddd8c291a91b1c Mon Sep 17 00:00:00 2001 From: Masatake Iwasaki <78918084+kochounoyume@users.noreply.github.com> Date: Fri, 17 Oct 2025 02:31:14 +0900 Subject: [PATCH 5/7] Add comment --- src/NuGetForUnity/Editor/Configuration/PackageConfig.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs b/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs index 282f6a70..7ed55abe 100644 --- a/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs +++ b/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs @@ -16,6 +16,13 @@ public class PackageConfig : NugetPackageIdentifier /// public bool AutoReferenced { get; set; } = true; + /// + /// 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 (for example, a semicolon ';'); the exact delimiter and parsing + /// rules are determined by the consumer of this property. An empty string means no constraints. The default value is an empty string. + /// public string DefineConstraints { get; set; } = string.Empty; /// From 7c7d8166949c29a8e04dd8bc895f20dd55cf84e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Stojkovi=C4=87?= Date: Sun, 19 Apr 2026 11:52:59 +0200 Subject: [PATCH 6/7] Update src/NuGetForUnity/Editor/Configuration/PackageConfig.cs Co-authored-by: JoC0de <53140583+JoC0de@users.noreply.github.com> --- src/NuGetForUnity/Editor/Configuration/PackageConfig.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs b/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs index 7ed55abe..99b72748 100644 --- a/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs +++ b/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs @@ -20,8 +20,8 @@ public class PackageConfig : NugetPackageIdentifier /// 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 (for example, a semicolon ';'); the exact delimiter and parsing - /// rules are determined by the consumer of this property. An empty string means no constraints. The default value is an empty string. + /// can be combined using a delimiter (a semicolon ';'). + /// An empty string means no constraints. The default value is an empty string. /// public string DefineConstraints { get; set; } = string.Empty; From 278733154684307d8ddff01483e6c39109ba23ee Mon Sep 17 00:00:00 2001 From: JoC0de <53140583+JoC0de@users.noreply.github.com> Date: Sun, 19 Apr 2026 11:59:19 +0200 Subject: [PATCH 7/7] use IsNullOrWhiteSpace instead of IsNullOrEmpty Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/NuGetForUnity/Editor/Configuration/PackagesConfigFile.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NuGetForUnity/Editor/Configuration/PackagesConfigFile.cs b/src/NuGetForUnity/Editor/Configuration/PackagesConfigFile.cs index 9d609b69..8d191f4d 100644 --- a/src/NuGetForUnity/Editor/Configuration/PackagesConfigFile.cs +++ b/src/NuGetForUnity/Editor/Configuration/PackagesConfigFile.cs @@ -151,7 +151,7 @@ public void Save() packageElement.Add(new XAttribute(AutoReferencedAttributeName, package.AutoReferenced)); } - if (!string.IsNullOrEmpty(package.DefineConstraints)) + if (!string.IsNullOrWhiteSpace(package.DefineConstraints)) { packageElement.Add(new XAttribute(DefineConstraintsAttributeName, package.DefineConstraints)); }