The way Unity code imports packages through Package Manager

I am working on a project today. The project depends on many other packages. Other packages are provided through the tgz package and are not placed in the central warehouse. If you need to click and load one by one to import through the Package Manager interface, it is time-consuming and laborious. Then I Just write a script, click import package, it will scan the Package tgz directory, and load all the packages in, this method cannot achieve the dependency package relationship, you need to load it several times by yourself.

code show as below:

using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
using UnityEngine;

namespace XXXX.MetaSpace
{
    internal static class MenuItems
    {
        static AddRequest request;
        static ListRequest listRequest;
        static List<string> packageNames;
        static bool isImporting = false;

        [MenuItem("MetaSpace/Import Package")]
        static void ImportPackages()
        {
            if (isImporting)
            {
                Debug.LogWarning("All packages are being imported.");
                return;
            }

            isImporting = true;
            PackagesOk();
        }

        static List<string> GetLocalPackages()
        {
            List<string> packages = new List<string>();
            string[] allFilePaths = Directory.GetFiles("Packages/com.xxxx.metaspace/Runtime/ThirdParty");
            foreach (var one in allFilePaths)
            {
                if (one.EndsWith("tgz"))
                {
                    string pn = one.Replace("Packages/", "");
                    string packageName = "file:" + pn;
                    Debug.Log("packageName: " + packageName);
                    packages.Add(packageName);
                }
            }

            return packages;
        }

        static void PackagesOk()
        {
            Debug.Log("Start List Packages...");
            listRequest = Client.List(false);
            EditorApplication.update += ListPackages;
        }

        static void StartInstallPackages()
        {
            EditorApplication.update += Progress;
            EditorApplication.update += AddPackage;
        }

        static void AddPackage()
        {
            if (request == null && (packageNames == null || packageNames.Count <= 0))
            {
                EditorApplication.update -= Progress;
                EditorApplication.update -= AddPackage;
                //isImporting = false;
                PackagesOk();

                return;
            }

            if (request == null && packageNames.Count > 0)
            {
                request = Client.Add(packageNames[0]);
                packageNames.RemoveAt(0);
            }
        }

        static void Progress()
        {
            if (request != null && request.IsCompleted)
            {
                if (request.Status == StatusCode.Success)
                    Debug.Log("Installed: " + request.Result.packageId);
                else if (request.Status >= StatusCode.Failure)
                    Debug.Log(request.Error.message);

                request = null;
            }
        }

        static void ListPackages()
        {
            if (listRequest.IsCompleted)
            {
                if (listRequest.Status == StatusCode.Success)
                {
                    List<string> localPackages = GetLocalPackages();
                    packageNames = new List<string>();

                    foreach (var localPackage in localPackages)
                    {
                        string[] splitPaths = localPackage.Split('/');
                        if (splitPaths.Length <= 0)
                            continue;

                        bool isFind = false;
                        foreach (var package in listRequest.Result)
                        {
                            if (package.packageId.Contains(splitPaths[splitPaths.Length - 1]))
                            {
                                isFind = true;
                                break;
                            }
                        }

                        if (!isFind)
                        {
                            Debug.Log($"package: {localPackage} need to install!");
                            packageNames.Add(localPackage);
                        }
                    }

                    if (packageNames.Count > 0)
                        StartInstallPackages();
                    else
                    {
                        Debug.Log("all packages are installed!");
                        isImporting = false;
                    }
                }
                else if (listRequest.Status >= StatusCode.Failure)
                    Debug.Log(listRequest.Error.message);

                EditorApplication.update -= ListPackages;
                listRequest = null;
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/grace_yi/article/details/125898723