SourceFolder.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 System.Collections.Generic;
  9. using UnityEditor;
  10. using UnityEngine;
  11. namespace UnityGameFramework.Editor.ResourceTools
  12. {
  13. public sealed class SourceFolder
  14. {
  15. private static Texture s_CachedIcon = null;
  16. private readonly List<SourceFolder> m_Folders;
  17. private readonly List<SourceAsset> m_Assets;
  18. public SourceFolder(string name, SourceFolder folder)
  19. {
  20. m_Folders = new List<SourceFolder>();
  21. m_Assets = new List<SourceAsset>();
  22. Name = name;
  23. Folder = folder;
  24. }
  25. public string Name
  26. {
  27. get;
  28. private set;
  29. }
  30. public SourceFolder Folder
  31. {
  32. get;
  33. private set;
  34. }
  35. public string FromRootPath
  36. {
  37. get
  38. {
  39. return Folder == null ? string.Empty : (Folder.Folder == null ? Name : Utility.Text.Format("{0}/{1}", Folder.FromRootPath, Name));
  40. }
  41. }
  42. public int Depth
  43. {
  44. get
  45. {
  46. return Folder != null ? Folder.Depth + 1 : 0;
  47. }
  48. }
  49. public static Texture Icon
  50. {
  51. get
  52. {
  53. if (s_CachedIcon == null)
  54. {
  55. s_CachedIcon = AssetDatabase.GetCachedIcon("Assets");
  56. }
  57. return s_CachedIcon;
  58. }
  59. }
  60. public void Clear()
  61. {
  62. m_Folders.Clear();
  63. m_Assets.Clear();
  64. }
  65. public SourceFolder[] GetFolders()
  66. {
  67. return m_Folders.ToArray();
  68. }
  69. public SourceFolder GetFolder(string name)
  70. {
  71. if (string.IsNullOrEmpty(name))
  72. {
  73. throw new GameFrameworkException("Source folder name is invalid.");
  74. }
  75. foreach (SourceFolder folder in m_Folders)
  76. {
  77. if (folder.Name == name)
  78. {
  79. return folder;
  80. }
  81. }
  82. return null;
  83. }
  84. public SourceFolder AddFolder(string name)
  85. {
  86. if (string.IsNullOrEmpty(name))
  87. {
  88. throw new GameFrameworkException("Source folder name is invalid.");
  89. }
  90. SourceFolder folder = GetFolder(name);
  91. if (folder != null)
  92. {
  93. throw new GameFrameworkException("Source folder is already exist.");
  94. }
  95. folder = new SourceFolder(name, this);
  96. m_Folders.Add(folder);
  97. return folder;
  98. }
  99. public SourceAsset[] GetAssets()
  100. {
  101. return m_Assets.ToArray();
  102. }
  103. public SourceAsset GetAsset(string name)
  104. {
  105. if (string.IsNullOrEmpty(name))
  106. {
  107. throw new GameFrameworkException("Source asset name is invalid.");
  108. }
  109. foreach (SourceAsset asset in m_Assets)
  110. {
  111. if (asset.Name == name)
  112. {
  113. return asset;
  114. }
  115. }
  116. return null;
  117. }
  118. public SourceAsset AddAsset(string guid, string path, string name)
  119. {
  120. if (string.IsNullOrEmpty(guid))
  121. {
  122. throw new GameFrameworkException("Source asset guid is invalid.");
  123. }
  124. if (string.IsNullOrEmpty(path))
  125. {
  126. throw new GameFrameworkException("Source asset path is invalid.");
  127. }
  128. if (string.IsNullOrEmpty(name))
  129. {
  130. throw new GameFrameworkException("Source asset name is invalid.");
  131. }
  132. SourceAsset asset = GetAsset(name);
  133. if (asset != null)
  134. {
  135. throw new GameFrameworkException(Utility.Text.Format("Source asset '{0}' is already exist.", name));
  136. }
  137. asset = new SourceAsset(guid, path, name, this);
  138. m_Assets.Add(asset);
  139. return asset;
  140. }
  141. }
  142. }