ResourceEditor.ResourceItem.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using UnityEditor;
  9. using UnityEngine;
  10. namespace UnityGameFramework.Editor.ResourceTools
  11. {
  12. internal sealed partial class ResourceEditor : EditorWindow
  13. {
  14. private sealed class ResourceItem
  15. {
  16. private static Texture s_CachedUnknownIcon = null;
  17. private static Texture s_CachedAssetIcon = null;
  18. private static Texture s_CachedSceneIcon = null;
  19. public ResourceItem(string name, Resource resource, ResourceFolder folder)
  20. {
  21. if (resource == null)
  22. {
  23. throw new GameFrameworkException("Resource is invalid.");
  24. }
  25. if (folder == null)
  26. {
  27. throw new GameFrameworkException("Resource folder is invalid.");
  28. }
  29. Name = name;
  30. Resource = resource;
  31. Folder = folder;
  32. }
  33. public string Name
  34. {
  35. get;
  36. private set;
  37. }
  38. public Resource Resource
  39. {
  40. get;
  41. private set;
  42. }
  43. public ResourceFolder Folder
  44. {
  45. get;
  46. private set;
  47. }
  48. public string FromRootPath
  49. {
  50. get
  51. {
  52. return Folder.Folder == null ? Name : Utility.Text.Format("{0}/{1}", Folder.FromRootPath, Name);
  53. }
  54. }
  55. public int Depth
  56. {
  57. get
  58. {
  59. return Folder != null ? Folder.Depth + 1 : 0;
  60. }
  61. }
  62. public Texture Icon
  63. {
  64. get
  65. {
  66. if (Resource.IsLoadFromBinary)
  67. {
  68. Asset asset = Resource.GetFirstAsset();
  69. if (asset != null)
  70. {
  71. Texture texture = AssetDatabase.GetCachedIcon(AssetDatabase.GUIDToAssetPath(asset.Guid));
  72. return texture != null ? texture : CachedUnknownIcon;
  73. }
  74. }
  75. else
  76. {
  77. switch (Resource.AssetType)
  78. {
  79. case AssetType.Asset:
  80. return CachedAssetIcon;
  81. case AssetType.Scene:
  82. return CachedSceneIcon;
  83. }
  84. }
  85. return CachedUnknownIcon;
  86. }
  87. }
  88. private static Texture CachedUnknownIcon
  89. {
  90. get
  91. {
  92. if (s_CachedUnknownIcon == null)
  93. {
  94. string iconName = null;
  95. #if UNITY_2018_3_OR_NEWER
  96. iconName = "GameObject Icon";
  97. #else
  98. iconName = "Prefab Icon";
  99. #endif
  100. s_CachedUnknownIcon = GetIcon(iconName);
  101. }
  102. return s_CachedUnknownIcon;
  103. }
  104. }
  105. private static Texture CachedAssetIcon
  106. {
  107. get
  108. {
  109. if (s_CachedAssetIcon == null)
  110. {
  111. string iconName = null;
  112. #if UNITY_2018_3_OR_NEWER
  113. iconName = "Prefab Icon";
  114. #else
  115. iconName = "PrefabNormal Icon";
  116. #endif
  117. s_CachedAssetIcon = GetIcon(iconName);
  118. }
  119. return s_CachedAssetIcon;
  120. }
  121. }
  122. private static Texture CachedSceneIcon
  123. {
  124. get
  125. {
  126. if (s_CachedSceneIcon == null)
  127. {
  128. s_CachedSceneIcon = GetIcon("SceneAsset Icon");
  129. }
  130. return s_CachedSceneIcon;
  131. }
  132. }
  133. private static Texture GetIcon(string iconName)
  134. {
  135. return EditorGUIUtility.IconContent(iconName).image;
  136. }
  137. }
  138. }
  139. }