Skip to content

Instantly share code, notes, and snippets.

@xavierarpa
Last active May 27, 2024 06:35
Show Gist options
  • Save xavierarpa/8f529162292add65cf1db347ee690caa to your computer and use it in GitHub Desktop.
Save xavierarpa/8f529162292add65cf1db347ee690caa to your computer and use it in GitHub Desktop.
Folder to package.tgz
#if UNITY_EDITOR
using System.IO;
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
public static class PackageCreator
{
public static void CreateTgzPackage(string packagePath, string sourcePath)
{
PackRequest packRequest = Client.Pack(sourcePath, packagePath);
//
while (!packRequest.IsCompleted) System.Threading.Thread.Sleep(10);
// Check for errors
if (packRequest.Status == StatusCode.Failure)
{
UnityEngine.Debug.LogError($"Failed to create package. Error: {packRequest.Error.message}");
throw new IOException($"Failed to create package. Error: {packRequest.Error.message}");
}
}
[MenuItem("Assets/Create package.tgz", false, 0)] private static void CustomOption()
{
DefaultAsset selectedFolder = Selection.activeObject as DefaultAsset;
if (selectedFolder != null)
{
string folderPath = AssetDatabase.GetAssetPath(selectedFolder);
string targetFolder = EditorUtility.OpenFolderPanel("Select target folder", "", "");
if (!string.IsNullOrEmpty(targetFolder))
{
CreateTgzPackage(targetFolder, folderPath);
AssetDatabase.Refresh();
}
}
}
[MenuItem("Assets/Create .tgz", true)] private static bool ValidateCustomOption() =>Selection.activeObject is DefaultAsset;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment