ResourceSyncToolsController.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 System.Linq;
  10. using UnityEditor;
  11. namespace UnityGameFramework.Editor.ResourceTools
  12. {
  13. public sealed class ResourceSyncToolsController
  14. {
  15. public ResourceSyncToolsController()
  16. {
  17. }
  18. public event GameFrameworkAction<int, int> OnLoadingResource = null;
  19. public event GameFrameworkAction<int, int> OnLoadingAsset = null;
  20. public event GameFrameworkAction OnCompleted = null;
  21. public event GameFrameworkAction<int, int, string> OnResourceDataChanged = null;
  22. public string[] GetAllAssetBundleNames()
  23. {
  24. return AssetDatabase.GetAllAssetBundleNames();
  25. }
  26. public string[] GetUsedAssetBundleNames()
  27. {
  28. HashSet<string> hashSet = new HashSet<string>(GetAllAssetBundleNames());
  29. hashSet.ExceptWith(GetUnusedAssetBundleNames());
  30. return hashSet.ToArray();
  31. }
  32. public string[] GetUnusedAssetBundleNames()
  33. {
  34. return AssetDatabase.GetUnusedAssetBundleNames();
  35. }
  36. public string[] GetAssetPathsFromAssetBundle(string assetBundleName)
  37. {
  38. return AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
  39. }
  40. public string[] GetAssetPathsFromAssetBundleAndAssetName(string assetBundleName, string assetName)
  41. {
  42. return AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(assetBundleName, assetName);
  43. }
  44. public bool RemoveAssetBundleName(string assetBundleName, bool forceRemove)
  45. {
  46. return AssetDatabase.RemoveAssetBundleName(assetBundleName, forceRemove);
  47. }
  48. public void RemoveUnusedAssetBundleNames()
  49. {
  50. AssetDatabase.RemoveUnusedAssetBundleNames();
  51. }
  52. public bool RemoveAllAssetBundleNames()
  53. {
  54. HashSet<string> allAssetNames = new HashSet<string>();
  55. string[] assetBundleNames = GetUsedAssetBundleNames();
  56. foreach (string assetBundleName in assetBundleNames)
  57. {
  58. string[] assetNames = GetAssetPathsFromAssetBundle(assetBundleName);
  59. foreach (string assetName in assetNames)
  60. {
  61. allAssetNames.Add(assetName);
  62. }
  63. }
  64. int assetIndex = 0;
  65. int assetCount = allAssetNames.Count;
  66. foreach (string assetName in allAssetNames)
  67. {
  68. AssetImporter assetImporter = AssetImporter.GetAtPath(assetName);
  69. if (assetImporter == null)
  70. {
  71. if (OnCompleted != null)
  72. {
  73. OnCompleted();
  74. }
  75. return false;
  76. }
  77. assetImporter.assetBundleVariant = null;
  78. assetImporter.assetBundleName = null;
  79. assetImporter.SaveAndReimport();
  80. if (OnResourceDataChanged != null)
  81. {
  82. OnResourceDataChanged(++assetIndex, assetCount, assetName);
  83. }
  84. }
  85. RemoveUnusedAssetBundleNames();
  86. if (OnCompleted != null)
  87. {
  88. OnCompleted();
  89. }
  90. return true;
  91. }
  92. public bool SyncToProject()
  93. {
  94. ResourceCollection resourceCollection = new ResourceCollection();
  95. resourceCollection.OnLoadingResource += delegate (int index, int count)
  96. {
  97. if (OnLoadingResource != null)
  98. {
  99. OnLoadingResource(index, count);
  100. }
  101. };
  102. resourceCollection.OnLoadingAsset += delegate (int index, int count)
  103. {
  104. if (OnLoadingAsset != null)
  105. {
  106. OnLoadingAsset(index, count);
  107. }
  108. };
  109. resourceCollection.OnLoadCompleted += delegate ()
  110. {
  111. if (OnCompleted != null)
  112. {
  113. OnCompleted();
  114. }
  115. };
  116. if (!resourceCollection.Load())
  117. {
  118. return false;
  119. }
  120. int assetIndex = 0;
  121. int assetCount = resourceCollection.AssetCount;
  122. Resource[] resources = resourceCollection.GetResources();
  123. foreach (Resource resource in resources)
  124. {
  125. if (resource.IsLoadFromBinary)
  126. {
  127. continue;
  128. }
  129. Asset[] assets = resource.GetAssets();
  130. foreach (Asset asset in assets)
  131. {
  132. AssetImporter assetImporter = AssetImporter.GetAtPath(asset.Name);
  133. if (assetImporter == null)
  134. {
  135. if (OnCompleted != null)
  136. {
  137. OnCompleted();
  138. }
  139. return false;
  140. }
  141. assetImporter.assetBundleName = resource.Name;
  142. assetImporter.assetBundleVariant = resource.Variant;
  143. assetImporter.SaveAndReimport();
  144. if (OnResourceDataChanged != null)
  145. {
  146. OnResourceDataChanged(++assetIndex, assetCount, asset.Name);
  147. }
  148. }
  149. }
  150. if (OnCompleted != null)
  151. {
  152. OnCompleted();
  153. }
  154. return true;
  155. }
  156. public bool SyncFromProject()
  157. {
  158. ResourceCollection resourceCollection = new ResourceCollection();
  159. string[] assetBundleNames = GetUsedAssetBundleNames();
  160. foreach (string assetBundleName in assetBundleNames)
  161. {
  162. string name = assetBundleName;
  163. string variant = null;
  164. int dotPosition = assetBundleName.LastIndexOf('.');
  165. if (dotPosition > 0 && dotPosition < assetBundleName.Length - 1)
  166. {
  167. name = assetBundleName.Substring(0, dotPosition);
  168. variant = assetBundleName.Substring(dotPosition + 1);
  169. }
  170. if (!resourceCollection.AddResource(name, variant, null, LoadType.LoadFromFile, false))
  171. {
  172. return false;
  173. }
  174. string[] assetNames = GetAssetPathsFromAssetBundle(assetBundleName);
  175. foreach (string assetName in assetNames)
  176. {
  177. string guid = AssetDatabase.AssetPathToGUID(assetName);
  178. if (string.IsNullOrEmpty(guid))
  179. {
  180. return false;
  181. }
  182. if (!resourceCollection.AssignAsset(guid, name, variant))
  183. {
  184. return false;
  185. }
  186. }
  187. }
  188. return resourceCollection.Save();
  189. }
  190. }
  191. }