override public void UpdateSkeleton() { if (useAnimationName) { // Keep AnimationState in sync with animationName and loop fields. if (animationName == null || animationName.Length == 0) { if (state.Animation != null) { state.ClearAnimation(); } } else if (state.Animation == null || animationName != state.Animation.Name) { Spine.Animation animation = skeleton.Data.FindAnimation(animationName); if (animation != null) { state.SetAnimation(animation, loop); } } state.Loop = loop; } // Apply the animation. state.Update(Time.deltaTime * timeScale); state.Apply(skeleton); // Call overridden method to call skeleton Update and UpdateWorldTransform. base.UpdateSkeleton(); }
private void UpdateAnimation() { // Check if we need to stop current animation if (state.Animation != null && animationName == null) { state.ClearAnimation(); } // 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); } } state.Loop = loop; // Update animation cachedSpineSkeleton.skeleton.Update(Time.deltaTime * animationSpeed); state.Update(Time.deltaTime * animationSpeed); state.Apply(cachedSpineSkeleton.skeleton); }
public void Update() { // Clear fields if missing information to render. if (skeletonDataAsset == null || skeletonDataAsset.GetSkeletonData(false) == null) { Clear(); return; } // Initialize fields. if (skeleton == null || skeleton.Data != skeletonDataAsset.GetSkeletonData(false)) { Initialize(); } // Keep AnimationState in sync with animationName and loop fields. if (animationName == null && state.Animation != null) { state.ClearAnimation(); } else if (state.Animation == null || animationName != state.Animation.Name) { Spine.Animation animation = skeleton.Data.FindAnimation(animationName); if (animation != null) { state.SetAnimation(animation, loop); } } state.Loop = loop; UpdateAnimation(); // Count quads. int quadCount = 0; List <Slot> drawOrder = skeleton.DrawOrder; for (int i = 0, n = drawOrder.Count; i < n; i++) { Slot slot = drawOrder[i]; Attachment attachment = slot.Attachment; if (attachment is RegionAttachment) { quadCount++; } } // Ensure mesh data is the right size. if (quadCount != this.quadCount) { this.quadCount = quadCount; vertices = new Vector3[quadCount * 4]; uvs = new Vector2[quadCount * 4]; triangles = new int[quadCount * 6]; } // Setup mesh. int quadIndex = 0; for (int i = 0, n = drawOrder.Count; i < n; i++) { Slot slot = drawOrder[i]; Attachment attachment = slot.Attachment; if (attachment is RegionAttachment) { RegionAttachment regionAttachment = (RegionAttachment)attachment; regionAttachment.UpdateVertices(slot.Bone); float[] regionVertices = regionAttachment.Vertices; int vertexIndex = quadIndex * 4; vertices[vertexIndex] = new Vector3(regionVertices[RegionAttachment.X1], regionVertices[RegionAttachment.Y1], 0); vertices[vertexIndex + 1] = new Vector3(regionVertices[RegionAttachment.X4], regionVertices[RegionAttachment.Y4], 0); vertices[vertexIndex + 2] = new Vector3(regionVertices[RegionAttachment.X2], regionVertices[RegionAttachment.Y2], 0); vertices[vertexIndex + 3] = new Vector3(regionVertices[RegionAttachment.X3], regionVertices[RegionAttachment.Y3], 0); AtlasRegion region = regionAttachment.Region; if (region.rotate) { uvs[vertexIndex + 1] = new Vector2(region.u, 1 - region.v2); uvs[vertexIndex + 2] = new Vector2(region.u2, 1 - region.v2); uvs[vertexIndex + 3] = new Vector2(region.u, 1 - region.v); uvs[vertexIndex] = new Vector2(region.u2, 1 - region.v); } else { uvs[vertexIndex] = new Vector2(region.u, 1 - region.v2); uvs[vertexIndex + 1] = new Vector2(region.u2, 1 - region.v2); uvs[vertexIndex + 2] = new Vector2(region.u, 1 - region.v); uvs[vertexIndex + 3] = new Vector2(region.u2, 1 - region.v); } int index = quadIndex * 6; triangles[index] = vertexIndex; triangles[index + 1] = vertexIndex + 2; triangles[index + 2] = vertexIndex + 1; triangles[index + 3] = vertexIndex + 2; triangles[index + 4] = vertexIndex + 3; triangles[index + 5] = vertexIndex + 1; quadIndex++; } } mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = triangles; renderer.sharedMaterial = skeletonDataAsset.atlasAsset.material; }
public void Update() { // Clear fields if missing information to render. if (skeletonDataAsset == null || skeletonDataAsset.GetSkeletonData(false) == null) { Clear(); return; } // Initialize fields. if (skeleton == null || skeleton.Data != skeletonDataAsset.GetSkeletonData(false)) { Initialize(); } // Keep AnimationState in sync with animationName and loop fields. if (animationName == null || animationName.Length == 0) { if (state.Animation != null) { state.ClearAnimation(); } } else if (state.Animation == null || animationName != state.Animation.Name) { Spine.Animation animation = skeleton.Data.FindAnimation(animationName); if (animation != null) { state.SetAnimation(animation, loop); } } state.Loop = loop; // Keep Skeleton in sync with skinName. if (skinName == null || skinName.Length == 0) { if (skeleton.Skin != null) { skeleton.SetSkin((Skin)null); skeleton.SetSlotsToBindPose(); } } else if (skeleton.Skin == null || skinName != skeleton.Skin.Name) { skeleton.SetSkin(skinName); skeleton.SetSlotsToBindPose(); } UpdateAnimation(); // Count quads. int quadCount = 0; List <Slot> drawOrder = skeleton.DrawOrder; for (int i = 0, n = drawOrder.Count; i < n; i++) { Slot slot = drawOrder[i]; Attachment attachment = slot.Attachment; if (attachment is RegionAttachment) { quadCount++; } } // Ensure mesh data is the right size. if (quadCount != this.quadCount) { this.quadCount = quadCount; vertices = new Vector3[quadCount * 4]; colors = new Color[quadCount * 4]; uvs = new Vector2[quadCount * 4]; triangles = new int[quadCount * 6]; mesh.Clear(); } // Setup mesh. int quadIndex = 0; Color color = new Color(); for (int i = 0, n = drawOrder.Count; i < n; i++) { Slot slot = drawOrder[i]; Attachment attachment = slot.Attachment; if (attachment is RegionAttachment) { RegionAttachment regionAttachment = (RegionAttachment)attachment; regionAttachment.UpdateVertices(slot.Bone); float[] regionVertices = regionAttachment.Vertices; int vertexIndex = quadIndex * 4; vertices[vertexIndex] = new Vector3(regionVertices[RegionAttachment.X1], regionVertices[RegionAttachment.Y1], 0); vertices[vertexIndex + 1] = new Vector3(regionVertices[RegionAttachment.X4], regionVertices[RegionAttachment.Y4], 0); vertices[vertexIndex + 2] = new Vector3(regionVertices[RegionAttachment.X2], regionVertices[RegionAttachment.Y2], 0); vertices[vertexIndex + 3] = new Vector3(regionVertices[RegionAttachment.X3], regionVertices[RegionAttachment.Y3], 0); color.r = skeleton.R * slot.R; color.g = skeleton.G * slot.G; color.b = skeleton.B * slot.B; color.a = skeleton.A * slot.A; colors[vertexIndex] = color; colors[vertexIndex + 1] = color; colors[vertexIndex + 2] = color; colors[vertexIndex + 3] = color; float[] regionUVs = regionAttachment.UVs; uvs[vertexIndex] = new Vector2(regionUVs[RegionAttachment.X1], 1 - regionUVs[RegionAttachment.Y1]); uvs[vertexIndex + 1] = new Vector2(regionUVs[RegionAttachment.X4], 1 - regionUVs[RegionAttachment.Y4]); uvs[vertexIndex + 2] = new Vector2(regionUVs[RegionAttachment.X2], 1 - regionUVs[RegionAttachment.Y2]); uvs[vertexIndex + 3] = new Vector2(regionUVs[RegionAttachment.X3], 1 - regionUVs[RegionAttachment.Y3]); int index = quadIndex * 6; triangles[index] = vertexIndex; triangles[index + 1] = vertexIndex + 2; triangles[index + 2] = vertexIndex + 1; triangles[index + 3] = vertexIndex + 2; triangles[index + 4] = vertexIndex + 3; triangles[index + 5] = vertexIndex + 1; quadIndex++; } } mesh.vertices = vertices; mesh.colors = colors; mesh.uv = uvs; mesh.triangles = triangles; renderer.sharedMaterial = skeletonDataAsset.atlasAsset.material; }
// stop the current animation and reset to the base pose public void Stop() { animation.ClearAnimation(); skeleton.SetToBindPose(); _isSpineDirty = true; }