今天在做一个项目,项目中依赖于很多其他的Package,其他的Package又是通过tgz包提供的,没有放到中心仓库,要是通过Package Manager界面导入需要一个一个点击加载,费时又费力,那我就写了一个脚本,点击import package,就会扫描Package tgz目录,把所有的Package加载进来,这个方法无法做到依赖包关系,需要你自己多加载几次。
代码如下:
- 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;
- }
- }
- }
- }
-