文档

Unity 引擎

Unity 引擎

包的脚本 API

了解如何使用 C# 来管理Projects中的资源包。
阅读时间7 分钟最后更新于 8 天前

您可以使用 包管理器 脚本 API 使用 C# 脚本与 包管理器 交互。例如,您可能希望根据 Target 机器的平台安装指定的包或版本。
使用脚本 API 进行包管理依赖于 PackageManager.Client 类。使用类可查查找、浏览包列表以及通过脚本安装和卸载包。
另一个重要类是 PackageManager.PackageInfo,其中包含包的状态,包括从包清单和注册表获取的元数据。例如,您可以获取可用于该包的版本列表,或查找或安装该包时可能发生的任何错误列表。

向 Project 添加包

此示例演示如何使用 Client 类来安装包或向项目添加包。
您可以使用 Client.Add 来添加包。当您调用
Client.Add
方法时,可以仅指定 Package Name 或具有指定版本的 Name。例如,使用
Client.Add("com.unity.textmeshpro")
会安装(或更新)最新版本的 TextMesh Pro 包。使用
Client.Add("com.unity.textmeshpro@1.3.0")
会安装 TextMesh Pro 包的版本 1.3.0。
Client.Add
方法返回一个 AddRequest 实例,您可以使用该实例获取状态、任何错误或包含新添加的包的 PackageInfo 信息的请求响应。
using System;using UnityEditor;using UnityEditor.PackageManager.Requests;using UnityEditor.PackageManager;using UnityEngine;命名空间 Unity.Editor.Example { Static 类 AddPackageExample { static AddRequest 请求; [MenuItem("Window/添加 包 Example")] Static void 添加() { // Add a package to the project Request = Client.Add("com.unity.textmeshpro"); EditorApplication.Update += Progress; } Static void Progress() { if (Request.IsCompleted) { if (Request.Status == StatusCode.Success) Debug.Log("Installed: " + Request.Result.packageId); else if (Request.Status >= StatusCode.Failure) Debug.Log(Request.Error.message); EditorApplication.update -= Progress; } } }}

浏览Project中的包列列表

此示例演示如何使用 Client 类来迭代 Project 中的包。
Client.list 方法返回 ListRequest 实例。使用
ListRequest
实例可获取列表操作的状态、任何错误或包含可迭代的 PackageCollection 的请求响应。
using System;using UnityEditor;using UnityEditor.PackageManager.Requests;using UnityEditor.PackageManager;using UnityEngine;命名空间 Unity.Editor.Example { Static 类 ListPackageExample { static ListRequest 请求; [MenuItem("Window/列表 包 Example")] Static void 列表 () { 请求 = 列表 // 列表出为Project安装的包 EditorApplication.Update += Progress; } Static void Progress() { if (Request.IsCompleted) { if (Request.Status == StatusCode.Success) Foreach(请求中的 var 包。result) Debug.Log("Package name: " + package.name); else if (Request.Status >= StatusCode.Failure) Debug.Log(Request.Error.message); EditorApplication.update -= Progress; } } }}

在 Project 中嵌入包

此示例演示如何使用 Client 类将已安装的包之一嵌入到 Project 中。主要方法是 Client.Embed 方法,该方法会复制包并将其存储在项目的
Packages
Folder 下。
Client.Embed 方法返回 EmbedRequest 实例。使用
EmbedRequest
实例可获取嵌入式操作的状态、任何错误或包含新嵌入式包的 PackageInfo 信息的请求响应。
此示例还使用 Client.list 方法来访问 Project 中当前安装的包的集合。示例中的方法会选择第一个既不嵌入式也不内置的方法。
Client.list 方法返回 ListRequest 实例。使用
ListRequest
实例可获取列表操作的状态、任何错误或包含可迭代的 PackageCollection 的请求响应。
using System;using System.Collections;using System.Collections.Generic;using UnityEditor;using UnityEditor.PackageManager.Requests;using UnityEditor.PackageManager;using UnityEngine;命名空间 Unity.Editor.Example{ Static 类 EmbedPackageExample { static 字符串 targetPackage; static EmbedRequest 请求; static ListRequest LRequest; [MenuItem("Window/Embed 包 Example")] Static void GetPackageName() { // First get the name of an installed package LRequest = Client.List(); EditorApplication.Update += LProgress; } Static void LProgress() { if (LRequest.IsCompleted) { if (LRequest.Status == StatusCode.Success) { Foreach(LRequestresult 中的 var 包) { // Only retrieve packages that are currently installed in the // project (and are neither Built-In nor already Embedded) if (package.isDirectDependency && package.source != PackageSource.BuiltIn && package.source != PackageSource.Embedded) { targetPackage = package.name; break; } } } else Debug.Log(LRequest.Error.message); EditorApplication.update -= LProgress; Embed(targetPackage); } } Static void Embed(字符串 inTarget) { // Embed a package in the project Debug.Log("Embed('" + inTarget + "') called"); Request = Client.Embed(inTarget); EditorApplication.Update += Progress; } Static void Progress() { if (Request.IsCompleted) { if (Request.Status == StatusCode.Success) Debug.Log("Embedded: " + Request.Result.packageId); else if (Request.Status >= StatusCode.Failure) Debug.Log(Request.Error.message); EditorApplication.update -= Progress; } } }}

包管理器事件

使用事件类向包管理器注册事件处理程序。事件类包含两个可以订阅的事件,由包管理器在以下位置引发:
  • 在包管理器更改依赖项列表之前(注册包)
  • 包管理器导入并编译已更改的包依赖项列表(registeredPackages)后
以下示例演示了如何使用这两个事件。

使用 registeredPackages 事件的示例

using UnityEditor.PackageManager;using UnityEngine;命名空间 Unity.Editor.Example{ public class EventSubscribingExample_RegisteringPackages { public EventSubscribingExample_RegisteringPackages() { // Subscribe to the event using the addition assignment operator (+=). // This executes the code in the handler whenever the event is fired. Events.registeringPackages += RegisteringPackagesEventHandler; } // The method is expected to receive a PackageRegistrationEventArgs event argument. void RegisteringPackagesEventHandler(PackageRegistrationEventArgs packageRegistrationEventArgs) { Debug.Log("The list of registered packages is about to change!"); Foreach(var addedPackage in packageRegistrationEventArgs.added) { Debug.Log($"Adding {addedPackage.displayName}"); } Foreach(var removedPackage in packageRegistrationEventArgs.已移除) { Debug.Log($"Removing {removedPackage.displayName}"); } // The changedFrom and changedTo collections contain the packages that are about to be updated. // Both collections are guaranteed to be the same size with indices matching the same package name. for (int i = 0; i <= packageRegistrationEventArgs.changedFrom.Count; i++) { var oldPackage = packageRegistrationEventArgs.changedFrom[i]; var newPackage = packageRegistrationEventArgs.changedTo[i]; Debug.Log($"Changing ${oldPackage.displayName} version from ${oldPackage.version} to ${newPackage.version}"); } } }}

使用 registeredPackages 事件的示例

using UnityEditor;using UnityEditor.PackageManager;using UnityEngine;命名空间 Unity.Editor.Example{ public class EventSubscribingExample_RegisteredPackages { // You must use '[InitializeOnLoadMethod]' or '[InitializeOnLoad]' to subscribe to this event. [InitializeOnLoadMethod] Static void SubscribeToEvent() { // This causes the method to be invoked after the Editor registers the new list of packages. Events.registeredPackages += RegisteredPackagesEventHandler; } Static void RegisteredPackagesEventHandler(PackageRegistrationEventArgs packageRegistrationEventArgs) { // Code executed here can safely assume that the Editor has finished compiling the new list of packages Debug.Log("The list of registered packages has changed!"); } }}