Code cleanup, fixed empty skeleton data errors, renderer materials are updated once with submeshes cached

This commit is contained in:
Andrey Viktorov 2013-06-13 19:56:29 +07:00
parent ba279725a8
commit 1f8e251732
3 changed files with 27 additions and 26 deletions

View File

@ -11,11 +11,11 @@ public class tk2dSpineAnimation : MonoBehaviour {
public float animationSpeed = 1; public float animationSpeed = 1;
public Spine.AnimationState state; public Spine.AnimationState state;
private tk2dSpineSkeleton spineSkeleton; private tk2dSpineSkeleton cachedSpineSkeleton;
void Start () { void Start () {
spineSkeleton = GetComponent<tk2dSpineSkeleton>(); cachedSpineSkeleton = GetComponent<tk2dSpineSkeleton>();
state = new Spine.AnimationState(spineSkeleton.skeletonDataAsset.GetAnimationStateData()); state = new Spine.AnimationState(cachedSpineSkeleton.skeletonDataAsset.GetAnimationStateData());
} }
void Update () { void Update () {
@ -26,17 +26,19 @@ public class tk2dSpineAnimation : MonoBehaviour {
// Check if we need to stop current animation // Check if we need to stop current animation
if (state.Animation != null && animationName == null) { if (state.Animation != null && animationName == null) {
state.ClearAnimation(); state.ClearAnimation();
} else if (state.Animation == null || animationName != state.Animation.Name) { }
// Check for different animation name or animation end
Spine.Animation animation = spineSkeleton.skeleton.Data.FindAnimation(animationName); // Check for different animation name or animation end
else if (state.Animation == null || animationName != state.Animation.Name) {
Spine.Animation animation = cachedSpineSkeleton.skeleton.Data.FindAnimation(animationName);
if (animation != null) state.SetAnimation(animation,loop); if (animation != null) state.SetAnimation(animation,loop);
} }
state.Loop = loop; state.Loop = loop;
// Update animation // Update animation
spineSkeleton.skeleton.Update(Time.deltaTime * animationSpeed); cachedSpineSkeleton.skeleton.Update(Time.deltaTime * animationSpeed);
state.Update(Time.deltaTime * animationSpeed); state.Update(Time.deltaTime * animationSpeed);
state.Apply(spineSkeleton.skeleton); state.Apply(cachedSpineSkeleton.skeleton);
} }
} }

View File

@ -2,8 +2,6 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using Spine; using Spine;
// TODO: multiple atlas support
[ExecuteInEditMode] [ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))] [RequireComponent(typeof(MeshRenderer))]
@ -19,12 +17,14 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
private float[] vertexPositions; private float[] vertexPositions;
private List<Material> submeshMaterials = new List<Material>(); private List<Material> submeshMaterials = new List<Material>();
private List<int[]> submeshIndices = new List<int[]>(); private List<int[]> submeshIndices = new List<int[]>();
private Color cachedCurrentColor;
void Awake() { void Awake() {
vertexPositions = new float[8]; vertexPositions = new float[8];
submeshMaterials = new List<Material>(); submeshMaterials = new List<Material>();
submeshIndices = new List<int[]>(); submeshIndices = new List<int[]>();
cachedCurrentColor = new Color();
} }
void Start () { void Start () {
@ -38,7 +38,7 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
return; return;
} }
if(skeleton == null || skeleton.Data != skeletonData) Initialize(); if (skeleton == null || skeleton.Data != skeletonData) Initialize();
skeleton.UpdateWorldTransform(); skeleton.UpdateWorldTransform();
@ -51,8 +51,6 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
DestroyImmediate(mesh); DestroyImmediate(mesh);
mesh = null; mesh = null;
renderer.sharedMaterials = null;
skeleton = null; skeleton = null;
} }
@ -62,13 +60,14 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
mesh.name = "tk2dSkeleton Mesh"; mesh.name = "tk2dSkeleton Mesh";
mesh.hideFlags = HideFlags.HideAndDontSave; mesh.hideFlags = HideFlags.HideAndDontSave;
skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData()); if(skeletonDataAsset != null) {
skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData());
}
} }
private void UpdateMesh() { private void UpdateMesh() {
int quadIndex = 0; int quadIndex = 0;
int drawCount = skeleton.DrawOrder.Count; int drawCount = skeleton.DrawOrder.Count;
Color currentColor = new Color();
for (int i = 0; i < drawCount; i++) { for (int i = 0; i < drawCount; i++) {
Slot slot = skeleton.DrawOrder[i]; Slot slot = skeleton.DrawOrder[i];
@ -91,15 +90,15 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
uvs[vertexIndex + 2] = new Vector2(regionUVs[RegionAttachment.X2],regionUVs[RegionAttachment.Y2]); uvs[vertexIndex + 2] = new Vector2(regionUVs[RegionAttachment.X2],regionUVs[RegionAttachment.Y2]);
uvs[vertexIndex + 3] = new Vector2(regionUVs[RegionAttachment.X3],regionUVs[RegionAttachment.Y3]); uvs[vertexIndex + 3] = new Vector2(regionUVs[RegionAttachment.X3],regionUVs[RegionAttachment.Y3]);
currentColor.a = skeleton.A * slot.A; cachedCurrentColor.a = skeleton.A * slot.A;
currentColor.r = skeleton.R * slot.R * slot.A; cachedCurrentColor.r = skeleton.R * slot.R * slot.A;
currentColor.g = skeleton.G * slot.G * slot.A; cachedCurrentColor.g = skeleton.G * slot.G * slot.A;
currentColor.b = skeleton.B * slot.B * slot.A; cachedCurrentColor.b = skeleton.B * slot.B * slot.A;
colors[vertexIndex] = currentColor; colors[vertexIndex] = cachedCurrentColor;
colors[vertexIndex + 1] = currentColor; colors[vertexIndex + 1] = cachedCurrentColor;
colors[vertexIndex + 2] = currentColor; colors[vertexIndex + 2] = cachedCurrentColor;
colors[vertexIndex + 3] = currentColor; colors[vertexIndex + 3] = cachedCurrentColor;
quadIndex++; quadIndex++;
} }
@ -118,7 +117,7 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
if (skeletonDataAsset.normalGenerationMode != tk2dSpriteCollection.NormalGenerationMode.None) { if (skeletonDataAsset.normalGenerationMode != tk2dSpriteCollection.NormalGenerationMode.None) {
mesh.RecalculateNormals(); mesh.RecalculateNormals();
if (skeletonDataAsset.normalGenerationMode == tk2dSpriteCollection.NormalGenerationMode.NormalsAndTangents) { if (skeletonDataAsset.normalGenerationMode == tk2dSpriteCollection.NormalGenerationMode.NormalsAndTangents) {
Vector4[] tangents = new Vector4[mesh.normals.Length]; Vector4[] tangents = new Vector4[mesh.normals.Length];
for (int i = 0; i < tangents.Length; i++) { for (int i = 0; i < tangents.Length; i++) {
@ -127,8 +126,6 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
mesh.tangents = tangents; mesh.tangents = tangents;
} }
} }
renderer.sharedMaterials = submeshMaterials.ToArray();
} }
private void UpdateCache() { private void UpdateCache() {
@ -189,6 +186,8 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
submeshIndices.Add(currentSubmesh.ToArray()); submeshIndices.Add(currentSubmesh.ToArray());
submeshMaterials.Add(oldMaterial); submeshMaterials.Add(oldMaterial);
renderer.sharedMaterials = submeshMaterials.ToArray();
} }

Binary file not shown.