using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using AnyThink.Scripts.IntegrationManager.Editor; using UnityEngine; // #if UNITY_EDITOR //是Unity编辑器才引入 using UnityEditor; // #endif public class ATSdkUtil { // #if UNITY_EDITOR /// /// Gets the path of the asset in the project for a given Anythink plugin export path. /// /// The actual exported path of the asset. /// The exported path of the MAX plugin asset or the default export path if the asset is not found. public static string GetAssetPathForExportPath(string exportPath) { var normalizedExportPath = NormalizeAssetPath(exportPath); var defaultPath = NormalizeAssetPath(Path.Combine("Assets", normalizedExportPath)); var assetLabelToFind = "l:al_max_export_path-" + normalizedExportPath; var assetGuids = AssetDatabase.FindAssets(assetLabelToFind); if (assetGuids.Length > 0) { var assetPath = AssetDatabase.GUIDToAssetPath(assetGuids[0]); if (!string.IsNullOrEmpty(assetPath)) { return assetPath; } } foreach (var candidatePath in GetCandidateAssetPaths(normalizedExportPath)) { if (AssetExists(candidatePath)) { return candidatePath; } } var assetName = Path.GetFileNameWithoutExtension(normalizedExportPath); if (!string.IsNullOrEmpty(assetName)) { var expectedSuffixes = new[] { normalizedExportPath, GetPluginRelativeExportPath(normalizedExportPath) }; foreach (var guid in AssetDatabase.FindAssets(assetName)) { var assetPath = NormalizeAssetPath(AssetDatabase.GUIDToAssetPath(guid)); if (expectedSuffixes.Any(suffix => !string.IsNullOrEmpty(suffix) && assetPath.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))) { return assetPath; } } } return defaultPath; } public static bool Exists(string filePath) { return Directory.Exists(filePath) || File.Exists(filePath); } private static IEnumerable GetCandidateAssetPaths(string normalizedExportPath) { var pluginRelativeExportPath = GetPluginRelativeExportPath(normalizedExportPath); var dynamicRootPath = NormalizeAssetPath(ATConfig.RootPath); if (!string.IsNullOrEmpty(dynamicRootPath)) { yield return NormalizeAssetPath(Path.Combine(dynamicRootPath, pluginRelativeExportPath)); } yield return NormalizeAssetPath(Path.Combine("Assets", normalizedExportPath)); } private static string GetPluginRelativeExportPath(string normalizedExportPath) { const string pluginRootName = "AnyThinkPlugin/"; return normalizedExportPath.StartsWith(pluginRootName, StringComparison.OrdinalIgnoreCase) ? normalizedExportPath.Substring(pluginRootName.Length) : normalizedExportPath; } private static string NormalizeAssetPath(string assetPath) { return string.IsNullOrEmpty(assetPath) ? string.Empty : assetPath.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).TrimStart(Path.AltDirectorySeparatorChar); } private static bool AssetExists(string assetPath) { if (string.IsNullOrEmpty(assetPath)) { return false; } return AssetDatabase.LoadAssetAtPath(assetPath) != null || File.Exists(assetPath); } // #endif }