/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of xAPI4Unity. */ using System; using System.IO; using UnityEditor; using UnityEngine; using xAPI4Unity.Editor.Settings; #if UNITY_EDITOR namespace xAPI4Unity.Editor { /// /// Handles exporting Unity assets and folders as .unitypackage files for xAPI4Unity. /// internal class UnityPackageExporter { /// /// Gets the relative path within the Unity "Assets/..." directory for a given absolute path. /// /// The absolute path to convert. /// The path relative to the Unity "Assets" folder. private static string GetRelativePath(string path) { var assetsPathIndex = path.IndexOf("/Assets", StringComparison.Ordinal); return path.Substring(assetsPathIndex); } /// /// Retrieves the parent directory of the given path. /// /// The original path. /// The parent directory path. private static string GetParentPath(string path) { var s = path.Replace("\\", "/").Split('/'); return string.Join("/", s, 0, s.Length - 1); } /// /// Exports a single path as a .unitypackage file. /// /// The path to export. /// The name of the output .unitypackage file. public static void ExportAsPackage(string path, string name) => ExportAsPackage(new []{path}, name); /// /// Exports multiple paths as a single .unitypackage file. /// /// The array of paths to include in the package. /// The name of the output .unitypackage file. public static void ExportAsPackage(string[] paths, string name) { AssetDatabase.ExportPackage(paths, name, ExportPackageOptions.Recurse); // Determine the target path for the created package var targetPath = Path.Combine(GetParentPath(Application.dataPath), name); Debug.Log($"Exported into '{targetPath}'."); } /// /// Menu option to export only the "xapi" folder as a .unitypackage. /// [MenuItem("xAPI4Unity / Export .unitypackage / Only 'xapi' folder", priority = 200)] private static void ExportXApiFolder() { var path = GetRelativePath(MainSettings.Instance.localSource.path); ExportAsPackage(path, "xapi.unitypackage"); } /// /// Menu option to export only the "xAPI.Registry" folder as a .unitypackage. /// [MenuItem("xAPI4Unity / Export .unitypackage / Only 'xAPI.Registry' folder", priority = 200)] private static void ExportXApiRegistryFolder() { ExportAsPackage($"Assets/{MainSettings.Instance.@namespace}", "xAPIRegistry.unitypackage"); } /// /// Menu option to export both the "xapi" and "xAPI.Registry" folders as a single .unitypackage. /// [MenuItem("xAPI4Unity / Export .unitypackage / 'xapi' and 'xAPI.Registry' folder together", priority = 200)] private static void ExportBoth() { ExportAsPackage(new [] { MainSettings.Instance.localSource.path, $"Assets/{MainSettings.Instance.@namespace}" }, "xAPIRegistry.unitypackage"); } } } #endif