AssetInfo.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace YooAsset.Editor
  5. {
  6. [Serializable]
  7. public class AssetInfo : IComparable<AssetInfo>
  8. {
  9. private string _fileExtension = null;
  10. /// <summary>
  11. /// 资源路径
  12. /// </summary>
  13. public string AssetPath;
  14. /// <summary>
  15. /// 资源GUID
  16. /// </summary>
  17. public string AssetGUID;
  18. /// <summary>
  19. /// 资源类型
  20. /// </summary>
  21. public System.Type AssetType;
  22. /// <summary>
  23. /// 文件格式
  24. /// </summary>
  25. public string FileExtension
  26. {
  27. get
  28. {
  29. if (string.IsNullOrEmpty(_fileExtension))
  30. _fileExtension = System.IO.Path.GetExtension(AssetPath);
  31. return _fileExtension;
  32. }
  33. }
  34. public AssetInfo(string assetPath)
  35. {
  36. AssetPath = assetPath;
  37. AssetGUID = UnityEditor.AssetDatabase.AssetPathToGUID(AssetPath);
  38. AssetType = UnityEditor.AssetDatabase.GetMainAssetTypeAtPath(AssetPath);
  39. // 注意:如果资源文件损坏或者实例化关联脚本丢失,获取的资源类型会无效!
  40. if (AssetType == null)
  41. {
  42. throw new Exception($"Found invalid asset : {AssetPath}");
  43. }
  44. }
  45. /// <summary>
  46. /// 是否为着色器资源
  47. /// </summary>
  48. public bool IsShaderAsset()
  49. {
  50. if (AssetType == typeof(UnityEngine.Shader) || AssetType == typeof(UnityEngine.ShaderVariantCollection))
  51. return true;
  52. else
  53. return false;
  54. }
  55. public int CompareTo(AssetInfo other)
  56. {
  57. return this.AssetPath.CompareTo(other.AssetPath);
  58. }
  59. }
  60. }