/* ┌──────────────────────────────────────────────────────────────────┐ │ Author: Ivan Murzak (https://github.com/IvanMurzak) │ │ Repository: GitHub (https://github.com/IvanMurzak/Unity-MCP) │ │ Copyright (c) 2025 Ivan Murzak │ │ Licensed under the Apache License, Version 2.0. │ │ See the LICENSE file in the project root for more information. │ └──────────────────────────────────────────────────────────────────┘ */ #nullable enable using System.Collections.Generic; using Microsoft.Extensions.Logging; using UnityEditor; namespace com.IvanMurzak.Unity.MCP.Editor.Utils { /// /// Utility class for loading Unity Editor assets with fallback paths. /// public static class EditorAssetLoader { private const string PackagePathPrefix = "Packages/com.ivanmurzak.unity.mcp/"; private const string AssetsPathPrefix = "Packages/com.ivanmurzak.unity.mcp/"; private const string LogoIconRelativePath = "Editor/Gizmos/logo_window_icon.png"; /// /// Full package path to the logo icon. Usable in compile-time attributes (e.g., [Icon]). /// public const string PackageLogoIconPath = PackagePathPrefix + LogoIconRelativePath; public static readonly string[] PackageLogoIcon = GetEditorAssetPaths(LogoIconRelativePath); /// /// Generates an array of paths for an editor asset, with both package and development paths. /// /// The path relative to the package/assets root (e.g., "Editor/Gizmos/logo.png"). /// An array containing both the package path and the assets path. public static string[] GetEditorAssetPaths(string relativePath) { return new[] { PackagePathPrefix + relativePath//, //AssetsPathPrefix + relativePath }; } /// /// Attempts to load an asset from multiple paths, trying each in order until one succeeds. /// /// The type of asset to load. /// Collection of paths to try in order. /// Optional logger for diagnostic messages. /// The loaded asset, or null if not found in any path. public static T? LoadAssetAtPath(IEnumerable paths, ILogger? logger = null) where T : UnityEngine.Object { foreach (var path in paths) { var asset = AssetDatabase.LoadAssetAtPath(path); if (asset != null) { logger?.LogTrace("{method} Loaded file from: {path}", nameof(LoadAssetAtPath), path); return asset; } } logger?.LogWarning("{method} File not found. Checked: {paths}", nameof(LoadAssetAtPath), string.Join(", ", paths)); return null; } } }