除了 Unity 默认注册表之外,您还可以使用范围注册表来托管您自己的包。使用范围注册表可确保 Package Manager 始终将包映射到一个注册表,并且仅将其映射到一个注册表,从而无论网络状况如何,都可以确保结果一致。
例如,如果您设置了自己的服务器来用于托管某些 Unity 包,但某一个注册表临时不可用,那么您可能最终会从错误的注册表获取包,即使 Package Manager 始终按照相同顺序来搜索注册表。但是,如果您为自己的服务器设置范围注册表,则包将始终映射到一个注册表,并且仅映射到一个注册表,无论网络状况如何,都可以保证结果一致。
Unity Package Manager 支持基于 npm 协议的注册表。您可以使用任何现成的 npm 注册表服务器,都应该有效,但是 Verdaccio 可以快速设置并且不需要很多配置。
一旦设置了这些服务器,便可以将它们包含为范围注册表,也就是 npm 使用的同一个概念。
一些 npm 注册表服务器不支持使用 /all
Web API 路由来搜索所有包。Package Manager 搜索 API 依靠配置的范围注册表来支持这个旧的 npm API 协议。它有一个 HTTP 终端,该终端返回所有已发布包的元数据(例如,https://registry.my-company.com/-/all
)。
If you have a tool that uses the Package Manager API to list available packages, you might experience unexpected results if your registry does not support the old protocol. For example, in this case, the Package Manager window does not display packages from those scoped registries in the All packages tab. However, this limitation does not apply to package resolution, so you can still manually add packages from scoped registries to the Project manifest.
To set up your scoped registries in your Project manifest, use the scopedRegistries attribute, which takes an array of scoped registry configuration objects. Each object contains a name, a url location, and a list of scopes for each package name pattern you want to map to that scoped registry. When the Package Manager decides which registry to fetch a package from, it compares the package name to the scopes values and finds the registry whose scopes most closely match.
For example, in the Project manifest below, there are two scoped registries, Main and Tools:
{
"scopedRegistries": [
{
"name": "Main",
"url": "https://my.company.com/registry",
"scopes": [
"com.my-company", "com.my-company.tools.foo"
]
},
{
"name": "Tools",
"url": "https://my.company.com/tools-registry",
"scopes": [
"com.my-company.tools"
]
}
],
"dependencies": {
"com.unity.cinemachine": "1.0.0",
"com.unity.2d.common": "1.0.0",
"com.unity.2d.animation": "1.0.0",
"com.my-company.bar": "1.0.0"
}
}
当用户请求 com.my-company.bar 包时,Package Manager 发现 com.my-company.* 命名空间与其名称最匹配,因此从 Main 注册表获取包。
当用户请求 com.my-company.tools.foo 包时,Main 注册表有一个范围中的一个命名空间是精确匹配项。
当用户请求 com.my-company.tools.animation 包时,Package Manager 发现 com.my-company.tools.* 命名空间与其名称最匹配,因此从 Tools 注册表获取包。虽然它也与 Main 范围匹配,但 com.my-company.* 命名空间的匹配度不够精确。
当用户请求 com.other-company.bar 包时,Package Manager 在任何范围注册表中都找不到匹配项,因此从默认注册表获取该包。
Configure scoped registries with the scopedRegistries attribute in the Project manifest. The scopedRegistries attribute contains an array of entries (objects) that represent all of the registries.
每个注册表对象都包含一个唯一名称 (name)、一个位置 (url) 以及一个命名空间数组(即 scopes)。Package Manager 使用范围将包名称与注册表进行匹配。
属性 | JSON 类型 | 描述 |
---|---|---|
name | String | 范围名称,在用户界面中显示。Package Manager 窗口在包详细信息视图中显示此名称。例如 "name": "Tools" 。 |
url | String | The URL to the npm-compatible registry. For example, "url": "https://my.company.com/tools-registry"
|
scopes | 字符串数组 | Array of scopes that you can map to a package name, either as an exact match on the package name, or as a namespace. For example, "scopes": [ "com.my-company", "com.my-company.tools.foo" ] NOTE: This type of configuration assumes that packages follow the Reverse domain name notation. This ensures that com.unity is equivalent to com.unity.*. |