Skip to content

Instantly share code, notes, and snippets.

@pharan
Created January 9, 2017 22:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pharan/f5a58df99b6ed922c6916428990dcc12 to your computer and use it in GitHub Desktop.
Save pharan/f5a58df99b6ed922c6916428990dcc12 to your computer and use it in GitHub Desktop.
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