当前位置:网站首页>Unity code imports packages through package manager
Unity code imports packages through package manager
2022-07-24 19:13:00 【XR Fengyun】
I'm working on a project today , The project depends on many others Package, Other Package Again through tgz Provided by the package , Not put in the central warehouse , If it passes Package Manager The interface import needs to be loaded one by one , Take time and trouble , Then I wrote a script , Click on import package, It will scan Package tgz Catalog , Put all the Package Loading in , This method cannot rely on package relationships , You need to load it yourself several times .
The code is as follows :
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;
}
}
}
}
边栏推荐
- MySQL1
- Oneinstack installation and configuration PHP 8.1 and MySQL 8.0-oneinstack site building novice tutorial
- Common problems of multithreading and concurrent programming (to be continued)
- Unity框架之ConfigManager【Json配置文件读写】
- Ebpf verifier
- [Huawei lyevk-3861a intelligent IOT development board evaluation] unpacking experience and Hisilicon hi3861v100 chip learning experience
- Data model subclassing reference
- 投资的新阶段
- BUUCTF-pwn[1]
- Taokeeper environment setup
猜你喜欢

Attack and defense world novice zone PWN

Emergency lighting design of large stadiums and gymnasiums

High speed ASIC packaging trends: integration, SKU and 25g+

Type-C PD protocol chip while charging and listening

MySQL1

The difference between static method and instance method

多线程与并发编程常见问题(未完待续)

Tencent Browser service TBS usage

Why is gradient the fastest changing direction of function

Pam4 popular science
随机推荐
matplotlib
Excel practice notes 1
暑期牛客多校1: I Chiitoitsu(期望dp,求逆元)
Mysql database, subquery, union, limit
Arrays
[wechat applet development] custom tabbar case (custom message 99 + little hearts)
Equals() method of object class
Principle and application of database
Add column by column selection for JTable
matplotlib
文献阅读:GoPose 3D Human Pose Estimation Using WiFi
第4章 复合类型
MySQL sort. Sort by field value
Nacos introduction and console service installation
Free and open source website navigation source code collection, sorting and summary - self built personal navigation Homepage
2022杭电多校第一场Dragon slayer(dfs+状态压缩)
Tcl/tk file operation
Principle and application of database
Convolutional Neural Networks in TensorFlow quizs on Coursera
OpenGL learning (IV) glut 3D image rendering