From 953a98706f8dd2f5b1b3782b74f1cfddbb36b76a Mon Sep 17 00:00:00 2001 From: pharan Date: Thu, 16 Mar 2017 12:15:58 +0800 Subject: [PATCH] [unity] Manually destroy meshes where required. --- .../Arrays/ArraysSubmeshedMeshGenerator.cs | 19 +++++++++++++-- .../Assets/spine-unity/SkeletonRenderer.cs | 24 +++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/spine-unity/Assets/spine-unity/Mesh Generation/Arrays/ArraysSubmeshedMeshGenerator.cs b/spine-unity/Assets/spine-unity/Mesh Generation/Arrays/ArraysSubmeshedMeshGenerator.cs index 691d2e3be..4f0bd474e 100644 --- a/spine-unity/Assets/spine-unity/Mesh Generation/Arrays/ArraysSubmeshedMeshGenerator.cs +++ b/spine-unity/Assets/spine-unity/Mesh Generation/Arrays/ArraysSubmeshedMeshGenerator.cs @@ -35,7 +35,7 @@ namespace Spine.Unity.MeshGeneration { /// /// Arrays submeshed mesh generator. /// - public class ArraysSubmeshedMeshGenerator : ArraysMeshGenerator, ISubmeshedMeshGenerator { + public class ArraysSubmeshedMeshGenerator : ArraysMeshGenerator, ISubmeshedMeshGenerator, System.IDisposable { readonly List separators = new List(); public List Separators { get { return this.separators; } } @@ -50,6 +50,11 @@ namespace Spine.Unity.MeshGeneration { readonly ExposedList submeshBuffers = new ExposedList(); Material[] sharedMaterials = new Material[0]; + public void Dispose () { + doubleBufferedSmartMesh.GetNext().Dispose(); + doubleBufferedSmartMesh.GetNext().Dispose(); + } + public SubmeshedMeshInstruction GenerateInstruction (Skeleton skeleton) { if (skeleton == null) throw new System.ArgumentNullException("skeleton"); @@ -232,11 +237,21 @@ namespace Spine.Unity.MeshGeneration { #region Types // A SmartMesh is a Mesh (with submeshes) that knows what attachments and instructions were used to generate it. - class SmartMesh { + class SmartMesh : System.IDisposable { public readonly Mesh mesh = SpineMesh.NewMesh(); readonly ExposedList attachmentsUsed = new ExposedList(); readonly ExposedList instructionsUsed = new ExposedList(); + public void Dispose () { + if (mesh != null) { + if (Application.isEditor && !Application.isPlaying) { + UnityEngine.Object.DestroyImmediate(mesh); + } else { + UnityEngine.Object.Destroy(mesh); + } + } + } + public void Set (Vector3[] verts, Vector2[] uvs, Color32[] colors, SubmeshedMeshInstruction instruction) { mesh.vertices = verts; mesh.uv = uvs; diff --git a/spine-unity/Assets/spine-unity/SkeletonRenderer.cs b/spine-unity/Assets/spine-unity/SkeletonRenderer.cs index 872e707b6..8917befc8 100644 --- a/spine-unity/Assets/spine-unity/SkeletonRenderer.cs +++ b/spine-unity/Assets/spine-unity/SkeletonRenderer.cs @@ -35,7 +35,6 @@ //#define SPINE_OPTIONAL_FRONTFACING -using System; using System.Collections.Generic; using UnityEngine; using Spine.Unity.MeshGeneration; @@ -165,6 +164,13 @@ namespace Spine.Unity { ClearState(); } + void OnDestroy () { + if (doubleBufferedMesh == null) return; + doubleBufferedMesh.GetNext().Dispose(); + doubleBufferedMesh.GetNext().Dispose(); + doubleBufferedMesh = null; + } + protected virtual void ClearState () { meshFilter.sharedMesh = null; currentInstructions.Clear(); @@ -740,10 +746,24 @@ namespace Spine.Unity { #endif ///This is a Mesh that also stores the instructions SkeletonRenderer generated for it. - public class SmartMesh { + public class SmartMesh : System.IDisposable { public Mesh mesh = Spine.Unity.SpineMesh.NewMesh(); public SmartMesh.Instruction instructionUsed = new SmartMesh.Instruction(); + public void Dispose () { + if (mesh != null) { + #if UNITY_EDITOR + if (Application.isEditor && !Application.isPlaying) + UnityEngine.Object.DestroyImmediate(mesh); + else + UnityEngine.Object.Destroy(mesh); + #else + UnityEngine.Object.Destroy(mesh); + #endif + } + mesh = null; + } + public class Instruction { public bool immutableTriangles; public int vertexCount = -1;