mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2025-12-21 01:36:02 +08:00
[unity] Improved memory utilization during build pre- and post processing.
This commit is contained in:
parent
689daa147a
commit
299ab8fff8
@ -51,7 +51,7 @@ 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>();
|
static Dictionary<string, string> spriteAtlasTexturesToRestore = new Dictionary<string, string>();
|
||||||
|
|
||||||
internal static void PreprocessBuild () {
|
internal static void PreprocessBuild () {
|
||||||
isBuilding = true;
|
isBuilding = true;
|
||||||
@ -71,19 +71,24 @@ namespace Spine.Unity.Editor {
|
|||||||
|
|
||||||
#if HAS_ON_POSTPROCESS_PREFAB
|
#if HAS_ON_POSTPROCESS_PREFAB
|
||||||
internal static void PreprocessSpinePrefabMeshes () {
|
internal static void PreprocessSpinePrefabMeshes () {
|
||||||
|
AssetDatabase.StartAssetEditing();
|
||||||
|
prefabsToRestore.Clear();
|
||||||
var prefabAssets = AssetDatabase.FindAssets("t:Prefab");
|
var prefabAssets = AssetDatabase.FindAssets("t:Prefab");
|
||||||
foreach (var asset in prefabAssets) {
|
foreach (var asset in prefabAssets) {
|
||||||
string assetPath = AssetDatabase.GUIDToAssetPath(asset);
|
string assetPath = AssetDatabase.GUIDToAssetPath(asset);
|
||||||
GameObject g = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
|
GameObject prefabGameObject = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
|
||||||
if (SpineEditorUtilities.CleanupSpinePrefabMesh(g)) {
|
if (SpineEditorUtilities.CleanupSpinePrefabMesh(prefabGameObject)) {
|
||||||
prefabsToRestore.Add(assetPath);
|
prefabsToRestore.Add(assetPath);
|
||||||
}
|
}
|
||||||
|
EditorUtility.UnloadUnusedAssetsImmediate();
|
||||||
}
|
}
|
||||||
|
AssetDatabase.StopAssetEditing();
|
||||||
if (prefabAssets.Length > 0)
|
if (prefabAssets.Length > 0)
|
||||||
AssetDatabase.SaveAssets();
|
AssetDatabase.SaveAssets();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void PostprocessSpinePrefabMeshes () {
|
internal static void PostprocessSpinePrefabMeshes () {
|
||||||
|
Debug.Log("PostprocessSpinePrefabMeshes called");
|
||||||
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);
|
||||||
@ -94,15 +99,19 @@ namespace Spine.Unity.Editor {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
internal static void PreprocessSpriteAtlases () {
|
internal static void PreprocessSpriteAtlases () {
|
||||||
|
AssetDatabase.StartAssetEditing();
|
||||||
|
spriteAtlasTexturesToRestore.Clear();
|
||||||
var spriteAtlasAssets = AssetDatabase.FindAssets("t:SpineSpriteAtlasAsset");
|
var spriteAtlasAssets = AssetDatabase.FindAssets("t:SpineSpriteAtlasAsset");
|
||||||
foreach (var asset in spriteAtlasAssets) {
|
foreach (var asset in spriteAtlasAssets) {
|
||||||
string assetPath = AssetDatabase.GUIDToAssetPath(asset);
|
string assetPath = AssetDatabase.GUIDToAssetPath(asset);
|
||||||
SpineSpriteAtlasAsset atlasAsset = AssetDatabase.LoadAssetAtPath<SpineSpriteAtlasAsset>(assetPath);
|
SpineSpriteAtlasAsset atlasAsset = AssetDatabase.LoadAssetAtPath<SpineSpriteAtlasAsset>(assetPath);
|
||||||
if (atlasAsset && atlasAsset.materials.Length > 0) {
|
if (atlasAsset && atlasAsset.materials.Length > 0) {
|
||||||
spriteAtlasTexturesToRestore[assetPath] = atlasAsset.materials[0].mainTexture;
|
spriteAtlasTexturesToRestore[assetPath] = AssetDatabase.GetAssetPath(atlasAsset.materials[0].mainTexture);
|
||||||
atlasAsset.materials[0].mainTexture = null;
|
atlasAsset.materials[0].mainTexture = null;
|
||||||
}
|
}
|
||||||
|
EditorUtility.UnloadUnusedAssetsImmediate();
|
||||||
}
|
}
|
||||||
|
AssetDatabase.StopAssetEditing();
|
||||||
if (spriteAtlasAssets.Length > 0)
|
if (spriteAtlasAssets.Length > 0)
|
||||||
AssetDatabase.SaveAssets();
|
AssetDatabase.SaveAssets();
|
||||||
}
|
}
|
||||||
@ -112,7 +121,8 @@ namespace Spine.Unity.Editor {
|
|||||||
string assetPath = pair.Key;
|
string assetPath = pair.Key;
|
||||||
SpineSpriteAtlasAsset atlasAsset = AssetDatabase.LoadAssetAtPath<SpineSpriteAtlasAsset>(assetPath);
|
SpineSpriteAtlasAsset atlasAsset = AssetDatabase.LoadAssetAtPath<SpineSpriteAtlasAsset>(assetPath);
|
||||||
if (atlasAsset && atlasAsset.materials.Length > 0) {
|
if (atlasAsset && atlasAsset.materials.Length > 0) {
|
||||||
atlasAsset.materials[0].mainTexture = pair.Value;
|
Texture atlasTexture = AssetDatabase.LoadAssetAtPath<Texture>(pair.Value);
|
||||||
|
atlasAsset.materials[0].mainTexture = atlasTexture;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (spriteAtlasTexturesToRestore.Count > 0)
|
if (spriteAtlasTexturesToRestore.Count > 0)
|
||||||
|
|||||||
@ -108,8 +108,7 @@ namespace Spine.Unity.Editor {
|
|||||||
SetupSpinePrefabMesh(g, context);
|
SetupSpinePrefabMesh(g, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool SetupSpinePrefabMesh(GameObject g, UnityEditor.AssetImporters.AssetImportContext context)
|
public static bool SetupSpinePrefabMesh (GameObject g, UnityEditor.AssetImporters.AssetImportContext context) {
|
||||||
{
|
|
||||||
Dictionary<string, int> nameUsageCount = new Dictionary<string, int>();
|
Dictionary<string, int> nameUsageCount = new Dictionary<string, int>();
|
||||||
bool wasModified = false;
|
bool wasModified = false;
|
||||||
var skeletonRenderers = g.GetComponentsInChildren<SkeletonRenderer>(true);
|
var skeletonRenderers = g.GetComponentsInChildren<SkeletonRenderer>(true);
|
||||||
@ -124,7 +123,7 @@ namespace Spine.Unity.Editor {
|
|||||||
renderer.LateUpdateMesh();
|
renderer.LateUpdateMesh();
|
||||||
var mesh = meshFilter.sharedMesh;
|
var mesh = meshFilter.sharedMesh;
|
||||||
if (mesh == null) continue;
|
if (mesh == null) continue;
|
||||||
|
|
||||||
string meshName = string.Format("Skeleton Prefab Mesh \"{0}\"", renderer.name);
|
string meshName = string.Format("Skeleton Prefab Mesh \"{0}\"", renderer.name);
|
||||||
if (nameUsageCount.ContainsKey(meshName)) {
|
if (nameUsageCount.ContainsKey(meshName)) {
|
||||||
nameUsageCount[meshName]++;
|
nameUsageCount[meshName]++;
|
||||||
@ -140,8 +139,7 @@ namespace Spine.Unity.Editor {
|
|||||||
return wasModified;
|
return wasModified;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool CleanupSpinePrefabMesh(GameObject g)
|
public static bool CleanupSpinePrefabMesh (GameObject g) {
|
||||||
{
|
|
||||||
bool wasModified = false;
|
bool wasModified = false;
|
||||||
var skeletonRenderers = g.GetComponentsInChildren<SkeletonRenderer>(true);
|
var skeletonRenderers = g.GetComponentsInChildren<SkeletonRenderer>(true);
|
||||||
foreach (SkeletonRenderer renderer in skeletonRenderers) {
|
foreach (SkeletonRenderer renderer in skeletonRenderers) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user