0%

Unity Package Menu Item

Package扩展命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.PackageManager.Requests;
using UnityEditor.PackageManager;
using UnityEngine;
using System.Text.RegularExpressions;

namespace Unity.Editor.Extention
{
static class PackageMenuItem
{
static String targetPackage;
static EmbedRequest Request;
static ListRequest LRequest;

[MenuItem("Assets/Package/Embed Installed Package")]
static void EmbedInstalledPackage()
{
if(Selection.assetGUIDs.Length > 0)
{
string path = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);
Match match = Regex.Match(path, "^Packages/([^/]+)$");
if(match.Success)
{
string targetPackage = match.Groups[1].Value;
//Debug.LogError(targetPackage);
Embed(targetPackage);
}
}
}
[MenuItem("Assets/Package/Embed Installed Package", validate = true)]
static bool Check_EmbedInstalledPackage()
{
if (Selection.assetGUIDs.Length > 0)
{
string path = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);
Match match = Regex.Match(path, "^Packages/([^/]+)$");
return match.Success;
}
return false;
}
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;
}
}


#region Test
[MenuItem("Assets/Package/Test/Get Package Name", priority = 100)]
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;
Debug.LogError(targetPackage);
//break;
}
}

}
else
Debug.Log(LRequest.Error.message);

EditorApplication.update -= LProgress;

//Embed(targetPackage);

}
}
#endregion
}
}