Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A sample MonoBehaviour for Spine-Unity that holds a collection of SkeletonAnimation components and updates their animation at a certain limited rate.
using UnityEngine;
using System.Collections.Generic;
namespace Spine.Unity.Examples {
public class SampleManualUpdate : MonoBehaviour {
[Range(0f, 1/8f)]
[Tooltip("To specify a framerate, type 1/framerate in the inspector text box.")]
public float timeBetweenFrames = 1f/24f; //24 fps
public List<SkeletonAnimation> components;
float timeOfLastUpdate;
float accumulator;
bool doLateUpdate;
void OnValidate () {
if (timeBetweenFrames < 0)
timeBetweenFrames = 0;
}
void OnEnable () {
timeOfLastUpdate = Time.time;
}
void Start () {
for (int i = 0, n = components.Count; i < n; i++) {
var skeletonAnimation = components[i];
skeletonAnimation.Initialize(false);
skeletonAnimation.clearStateOnDisable = false;
skeletonAnimation.enabled = false;
skeletonAnimation.Update(0);
skeletonAnimation.LateUpdate();
}
}
void Update () {
if (timeBetweenFrames <= 0) return;
accumulator += Time.deltaTime;
bool doUpdate = false;
if (accumulator > timeBetweenFrames) {
accumulator = accumulator % timeBetweenFrames;
doUpdate = true;
}
if (doUpdate) {
float time = Time.time;
float updateDelta = time - timeOfLastUpdate;
for (int i = 0, n = components.Count; i < n; i++)
components[i].Update(updateDelta); // Update AnimationState
doLateUpdate = true;
timeOfLastUpdate = time;
}
}
void LateUpdate () {
if (doLateUpdate) {
for (int i = 0, n = components.Count; i < n; i++)
components[i].LateUpdate(); // Generate Mesh
doLateUpdate = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.