Version: 2023.1
包的脚本 API
Scoped registries

访问包资源

本节说明如何访问或引用包中定义的资源:

注意:Package Manager 不支持包中的流媒体资源。而应使用 Addressables 包。

引用包路径

要引用在包内定义的资源,请使用以下路径方案:

"Packages/<package-name>/..."

包中资源的路径以 Packages/ 和包名称(不是显示名称)开头。

相比之下,应使用以下方案访问项目资源:

"Assets/..."

例如,在 com.unity.images-library 包的包子文件夹 /Example/Images 中,文件 image.png 的路径是:

"Packages/com.unity.images-library/Example/Images/image.png"

加载包中的纹理

To load a Texture stored inside a package, use the LoadAssetAtPath method, which requires the using UnityEditor directive. Specify the path following the Packages/<package-name>/ path scheme as demonstrated in this example:

using UnityEditor;
// ...
Texture2D texture = (Texture2D)AssetDatabase.LoadAssetAtPath("Packages/com.unity.images-library/Example/Images/image.png", typeof(Texture2D));

解析绝对路径

To get the absolute path of a packaged asset, use the Path.GetFullPath() method, which requires the using System.IO directive. For example:

using System.IO;
// ...
string absolute =   Path.GetFullPath("Packages/com.unity.images-library/Example/Images/image.png");


包的脚本 API
Scoped registries