GlitchEnzo / NuGetForUnity

A NuGet Package Manager for Unity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: nugets from unity packages

Orcolom opened this issue · comments

Description

Is there a way to support getting nugets defined in a unity package?
The idea would be that we have a central package that includes NugetForUnity and other packages would include nugets if they needed it.

Hi @Orcolom,
thanks for the question.
The easiest way of achieving this is by simply adding a initialization script to your package.

[InitializeOnLoad]
public static class RestorNuGetPackages
{
  static RestorNuGetPackages()
  {
      if (EditorApplication.isPlayingOrWillChangePlaymode || SessionState.GetBool("NuGetPackagesRestored", false))
      {
          return;
      }

      SessionState.SetBool("NuGetPackagesRestored", true);
      RestoreSharedNuGetPackages();
  }

  [MenuItem("Custom/Restore Shared Packages")]
  public static void RestoreSharedNuGetPackages()
  {
    ....
  }
}

I'am not sure how we can integrate a feature that allows this using some sort of configuration. I can only imagine this two options:

  1. I mean one option would be to add a AdditionalPackageConfigFiles configuration value so you add the path to the packages.config of the shared package to the Nuget.config of the consumer project. But this needs configuration in the consumer project so it is no simple setup. Also the path to the packages.config need to be fixed.
  2. Add a Asset importer that triggers the nuget package restore when a packages.config asset is imported. I am not sure if this will be triggered at the correct time. Probably it will also trigger relatively often.

One additional problem that all of this approaches have is the way unity reloads assemblies / assets. So if we have a script that requires a NuGet package in the same assembly as the script that triggers the restore of the NuGet package Unity will not execute the package restore as it has compiler errors. I never tested if e.g. a packages.config file is imported even if the scripts of the containing assembly having compiler errors.