您可以使用 Package Manager 脚本 API 以编程方式与 Package Manager 进行交互。例如,可能需要根据目标机器的平台来安装特定的包或版本。
系统的核心是 PackageManager.Client 类,可使用该类来查找包、浏览包列表以及通过脚本安装和卸载包。
另一个重要的类是 PackageManager.PackageInfo,其中包含包的状态,包括从包清单和注册表获取的元数据。例如,可获取适用于包的版本列表,或者获取在查找或安装包时可能发生的所有错误的列表。
This example demonstrates how to use the Client class to install or add a package to the Project.
在调用 Client.Add 方法时,您可以指定包名称或具有特定版本的名称。例如,如果使用 Client.Add("com.unity.textmeshpro@1.3.0")
,则会安装 TextMesh Pro 包版本 1.3.0,但如果仅使用 Client.Add("com.unity.textmeshpro")
,则会安装(或更新到)该包的最新版本。
Client.Add 方法将返回 AddRequest 实例,可以用来获取状态、任何错误或 Request 响应(包含新添加的包的 PackageInfo 信息)。
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;
}
}
}
}
This example demonstrates how to use the Client class to iterate over the packages in the Project.
Client.List 方法将返回 ListRequest 实例,可以用来获取 List 操作的状态、任何错误或者 Request 响应(包含您可以遍历的 PackageCollection)。
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;
}
}
}
}
This example demonstrates how to use the Client class to embed one of the packages already installed in your Project. The main method is the Client.Embed method, which makes a copy of the package and stores it under the Packages
folder of your Project.
Client.Embed 方法将返回 EmbedRequest 实例,可以用来获取 Embed 操作的状态、任何错误或 Request 响应(包含新嵌入的包的 PackageInfo 信息)。
This example also uses the Client.List method to access the collection of packages currently installed in your Project and picks out the first one that is neither embedded nor built-in.
Client.List 方法将返回 ListRequest 实例,可以用来获取 List 操作的状态、任何错误或者 Request 响应(包含您可以遍历的 PackageCollection)。
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;
}
}
}
}
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.