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? diff --git a/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs b/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs index b5931d59..99b72748 100644 --- a/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs +++ b/src/NuGetForUnity/Editor/Configuration/PackageConfig.cs @@ -16,6 +16,15 @@ 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 (a semicolon ';'). + /// An empty string means no constraints. The default value is an empty string. + /// + 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..8d191f4d 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.IsNullOrWhiteSpace(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..e9194595 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(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); + } + return new[] { ProcessedLabel }; }