除了 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
)。
如果您的某个工具可以使用 Package Manager API 来列出可用的包,则在注册表不支持旧协议的情况下,可能会遇到意外结果。例如,在此情况下,Package Manager 窗口不会在 All packages 选项卡中显示这些范围注册表中的包。但是,这一限制不适用于包解析,因此您仍然可以手动将包从范围注册表添加到项目清单中。
要在项目清单中设置范围注册表,请使用 scopedRegistries 属性,该属性接受一个范围注册表配置对象数组。每个对象包含一个 name、url 位置以及要映射到该范围注册表的每个包名称模式的 scopes 列表。Package Manager 决定从哪个注册表获取包时,会将包 name 的值与 scopes 的值进行比较,并找到 scopes 最匹配的注册表。
例如,在以下项目清单中,有两个范围注册表(Main 和 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 在任何范围注册表中都找不到匹配项,因此从默认注册表获取该包。
在项目清单中使用 scopedRegistries 属性配置范围注册表。scopedRegistries 属性包含一个代表所有注册表的条目(对象)数组。
每个注册表对象都包含一个唯一名称 (name)、一个位置 (url) 以及一个命名空间数组(即 scopes)。Package Manager 使用范围将包名称与注册表进行匹配。
属性 | JSON 类型 | 描述 |
---|---|---|
name | String | 范围名称,在用户界面中显示。Package Manager 窗口在包详细信息视图中显示此名称。例如 "name": "Tools" 。 |
url | String |
与 npm 兼容的注册表的 URL。例如 "url": "https://my.company.com/tools-registry"
|
scopes | 字符串数组 | 可映射到包名称的范围数组(可与包名称完全匹配,也可以是命名空间)。 例如 "scopes": [ "com.my-company", "com.my-company.tools.foo" ] 注意:该类型的配置假设包遵循反向域名表示法。这可以确保 com.unity 等效于 com.unity.*。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.