How to add a Scoped Registry using code?

I am trying to create an easy way to import this package easily in future projects.

It needs to add a Scoped Registry before adding the dependency, and I was wondering how to do it all through code.

Adding the dependency “should be easy” simply by using

Client.Add("com.av.smart-hierarchy");

But I cannot find anywhere how to add a Scope Registry… Does anybody know how to do it?

Thanks in advance for your help and time!

Hey there!

Apologies for the delay in reply. I checked with the team who advise me that this replies on public API calls that is not supported at present, so it will likely not work.

1 Like

I’m facing exactly this issue at the moment

@gillemp @leon-sunday Check out this package, might have some helpful code you can repurpose to build your own way to add scoped registries https://github.com/Halodi/halodi-unity-package-registry-manager

This specifically: https://github.com/Halodi/halodi-unity-package-registry-manager/blob/2fbfd2d15bdb15164d29a8557f274cdf607ee798/Editor/Halodi/PackageRegistry/Core/RegistryManager.cs

1 Like

I had to do this at work and ended up using reflection to call the private unity apis.

There is a Client.AddScopedRegistry if I remember correctly.

1 Like

I ended with a similar solution than @firstuser but this Reflection approach sounds very interesting

Is there a solutions to this, like an API in the Editor?

If you add NewtonsoftJson to your project (available via PackageManager) following code lets you modify the manifest.json that contains the scoped registry:

    public static void AddMyScopedRegistry() {
        AddScopedRegistry(new ScopedRegistry {
            name = "Test Registry",
            url = "https://my.scoped.registry",
            scopes = new string[] {
                "my.scope"
            }
        });
    }

    public static void AddScopedRegistry(ScopedRegistry pScopeRegistry) {
        var manifestPath = Path.Combine(Application.dataPath, "..", "Packages/manifest.json");
        var manifestJson = File.ReadAllText(manifestPath);

        var manifest = JsonConvert.DeserializeObject<ManifestJson>(manifestJson);

        manifest.scopedRegistries.Add(pScopeRegistry);

        File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest, Formatting.Indented));
    }


    public class ScopedRegistry {
        public string name;
        public string url;
        public string[] scopes;
    }

    public class ManifestJson {
        public Dictionary<string,string> dependencies = new Dictionary<string, string>();

        public List<ScopedRegistry> scopedRegistries = new List<ScopedRegistry>();
    }
3 Likes