Version: 2019.2
Package Manifest 窗口
Accessing package Assets

包的脚本 API

您可以使用 Package Manager 脚本 API 以编程方式与 Package Manager 进行交互。例如,可能需要根据目标机器的平台来安装特定的包或版本。

系统的核心是 PackageManager.Client 类,可使用该类来查找包、浏览包列表以及通过脚本安装和卸载包。

另一个重要的类是 PackageManager.PackageInfo,其中包含包的状态,包括从包清单和注册表获取的元数据。例如,可获取适用于包的版本列表,或者获取在查找或安装包时可能发生的所有错误的列表

Adding a package to the Project

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;
           }
       }
   }
}

Browsing the list of packages in a Project

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;
           }
       }
   }
}

Embedding a package in the Project

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;
            }
        }
    }
}
Package Manifest 窗口
Accessing package Assets