[unity] Manually destroy meshes where required.

This commit is contained in:
pharan 2017-03-16 12:15:58 +08:00
parent fe0b627c75
commit 953a98706f
2 changed files with 39 additions and 4 deletions

View File

@ -35,7 +35,7 @@ namespace Spine.Unity.MeshGeneration {
/// <summary>
/// Arrays submeshed mesh generator.
/// </summary>
public class ArraysSubmeshedMeshGenerator : ArraysMeshGenerator, ISubmeshedMeshGenerator {
public class ArraysSubmeshedMeshGenerator : ArraysMeshGenerator, ISubmeshedMeshGenerator, System.IDisposable {
readonly List<Slot> separators = new List<Slot>();
public List<Slot> Separators { get { return this.separators; } }
@ -50,6 +50,11 @@ namespace Spine.Unity.MeshGeneration {
readonly ExposedList<SubmeshTriangleBuffer> submeshBuffers = new ExposedList<SubmeshTriangleBuffer>();
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<Attachment> attachmentsUsed = new ExposedList<Attachment>();
readonly ExposedList<SubmeshInstruction> instructionsUsed = new ExposedList<SubmeshInstruction>();
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;

View File

@ -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
///<summary>This is a Mesh that also stores the instructions SkeletonRenderer generated for it.</summary>
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;