Skip to content

Instantly share code, notes, and snippets.

@ArthurD
Created January 5, 2021 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArthurD/09d8a174cc036264c83205dde93392e9 to your computer and use it in GitHub Desktop.
Save ArthurD/09d8a174cc036264c83205dde93392e9 to your computer and use it in GitHub Desktop.
See the 'PackageToVendor' constant -- set it to decide which pkg to "vendor"/embed locally. References indeed appear to be kept completely intact.
#if UNITY_EDITOR
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 {
private const string PackageToVendor = "com.unity.addressables";
private static string targetPackage = null;
private static EmbedRequest Request;
private static ListRequest LRequest;
[MenuItem("Window/Embed Package '"+ PackageToVendor+"'")]
static void GetPackageName() {
// First get the name of an installed package
LRequest = Client.List();
EditorApplication.update += LProgress;
targetPackage = null;
}
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) {
if (package.name == PackageToVendor) {
Debug.LogFormat("FOUND IT: {0}", package.name);
targetPackage = package.name;
break;
}
}
}
} else {
Debug.LogError(LRequest.Error.message);
}
// Stop Updating
EditorApplication.update -= LProgress;
if (targetPackage == null || targetPackage != null && targetPackage != PackageToVendor) {
Debug.LogErrorFormat("Unable to find targetPackage: {0}", PackageToVendor);
} else {
Debug.LogFormat("Going to Vendor Package NOW: {0} ", targetPackage);
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;
}
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment