瀏覽代碼

[unity] Unity SpriteAtlas Support: Removed unnecessary material reference to generated editor atlas texture in pre-build step. See #1900.

Harald Csaszar 3 年之前
父節點
當前提交
5af852d83f
共有 1 個文件被更改,包括 52 次插入12 次删除
  1. 52 12
      spine-unity/Assets/Spine/Editor/spine-unity/Editor/Utility/SpineBuildProcessor.cs

+ 52 - 12
spine-unity/Assets/Spine/Editor/spine-unity/Editor/Utility/SpineBuildProcessor.cs

@@ -50,31 +50,73 @@ namespace Spine.Unity.Editor {
 #if HAS_ON_POSTPROCESS_PREFAB
 #if HAS_ON_POSTPROCESS_PREFAB
 		static List<string> prefabsToRestore = new List<string>();
 		static List<string> prefabsToRestore = new List<string>();
 #endif
 #endif
+		static Dictionary<string, Texture> spriteAtlasTexturesToRestore = new Dictionary<string, Texture>();
 
 
 		internal static void PreprocessBuild () {
 		internal static void PreprocessBuild () {
 			isBuilding = true;
 			isBuilding = true;
 #if HAS_ON_POSTPROCESS_PREFAB
 #if HAS_ON_POSTPROCESS_PREFAB
-			var assets = AssetDatabase.FindAssets("t:Prefab");
-			foreach(var asset in assets) {
+			PreprocessSpinePrefabMeshes();
+#endif
+			PreprocessSpriteAtlases();
+		}
+
+		internal static void PostprocessBuild () {
+			isBuilding = false;
+#if HAS_ON_POSTPROCESS_PREFAB
+			PostprocessSpinePrefabMeshes();
+#endif
+			PostprocessSpriteAtlases();
+		}
+
+#if HAS_ON_POSTPROCESS_PREFAB
+		internal static void PreprocessSpinePrefabMeshes () {
+			var prefabAssets = AssetDatabase.FindAssets("t:Prefab");
+			foreach (var asset in prefabAssets) {
 				string assetPath = AssetDatabase.GUIDToAssetPath(asset);
 				string assetPath = AssetDatabase.GUIDToAssetPath(asset);
 				GameObject g = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
 				GameObject g = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
 				if (SpineEditorUtilities.CleanupSpinePrefabMesh(g)) {
 				if (SpineEditorUtilities.CleanupSpinePrefabMesh(g)) {
 					prefabsToRestore.Add(assetPath);
 					prefabsToRestore.Add(assetPath);
 				}
 				}
 			}
 			}
-			AssetDatabase.SaveAssets();
-#endif
+			if (prefabAssets.Length > 0)
+				AssetDatabase.SaveAssets();
 		}
 		}
 
 
-		internal static void PostprocessBuild () {
-			isBuilding = false;
-#if HAS_ON_POSTPROCESS_PREFAB
+		internal static void PostprocessSpinePrefabMeshes () {
 			foreach (string assetPath in prefabsToRestore) {
 			foreach (string assetPath in prefabsToRestore) {
 				GameObject g = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
 				GameObject g = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
 				SpineEditorUtilities.SetupSpinePrefabMesh(g, null);
 				SpineEditorUtilities.SetupSpinePrefabMesh(g, null);
 			}
 			}
-			AssetDatabase.SaveAssets();
+			if (prefabsToRestore.Count > 0)
+				AssetDatabase.SaveAssets();
+			prefabsToRestore.Clear();
+		}
 #endif
 #endif
+		internal static void PreprocessSpriteAtlases () {
+			var spriteAtlasAssets = AssetDatabase.FindAssets("t:SpineSpriteAtlasAsset");
+			foreach (var asset in spriteAtlasAssets) {
+				string assetPath = AssetDatabase.GUIDToAssetPath(asset);
+				SpineSpriteAtlasAsset atlasAsset = AssetDatabase.LoadAssetAtPath<SpineSpriteAtlasAsset>(assetPath);
+				if (atlasAsset && atlasAsset.materials.Length > 0) {
+					spriteAtlasTexturesToRestore[assetPath] = atlasAsset.materials[0].mainTexture;
+					atlasAsset.materials[0].mainTexture = null;
+				}
+			}
+			if (spriteAtlasAssets.Length > 0)
+				AssetDatabase.SaveAssets();
+		}
+
+		internal static void PostprocessSpriteAtlases () {
+			foreach (var pair in spriteAtlasTexturesToRestore) {
+				string assetPath = pair.Key;
+				SpineSpriteAtlasAsset atlasAsset = AssetDatabase.LoadAssetAtPath<SpineSpriteAtlasAsset>(assetPath);
+				if (atlasAsset && atlasAsset.materials.Length > 0) {
+					atlasAsset.materials[0].mainTexture = pair.Value;
+				}
+			}
+			if (spriteAtlasTexturesToRestore.Count > 0)
+				AssetDatabase.SaveAssets();
+			spriteAtlasTexturesToRestore.Clear();
 		}
 		}
 	}
 	}
 
 
@@ -89,8 +131,7 @@ namespace Spine.Unity.Editor {
 			get { return -2000; }
 			get { return -2000; }
 		}
 		}
 #if HAS_BUILD_PROCESS_WITH_REPORT
 #if HAS_BUILD_PROCESS_WITH_REPORT
-		void IPreprocessBuildWithReport.OnPreprocessBuild(BuildReport report)
-		{
+		void IPreprocessBuildWithReport.OnPreprocessBuild (BuildReport report) {
 			SpineBuildProcessor.PreprocessBuild();
 			SpineBuildProcessor.PreprocessBuild();
 		}
 		}
 #else
 #else
@@ -113,8 +154,7 @@ namespace Spine.Unity.Editor {
 
 
 
 
 #if HAS_BUILD_PROCESS_WITH_REPORT
 #if HAS_BUILD_PROCESS_WITH_REPORT
-		void IPostprocessBuildWithReport.OnPostprocessBuild(BuildReport report)
-		{
+		void IPostprocessBuildWithReport.OnPostprocessBuild (BuildReport report) {
 			SpineBuildProcessor.PostprocessBuild();
 			SpineBuildProcessor.PostprocessBuild();
 		}
 		}
 #else
 #else