包含此页的版本:
不含此页的版本:
可以使用包管理器脚本 API 通过 C# 与包管理器进行交互脚本一段代码,允许您创建自己的组件、触发游戏事件、随时间修改组件属性以及以您喜欢的任何方式响应用户输入。更多信息
请参阅术语表.例如,您可能希望安装特定的包或版本,具体取决于目标计算机的平台。
使用脚本 API 进行包管理依赖于 PackageManager.Client 类。使用此类查找包、浏览包列表以及通过脚本安装和卸载包。
另一个重要的类是 PackageManager.PackageInfo,它包含包的状态,包括从包清单每个包都有一个清单,该清单向包管理器提供有关包的信息。清单包含包的名称、版本、用户说明、对其他包的依赖关系(如果有)以及其他详细信息等信息。更多信息
请参阅术语表和注册表。例如,可以获取可用于包的版本列表,或者查找或安装包时可能发生的任何错误的列表。
此示例演示如何使用 Client 类安装包或向项目添加包。
可以使用 Client.Add 添加包。当您调用Client.Add方法中,您可以仅指定包名称,也可以指定具有特定版本的名称。例如,使用Client.Add("com.unity.textmeshpro")安装(或更新)最新版本的 TextMesh Pro 包。用Client.Add("com.unity.textmeshpro@1.3.0")安装 TextMesh Pro 包的 1.3.0 版。
这Client.Add方法返回一个 AddRequest 实例,您可以使用该实例来获取状态、任何错误或包含新添加包的 PackageInfo 信息的 Request 响应。
using System;
using UnityEditor;
using UnityEditor.PackageManager.Requests;
using UnityEditor.PackageManager;
using UnityEngine;
namespace Unity.Editor.Example {
static class AddPackageExample
{
static AddRequest Request;
[MenuItem("Window/Add Package Example")]
static void Add()
{
// 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;
}
}
}
}
此示例演示如何使用 Client 类循环访问项目中的包。
Client.List 方法返回 ListRequest 实例。使用ListRequest实例来获取 List作的状态、任何错误或包含可以迭代的 PackageCollection 的 Request 响应。
using System;
using UnityEditor;
using UnityEditor.PackageManager.Requests;
using UnityEditor.PackageManager;
using UnityEngine;
namespace Unity.Editor.Example {
static class ListPackageExample
{
static ListRequest Request;
[MenuItem("Window/List Package Example")]
static void List()
{
Request = Client.List(); // List packages installed for the project
EditorApplication.update += Progress;
}
static void Progress()
{
if (Request.IsCompleted)
{
if (Request.Status == StatusCode.Success)
foreach (var package in Request.Result)
Debug.Log("Package name: " + package.name);
else if (Request.Status >= StatusCode.Failure)
Debug.Log(Request.Error.message);
EditorApplication.update -= Progress;
}
}
}
}
此示例演示如何使用 Client 类嵌入项目中已安装的包之一。main 方法是 Client.Embed 方法,它创建包的副本并将其存储在Packages文件夹。
Client.Embed 方法返回 EmbedRequest 实例。使用EmbedRequest实例来获取嵌入作的状态、任何错误或包含新 嵌入式包嵌入式包是存储在Packages目录。这与从包服务器下载的大多数包不同,并且是不可变的。更多信息
请参阅术语表.
此示例还使用 Client.List 方法访问项目中当前安装的包集合。示例中的方法选择第一个既不嵌入也不内置的包。
Client.List 方法返回 ListRequest 实例。使用ListRequest实例来获取 List作的状态、任何错误或包含可以迭代的 PackageCollection 的 Request 响应。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.PackageManager.Requests;
using UnityEditor.PackageManager;
using UnityEngine;
namespace Unity.Editor.Example
{
static class EmbedPackageExample
{
static String targetPackage;
static EmbedRequest Request;
static ListRequest LRequest;
[MenuItem("Window/Embed Package 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 (var package in LRequest.Result)
{
// 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(string 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;
}
}
}
}
使用 Events 类向包管理器注册事件处理程序。Events 类包含两个可以订阅的事件,包管理器在以下点引发这些事件:
以下示例演示了如何使用这两个事件。
using UnityEditor.PackageManager;
using UnityEngine;
namespace 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.removed)
{
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}");
}
}
}
}
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEngine;
namespace 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!");
}
}
}