[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> /// <summary>
/// Arrays submeshed mesh generator. /// Arrays submeshed mesh generator.
/// </summary> /// </summary>
public class ArraysSubmeshedMeshGenerator : ArraysMeshGenerator, ISubmeshedMeshGenerator { public class ArraysSubmeshedMeshGenerator : ArraysMeshGenerator, ISubmeshedMeshGenerator, System.IDisposable {
readonly List<Slot> separators = new List<Slot>(); readonly List<Slot> separators = new List<Slot>();
public List<Slot> Separators { get { return this.separators; } } public List<Slot> Separators { get { return this.separators; } }
@ -50,6 +50,11 @@ namespace Spine.Unity.MeshGeneration {
readonly ExposedList<SubmeshTriangleBuffer> submeshBuffers = new ExposedList<SubmeshTriangleBuffer>(); readonly ExposedList<SubmeshTriangleBuffer> submeshBuffers = new ExposedList<SubmeshTriangleBuffer>();
Material[] sharedMaterials = new Material[0]; Material[] sharedMaterials = new Material[0];
public void Dispose () {
doubleBufferedSmartMesh.GetNext().Dispose();
doubleBufferedSmartMesh.GetNext().Dispose();
}
public SubmeshedMeshInstruction GenerateInstruction (Skeleton skeleton) { public SubmeshedMeshInstruction GenerateInstruction (Skeleton skeleton) {
if (skeleton == null) throw new System.ArgumentNullException("skeleton"); if (skeleton == null) throw new System.ArgumentNullException("skeleton");
@ -232,11 +237,21 @@ namespace Spine.Unity.MeshGeneration {
#region Types #region Types
// A SmartMesh is a Mesh (with submeshes) that knows what attachments and instructions were used to generate it. // 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(); public readonly Mesh mesh = SpineMesh.NewMesh();
readonly ExposedList<Attachment> attachmentsUsed = new ExposedList<Attachment>(); readonly ExposedList<Attachment> attachmentsUsed = new ExposedList<Attachment>();
readonly ExposedList<SubmeshInstruction> instructionsUsed = new ExposedList<SubmeshInstruction>(); 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) { public void Set (Vector3[] verts, Vector2[] uvs, Color32[] colors, SubmeshedMeshInstruction instruction) {
mesh.vertices = verts; mesh.vertices = verts;
mesh.uv = uvs; mesh.uv = uvs;

View File

@ -35,7 +35,6 @@
//#define SPINE_OPTIONAL_FRONTFACING //#define SPINE_OPTIONAL_FRONTFACING
using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using Spine.Unity.MeshGeneration; using Spine.Unity.MeshGeneration;
@ -165,6 +164,13 @@ namespace Spine.Unity {
ClearState(); ClearState();
} }
void OnDestroy () {
if (doubleBufferedMesh == null) return;
doubleBufferedMesh.GetNext().Dispose();
doubleBufferedMesh.GetNext().Dispose();
doubleBufferedMesh = null;
}
protected virtual void ClearState () { protected virtual void ClearState () {
meshFilter.sharedMesh = null; meshFilter.sharedMesh = null;
currentInstructions.Clear(); currentInstructions.Clear();
@ -740,10 +746,24 @@ namespace Spine.Unity {
#endif #endif
///<summary>This is a Mesh that also stores the instructions SkeletonRenderer generated for it.</summary> ///<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 Mesh mesh = Spine.Unity.SpineMesh.NewMesh();
public SmartMesh.Instruction instructionUsed = new SmartMesh.Instruction(); 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 class Instruction {
public bool immutableTriangles; public bool immutableTriangles;
public int vertexCount = -1; public int vertexCount = -1;