Skip to content

Instantly share code, notes, and snippets.

@liortal53
Created October 25, 2019 07:19
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save liortal53/111ee2a659b0d59c80faab0f5d457531 to your computer and use it in GitHub Desktop.
Save liortal53/111ee2a659b0d59c80faab0f5d457531 to your computer and use it in GitHub Desktop.
Embed package into your Unity project to modify the code more easily :)
using System.IO;
using UnityEditor.PackageManager;
using UnityEngine;
namespace UnityEditor.Extensions
{
#if UNITY_2017_3_OR_NEWER
/// <summary>
/// Editor extension for embedding packages as a local copy in the project.
/// This can be useful in case you want to modify the package's source code.
/// </summary>
public static class EmbedPackage
{
[MenuItem("Assets/Embed Package", false, 1000000)]
private static void EmbedPackageMenuItem()
{
var selection = Selection.activeObject;
var packageName = Path.GetFileName(AssetDatabase.GetAssetPath(selection));
Debug.Log($"Embedding package '{packageName}' into the project.");
Client.Embed(packageName);
AssetDatabase.Refresh();
}
[MenuItem("Assets/Embed Package", true)]
private static bool EmbedPackageValidation()
{
var selection = Selection.activeObject;
if (selection == null)
{
return false;
}
var path = AssetDatabase.GetAssetPath(selection);
var folder = Path.GetDirectoryName(path);
// We only deal with direct folders under Packages/
return folder == "Packages";
}
}
#endif
}
@awwbees
Copy link

awwbees commented May 26, 2021

I was having an issue where Selection.activeObject was not reflecting the folder I clicked. Perhaps Unity changed their interface to make folders no longer behave as a selected object. Here is my fix:

using UnityEditor.PackageManager;
using UnityEngine;

namespace UnityEditor.Extensions
{
   #if UNITY_2017_3_OR_NEWER

   /// <summary>
   /// Editor extension for embedding packages as a local copy in the project.
   /// This can be useful in case you want to modify the package's source code.
   /// </summary>
   public static class EmbedPackage
   {
      [MenuItem("Assets/Embed Package", false, 1000000)]
      private static void EmbedPackageMenuItem()
      {
         var packageName = Path.GetFileName(GetSelectionPath());

         Debug.Log($"Embedding package '{packageName}' into the project.");

         Client.Embed(packageName);

         AssetDatabase.Refresh();
      }

      [MenuItem("Assets/Embed Package", true)]
      private static bool EmbedPackageValidation()
      {
         var path = GetSelectionPath();
         var folder = Path.GetDirectoryName(path);

         // We only deal with direct folders under Packages/
         return folder == "Packages";
      }

      private static string GetSelectionPath()
      {
         if (Selection.assetGUIDs.Length == 0)
            return "";

         string clickedAssetGuid = Selection.assetGUIDs[0];
         string clickedPath = AssetDatabase.GUIDToAssetPath(clickedAssetGuid);
         return clickedPath;
      }
   }

   #endif
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment