DirectoryAnchor.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace net.shutosg.UniEaseCopy
  8. {
  9. public class DirectoryAnchor : ScriptableObject
  10. {
  11. private static readonly Dictionary<string, string> Cache = new Dictionary<string, string>();
  12. [SerializeField] private string id;
  13. public static string Find(string id)
  14. {
  15. if (Cache.TryGetValue(id, out var path)) return path;
  16. var targetAnchorInfos = AssetDatabase.FindAssets($"t:{nameof(DirectoryAnchor)}")
  17. .Select(AssetDatabase.GUIDToAssetPath)
  18. .Select(p => (path: p, anchor: AssetDatabase.LoadAssetAtPath<DirectoryAnchor>(p)))
  19. .Where(x => x.anchor.id == id).ToList();
  20. if (targetAnchorInfos.Count > 1) throw new MultipleDirectoryAnchorException($"idが '{id}' の {nameof(DirectoryAnchor)} が複数存在します");
  21. if (targetAnchorInfos.Count == 0) throw new DirectoryAnchorNotFoundException($"idが '{id}' の {nameof(DirectoryAnchor)} が存在しません");
  22. var targetAnchorPath = targetAnchorInfos[0].path;
  23. var pathWithoutFileName = targetAnchorPath.Replace(Path.GetFileName(targetAnchorPath), "");
  24. Cache.Add(id, pathWithoutFileName);
  25. return pathWithoutFileName;
  26. }
  27. }
  28. public class MultipleDirectoryAnchorException : Exception
  29. {
  30. public MultipleDirectoryAnchorException(string message) : base(message) { }
  31. }
  32. public class DirectoryAnchorNotFoundException : Exception
  33. {
  34. public DirectoryAnchorNotFoundException(string message) : base(message) { }
  35. }
  36. }